我创建了一个应用程序,它与给定端口建立连接并以两种方式传输数据。但我在从服务器读取数据时遇到问题。
try{
Socket skt = new Socket(127.98.68.11, 1111); // connecting to this to get data
String message = "some test message";
if(option.equalsIgnoreCase("send")){
OutputStream outToServer = skt.getOutputStream();
outToServer.write(message); // this is working, message stored on server-side
}else if(option.equalsIgnoreCase("receive")){
BufferedReader in = new BufferedReader (new InputStreamReader(sit.getInputStream()));
String fromServer = in.readLine();
System.Out.Println(fromServer);
}
}catch(IOException io){
io.printStackTrace();
}
在此程序中,一切都按预期进行。除了 in.readline()。
我尝试在 Debug模式下运行该程序,当编译器到达该命令时。什么也没做,我也看不到光标
最佳答案
这可能是因为您正在尝试执行in.readLine()
,这要求服务器终止使用换行符发送到客户端的“接收”命令..“\n"或 "\r\n"沿
关于java - TCP 连接 Socket.getInputStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6932525/