Java Socket长连接

这段时间在写PC间通信的一些代码,用的是Java Socket,建长连接的时候总是出现socket中断连接,后来总结了下,发现一定要在服务器端/客户端实现接收和发送两个线程才可避免这个问题。

Server.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
 
 
public class Server {
	private ServerSocket serverSocket;
	private Socket socket;
	private int port = 5055;
 
	private void start() throws Exception {
		serverSocket = new ServerSocket(port);
		while (true) {
			socket = serverSocket.accept();
			new Thread(new SocketThread(socket)).start();
		}
	}
 
	/**
	 * 处理socket连接s
	 */
	class SocketThread implements Runnable {
		private Socket socket;
 
		public SocketThread(Socket socket) {
			this.socket = socket;
		}
 
		public void run() {
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				Writer writer = new OutputStreamWriter(socket.getOutputStream());
				String content;
				while ((content = reader.readLine()) != null) {
					System.out.println("receive:" + content);
					writer.write("this is server \r\n");
					writer.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (socket != null) {
					if (!socket.isClosed()) {
						try {
							socket.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
 
			}
		}
 
	}
 
	public static void main(String[] args) {
		Server s = new Server();
		try {
			s.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
}
3 weeks ago, this page was being read.


Subscribe to Comments