服务器看起来像这样(简化)
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWrite out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("42");
客户端看起来像这样(简化)
Socket clientSocket = new Socket("My public IP",port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data;
while(true){
if((data = in.readLine()) != null){
System.out.println(data);
break;
}
}
其他设置/操作:
这个问题成了答案,现在描述了通过网络空间发送生命,宇宙和一切的终极问题答案的设置。