Tcp Socket:如果在客户端的 while 循环中使用,则 readine 会挂起:java
我一直在敲着头试图弄清楚出了什么问题。
我正在尝试用 java 编写一个简单的服务器客户端 TCP 套接字。服务器可以发送多行作为响应。
我在客户端使用 while((str = in.readLine())!= null) ,它在读取响应后挂起。请找到下面的代码。我发帖前已经搜索过。我确保以新行结束响应。
我尝试过 \n、\r\n 的组合,但 readLine 未检测到行尾。
但它在服务器端运行良好。
请帮忙。
谢谢。
服务器:
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
ServerSocket s = new ServerSocket(55555);
Socket socket = s.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine= null;
System.out.println("call processInput");
while ((inputLine = in.readLine()) != null) {
System.out.println("before call processInput");
out.print("200 Success \r\n");
out.flush();
System.out.println("after call processInput: ");
}
System.out.println("after while");
out.close();
in.close();
socket.close();
}
}
客户端:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
public class Test {
public static void main(String[] args) throws IOException {
try {
System.out.println(":Connect");
Socket s = new Socket("192.168.1.114",55555);
System.out.println("Socket:" + s);
System.out.println("after :Connect");
OutputStream s1out = s.getOutputStream();
PrintWriter output = new PrintWriter(s1out, true);
output.println("login user root");
output.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
System.out.println( in.readLine());
output.println("servershare");
output.flush();
System.out.println( "servershare");
String str = null;
while((str = in.readLine())!= null) // hangs here
{
System.out.println(str);
}
System.out.println( "share");
output.println("share file1.txt file1.html, roopa ramesh");
str = null;
while((str = in.readLine())!= null && !str.equals(""))
System.out.println(str);
in.close();
output.close();
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have been banging my head trying to figure out what is going wrong.
I am trying a simple server client TCP socket in java. Server can send multiple lines as response.
I am using while((str = in.readLine())!= null)
at client side and it hangs after reading the response. Please find the code below. I have searched before posting. I am making sure I am ending the response with new line.
I have tried combinations of \n, \r\n but readLine is not detecting the end of line.
But it works fine at server end.
Please help.
Thanks.
SERVER:
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
ServerSocket s = new ServerSocket(55555);
Socket socket = s.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine= null;
System.out.println("call processInput");
while ((inputLine = in.readLine()) != null) {
System.out.println("before call processInput");
out.print("200 Success \r\n");
out.flush();
System.out.println("after call processInput: ");
}
System.out.println("after while");
out.close();
in.close();
socket.close();
}
}
CLIENT:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
public class Test {
public static void main(String[] args) throws IOException {
try {
System.out.println(":Connect");
Socket s = new Socket("192.168.1.114",55555);
System.out.println("Socket:" + s);
System.out.println("after :Connect");
OutputStream s1out = s.getOutputStream();
PrintWriter output = new PrintWriter(s1out, true);
output.println("login user root");
output.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
System.out.println( in.readLine());
output.println("servershare");
output.flush();
System.out.println( "servershare");
String str = null;
while((str = in.readLine())!= null) // hangs here
{
System.out.println(str);
}
System.out.println( "share");
output.println("share file1.txt file1.html, roopa ramesh");
str = null;
while((str = in.readLine())!= null && !str.equals(""))
System.out.println(str);
in.close();
output.close();
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个死锁:
客户端和服务器都在等待另一端的下一行。您的协议不正确。
此外,您在服务器端和客户端使用平台默认编码来读取和写入消息。如果客户端和服务器没有相同的默认编码,就会遇到问题。您应该使用输入/输出流编写器包装流,并在构建它们时指定特定的编码。
You have a deadlock :
Both the client and the server are waiting for the next line from the other end. Your protocol isn't correct.
Moreover, you're using the platform default encoding at server-side and client-side to read and write the messages. If the client and server don't have the same default encoding, you'll have a problem. You should wrap your streams with Input/Output stream writers and specify a specific encoding when constructing them.