使用Java从客户端套接字读取数据(Read data from a client socket in Java)
我编写了从客户端套接字发送/接收数据的代码。 发送数据步骤已成功完成,但是当我想从套接字读取数据时, readLine()方法阻止程序,而没有要读取的数据。
这是我的代码:
StringBuffer document = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + "\n");
reader.close()感谢所有我可以读取所有收到的数据,但readLine或read(byte [],int,int)方法在没有数据要读取时阻塞程序,而此方法必须在此时返回null / -1。
I have written the code for sending/receiving data from a client socket. The sending data step has been done successfully, but when I want to read the data from a socket, the readLine() method block program while there isn't data to be read.
This is my code:
StringBuffer document = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + "\n");
reader.close()
thanks all I can read all received data, but readLine or read(byte[], int, int) methods block program when there is no data to read, while this method must return null/-1 in this time.那是因为readLine()函数是一个阻塞调用,所以当然它会阻塞。
为了更具建设性,对readLine()类的方法的调用应该在一个单独的线程中,这样阻塞调用不会影响代码的其余部分。 从正在阅读的类中,我建议创建一个纯粹用于控制从套接字读取的线程。
我将传递对创建类的引用,以便如果线程接收信息,父类可以使用它。
That's because the readLine() function is a blocking call, so of course it's going to block.
To be more constructive, calls to methods like readLine() should be in a separate thread so that the blocking call does not affect the rest of your code. From the class which is reading, I would recommend creating a thread purely to control reading from the socket.
I would pass a reference to the creating class so that if the thread receives information, the parent class can use it.
2012-07-11
相关问答
in.readLine()在看到行终止符之前不会返回。 除非PHP中的socket_write隐式添加行终止符,否则您需要自己执行此操作,以便Java端看到您编写了完整的文本行。 in.readLine() won't return until it sees a line terminator. Unless socket_write in PHP implicitly adds a line terminator, you'll need to do so yourself, so that ...
您关闭了套接字然后继续使用它。 try-with-resource语句关闭了套接字; 你开始的两个线程继续使用它。 You closed the socket and then continued to use it. The try-with-resource statement closed the socket; the two threads you started continued to use it.
你正在读行,但你不是在写行。 您需要为每个已发送的消息添加行终止符。 You're reading lines but you aren't writing lines. You need to add a line terminator to each sent message.
您应该使用OutputStream来编写二进制数据,而不是PrintWriter 。 You should be using an OutputStream to write binary data, not a PrintWriter.
尝试在邮件末尾添加\n 。 也不要将'NoDelay`设置为false。 你正在进行异步发送。 Try adding a \n to the end of the message. Also do not set 'NoDelay` to false. You are doing an asynchronous send.
你的脚本运行正常,并且对你正在寻找的东西没有很大的缺陷。 由于从命令行而不是Web浏览器运行脚本时获得了所需的结果,因此我们可以查明问题的原因:HTTP协议 HTTP不是用于持续连接(就像你习惯使用Java套接字一样),但它的简化工作流程基于Request / Elaborate / Response / Forget。 因此,您无法与纯HTML / PHP over HTTP解决方案进行“实时聊天”。 唉,并非所有的希望都失去了! 要实现“实时”通信,您可以使用Ajax,这不是很难习惯。 我说“...
我对客户端和服务器通信方式之间的差异非常好奇。 例如,客户端使用Scanner读取输入,而服务器使用BufferedReader (这是我个人的偏好)。 只是一个建议:保持一致。 现在 - 首先,客户端只发送一条消息,但随后开始在无限循环中无限期地读取。 看到你确切地知道服务器在向它发送“HELO”之后应该如何响应(它应该用一行响应,“DERP”),没有理由在任何类型的循环中从服务器读取。 服务器上存在同样的问题。 由于客户端现在是,它总是只向服务器发送一行(“HELO”)。 因此,服务器应该只期...
我们需要在这里绑定吗? 或者仅仅是因为我们想要引用本地端口? 您不需要绑定客户端SocketChannel。 s.connect(someAddr)
如果返回true,则javadocs表示已建立连接。 这是否意味着我不需要调用finishConnect()? 正确。 从我读到的,这是本地连接,但它没有指定远程连接是否可能立即返回true。 它可以随时返回true,你必须检查。 这是客户端向服务器发送SYN的位置吗? 是。 服务器通过一些serverSocketChannel.accept()获...
那是因为readLine()函数是一个阻塞调用,所以当然它会阻塞。 为了更具建设性,对readLine()类的方法的调用应该在一个单独的线程中,这样阻塞调用不会影响代码的其余部分。 从正在阅读的类中,我建议创建一个纯粹用于控制从套接字读取的线程。 我将传递对创建类的引用,以便如果线程接收信息,父类可以使用它。 That's because the readLine() function is a blocking call, so of course it's going to block. To...
















