问题描述
我正在尝试使用Java学习套接字,并且成功地将数据发送到了在我自己的机器上运行的ServerSocket.当我尝试使用readline从此套接字读取数据(以便我可以回显自己发送的消息)时,我的程序挂起,不会返回.
代码如下:
public static void main(String [] args)引发UnknownHostException,IOException {TCPClient cli =新的TCPClient("127.0.0.1","15000");尝试 {cli.ostream.writeUTF("Teste");字符串回显= cli.istream.readLine();//它挂在这行System.out.println(echo);}
TCPClient是我定义的类,因此我可以在使用homwework上的swing之前在更简单的接口上测试程序.这是代码:
公共类TCPClient {public DataOutputStream ostream = null;public BufferedReader istream = null;公共TCPClient(字符串主机,字符串端口)抛出UnknownHostException {InetAddress ip = InetAddress.getByName(host);尝试 {套接字套接字=新套接字(主机,Integer.parseInt(端口));ostream =新的DataOutputStream(socket.getOutputStream());istream = new BufferedReader(new InputStreamReader(socket.getInputStream()));} catch(IOException ex){Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE,null,ex);}}
我的服务器非常简单.建立连接后,它将进入此循环并停留在此处,直到我关闭客户端为止(由于无限循环).之后,一些异常处理会将其返回到连接开始之前的位置.
while(true){字符串msg = istream.readLine();System.out.println(已到达服务器:" + msg);//只在调试时有效ostream.writeUTF("ACK:" + msg);ostream.flush();}
我看不到我想念的东西.
PS:比较奇怪的是,如果我调试服务器,则可以看到到达那里的消息(例如,可以打印它),但是如果我只运行此代码,则不可能.这是否有我忽略的并发关系?
thx
问题是 readLine
尝试读取一行.在确定到达行尾之前,它不会返回该行.这意味着它期望换行符或通信结束.由于服务器不会发送任何换行符,也不会关闭流,因此客户端将无限期等待.
I'm trying to learn sockets using Java and I sucessfully sent data to a ServerSocket running in my own machine. When I try to read from this socket using readline (so I can just echo my own sent message) my program hangs and won't return.
Here's the code:
public static void main(String[] args) throws UnknownHostException, IOException {
TCPClient cli = new TCPClient("127.0.0.1", "15000");
try {
cli.ostream.writeUTF("Teste");
String echo = cli.istream.readLine(); //it hangs in this line
System.out.println(echo);
}
TCPClient is a class I defined so I can test my program on a simpler interface before using swing on my homwework. here's the code:
public class TCPClient {
public DataOutputStream ostream = null;
public BufferedReader istream = null;
public TCPClient(String host, String port) throws UnknownHostException {
InetAddress ip = InetAddress.getByName(host);
try {
Socket socket = new Socket(host, Integer.parseInt(port));
ostream = new DataOutputStream(socket.getOutputStream());
istream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
My server is pretty simple. After the connection is estabilished, it enters in this loop and stays here until I close the client (because of the infinite loop). Afterwards, some exception handling returns it to the point before the connection started.
while(true){
String msg = istream.readLine();
System.out.println("Arrived on server: " + msg); //just works on debug
ostream.writeUTF("ACK: " + msg);
ostream.flush();
}
I don't see what am I missing.
PS: the wierd stuff is that if I debug the server, I can see the message arriving there (I can print it, for example), but this isn't possible if I just run this code. Does this have some concurrency relation I'm overlooking?
thx
The problem is that readLine
tries reading a line. It will not return the line until it's sure that the end of line has been reached. This means that it expects either a newline character, or the end of the communication. Since the server doesn't send any newline char and doesn't close the stream, the client waits indefinitely.
这篇关于BufferedReader readLine方法挂起并阻止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!