发现了一些类似的问题,但没有答案,所以这里 -
附加代码是自包含,可编译和可运行的,可以在我的计算机上设置服务器。 Socket被创建,我可以从我的本地计算机上执行telnet <ip_address> 9162
或telnet localhost 9162
。从远程主机,telnet无法连接。有任何想法吗? TIA。
import java.io.*;
import java.net.*;
public class TestServerSocket {
public static void main(String args[]) throws IOException {
final int portNumber = 9162;
System.out.println("Creating server socket on port " + portNumber);
ServerSocket serverSocket = new ServerSocket(portNumber);
while (true) {
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
while (true) {
pw.print("What's you name? ");
pw.flush();
String str = br.readLine();
if (str.trim().length() == 0)
break;
pw.println("Hello, " + str);
}
pw.close();
socket.close();
}
}
}