Socket网络编程:基于TCP多线程通信

第一步:编写启动服务端的线程类

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread {
	Socket socket = null;
	public ServerThread(Socket socket) {
		this.socket = socket;
	}
	
	
	@Override
	public void run() {
		InputStream is =null;
		InputStreamReader isr=null;
		BufferedReader reader=null;
		String response=null;
		OutputStream out=null;
		PrintWriter pw =null;
		try {
			is = socket.getInputStream();
			isr = new InputStreamReader(is);
			reader = new BufferedReader(isr,1024); 
			String info=null;
			while((info=reader.readLine())!=null){
				System.out.println(info);
			}
			
			socket.shutdownInput();
			
			response = "我是服务端,欢迎向服务端请求";
			out = socket.getOutputStream();
			pw= new PrintWriter(out);
			pw.write(response);
			pw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(pw!=null)
				pw.close();
				if(out!=null)
				out.close();
				if(reader!=null)
				reader.close();
				if(isr!=null)
				isr.close();
				if(is!=null)
				is.close();
				if(socket!=null)
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
第二步:编程服务端

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Servers {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(8888);
		System.out.println("****************服务端启动******************");
		int count =0;
		while(true){
			Socket socket = serverSocket.accept();
			ServerThread serverThread = new ServerThread(socket);
			count ++;
			
			serverThread.start();
			
			System.out.println(InetAddress.getLocalHost()+"***********");
		}
		/*
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader reader = new BufferedReader(isr,1024); 
		String info=null;
		while((info=reader.readLine())!=null){
			System.out.println(info);
		}
		
		socket.shutdownInput();
		
		String response = "我是服务端,欢迎向服务端请求";
		OutputStream out =  socket.getOutputStream();
		PrintWriter pw = new PrintWriter(out);
		pw.write(response);
		pw.flush();
		pw.close();
		out.close();
		reader.close();
		isr.close();
		is.close();
		socket.close();
		serverSocket.close();*/
	}
}

最后:编程客户端

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Clients {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket socket = new Socket("localhost", 8888);
		
		OutputStream out = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(out);
		
		pw.write("我是客户端2*********fsdfsdgds******");
		pw.flush();
		
		socket.shutdownOutput();
		
		InputStream is = socket.getInputStream();
		InputStreamReader  isr = new InputStreamReader(is);
		
		BufferedReader br = new BufferedReader(isr);
		String info =null;
		while((info=br.readLine())!=null){
			System.out.println(info);
		}
		
		br.close();
		isr.close();
		is.close();
		pw.close();
		out.close();
		socket.close();
	}
}



你可能感兴趣的:(Java基础知识,IO,线程,网络编程)