Socket+多线程

服务端:
实现Runnable接口:
public class MultiServerRunnable {
	private ServerSocket server;

	public void service() {
		try {
			server = new ServerSocket(2046);

			while (true) {
				Socket client = server.accept();

				Thread thread = new Thread(new Handler(client));
				thread.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new MultiServerRunnable().service();
	}
}

class Handler implements Runnable {
	private Socket socket;
	private BufferedReader is;
	private PrintWriter os;

	public Handler(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			os = new PrintWriter(socket.getOutputStream());

			String msg = is.readLine();

//			if (msg.equals("1")) {
//				try {
//					Thread.sleep(10000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}

			System.out.println("客户端说:" + msg);
			os.println("服务端收到消息:" + msg);
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				if (is != null) {
					is.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

继承Thread类:
public class MultiServerThread {
	private ServerSocket server;

	public void service() {
		try {
			server = new ServerSocket(2046);

			while (true) {
				Socket client = server.accept();

				Handler handler = new Handler(client);
				handler.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new MultiServerThread().service();
	}
}

class Handler extends Thread {
	private Socket socket;
	private BufferedReader is;
	private PrintWriter os;

	public Handler(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			os = new PrintWriter(socket.getOutputStream());

			String msg = is.readLine();

//			if (msg.equals("1")) {
//				try {
//					Thread.sleep(5000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}

			System.out.println("客户端说:" + msg);
			os.println("服务端收到消息:" + msg);
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				if (is != null) {
					is.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

客户端:
public class MultiClient {

	private static Runnable service(final int i) {
		return new Runnable() {
			private Socket socket = null;
			BufferedReader in = null;
			PrintWriter out = null;

			@Override
			public void run() {
				try {
					socket = new Socket("localhost", 2046);

					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					out = new PrintWriter(socket.getOutputStream());

					out.println(i);
					out.flush();
					System.out.println(in.readLine());

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		};
	}

	public static void main(String[] args) {
		Thread t1 = new Thread(service(1));
		Thread t2 = new Thread(service(2));
		t1.start();
		t2.start();
	}
}

你可能感兴趣的:(thread,多线程,socket,OS)