写好点应该这样,不会只有一次,因为有个while(true),只要你不停,一直可以接受请求的。public class TCPServer {
ExecutorService threadPool = Executors.newCachedThreadPool();
public TCPServer(int port) {
init(port);
}
private void init(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
if (socket != null) {
threadPool.execute(new ProcessRunnable(socket));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TCPServer(8888);
}
class ProcessRunnable implements Runnable {
public ProcessRunnable(Socket socket) {
System.out.println(socket.getInetAddress().toString());
}
public void run() {
}
}
}