且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++服务器和Java客户端聊天

更新时间:2022-04-20 23:10:04

read() 函数到达文件末尾时将返回0.

The read() function will return 0 when it reaches the end of file.

因此,在您的情况下,有一个循环迭代接收了"Hello world",然后又有一个迭代,除了read()返回0外,将不接收任何内容.因此,"Client:"显示为空字符串.

So in your case, there is a first loop iteration that receives "Hello world", then there's one more iteration, that will receive nothing but read() returns 0. So "Client :" is displayed with an empty string.

此外,请注意:

  • 使用基于套接字的通信,不能保证服务器端的一个read()将对应于客户端端的一个println().因此,对于较长的邮件,您将冒着分批接收的风险.然后,在输出中,您会在消息文本中看到几个客户端".
  • 该行分隔符已发送 Java端不一定与服务器端预期的行分隔符相对应,如果您跨平台工作.
  • with socket based communication, it is not guaranteed that one read() on the server side will correspond to one println() on the client side. So for longer messages, you'll risk to receive it in pieces. On the output you'd then see several "Client" within the message text.
  • the line separator sent on the java side might not necessarily correspond to the line separator expected on the server side, expect if you're working cross-platform.