java.net 套接字、readline 和行终止符(VNC 连接)
我正在使用 x11vnc,我想为其开发简单的客户端。 所以,工作是:
- 从套接字接收信息
- 发送信息
- 接收下一个数据
- 发送另一个信息
等等。
因此,例如,我正在使用下一个简单的代码: <代码>
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(socket.getInputStream()));
String test = null;
try {
test = lnr.readLine();
System.out.println(test);
test = lnr.readLine();
System.out.println(test);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
The first message from server I got without errors (first readLine). The next message is empty (or without any line terminator for readLine) and I have infinite loop. lnr.ready() not works for me.那么,当来自套接字的数据有时没有“\n”、“\r”、“\r\n”或者套接字缓冲区为空时,如何从套接字读取数据?
I am using x11vnc and I want to develop simple client for it.
So, the work is:
- recieve info from socket
- send info
- recieve next data
- send another info
and etc.
So, for example, I am using the next simple code:
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(socket.getInputStream())); String test = null;try { test = lnr.readLine(); System.out.println(test); test = lnr.readLine(); System.out.println(test); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } System.exit(0);
The first message from server I got without errors (first readLine). The next message is empty (or without any line terminator for readLine) and I have infinite loop.
lnr.ready() not works for me.So, how can I read from socket when data from socket sometimes have no "\n", "\r", "\r\n" or if the socket buffer is empty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一以换行符终止的 RFB/VNC 消息是初始版本交换。事实上,RFB/VNC 协议有些缺陷,因为它没有任何独特的消息帧;你必须处理每条消息,否则你将失去你的位置。您可能需要直接从套接字或 InputStreamReader 读取。用 LineNumberReader 包装它是行不通的。
我在 RFB/VNC 协议上找到的最佳参考: http://tigervnc.org/cgi-bin/ rfbproto
The only RFB/VNC message that is terminated by a newline is the initial version exchange. In fact the RFB/VNC protocol is somewhat deficient in that it does not have any unique message framing; you have to process every message or you will lose your place. You probably will need to read directly from the socket or from the InputStreamReader. Wrapping it with LineNumberReader won't work.
The best reference I have found on the RFB/VNC protocol: http://tigervnc.org/cgi-bin/rfbproto