我有一个开放的社交容器,我需要在该容器中放置一个小程序,该小程序需要充当tcpServer,我需要一些指导,我该如何做?
发布于 2013-02-07 16:05:31
networking tutorial详细介绍了如何创建TCP服务器套接字并接受来自该套接字的连接。
这里是它的要点:
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
// now get the input and output streams from the client socket
// and use them to read and write to the TCP connection
clientSocket.close();
serverSocket.close();https://stackoverflow.com/questions/14745318
复制相似问题