C与java之间的socket通信
我有一个客户端和一个服务器都在 C 中运行。我的任务是引入 java 程序,在该程序中我创建一个到 C 客户端的服务器和一个到 C 服务器的客户端。我成功地尝试正确设置连接。然而问题在于两个 C 程序之间的数据通信。下面是我在 java 程序中编写的内容:
while(true){
while((userInput1=br1.readLine())!=null||(userInput2=br2.readLine())!=null){
if(userInput1=!null){
bw1.write(userInput1);
bw1.flush();
}
if(userInput2=!null){
bw2.write(userInput2);
bw2.flush();
}
}
在调试上述内容时,可以看到执行卡在第二个 while 语句处,这意味着输入流永远等待 C 客户端的输入。我正在使用 BufferedReader 和 BufferedWriter 作为流。 C 客户端和服务器使用 send 和 receive 函数进行通信。 请提供任何输入帮助,以使 java 程序帮助两个 C 程序相互通信,就像没有它一样。
I have a client and a server both running in C. My task is to introduce java program in which I create a server to the C client and a client to the C server. I am successful in trying to get the connections set up properly. However the problem is in communicating the data between both C programs. Below is what I have written in my java program:
while(true){
while((userInput1=br1.readLine())!=null||(userInput2=br2.readLine())!=null){
if(userInput1=!null){
bw1.write(userInput1);
bw1.flush();
}
if(userInput2=!null){
bw2.write(userInput2);
bw2.flush();
}
}
While debugging the above, it is seen that the execution is stuck at the second while statement meaning that the input stream is waiting for the input for the C client for ever. I am using BufferedReader and BufferedWriter for the streams. The C client and server are using send and recv functions to communicate.
Kindly help with any inputs to make the java program help both the C programs communicate with each other as they do without this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是否正确地考虑过Java的“短路”或运算符的作用?
与 ||如果第一个子句为真,则永远不会评估第二个子句。
因此,您成功读取
并立即进入处理,然后返回 while 并再次将下一行读取到 userInput1 中。因此 userInput2 永远不会收到值。
您需要单独的逻辑,例如
但在读取 line2 且数据尚未准备好时您应该做什么?再试一次?您接下来读到的行是预期的第 2 行还是新的第 1 行?要做到这一点是相当棘手的。
我不想在我的协议中依赖两个单独的阅读行。
Have you correctly considered the effect of Java's "short circuit" or operator?
With || if the first clause is true the second is never evaluated.
So you successfully read
and immediately enter your processing, then come back to while and read the next line into userInput1 again. Hence userInput2 never will receive a value.
You need separate logic like
But exactly what should you do when reading line2 and a the data is not ready? Try again? Is the line you read next the expected line2 or a new line1? This is quite tricky to get right.
I would prefer not to rely on two separate readlines in my protocol.
这种情况意味着,在从 br2 读取任何内容之前,您将一直读取 br1 到 EOS。这真的是你的意图吗?
相反,如果您卡在
br2.readLine()
处,则意味着两件事:(a)br1
位于 EOS,(b) 与关联的对等方br2
还没有发送任何内容,或者至少没有发送以换行符结尾的行。您是否可能有一种常见的错觉:当没有数据可供读取时,readLine() 返回 null?
此外,您正在读取由换行符终止的行,这些行由 readLine() 调用删除,然后在没有任何换行符的情况下将它们写出,这几乎是不正确的。
在我看来,你真正写的是一个代理,在这种情况下,每个套接字需要两个线程,一个从 A 读取并写入 B,另一个从 B 读取并写入 A。如果它是代理,你应该使用 InputStreams 和 OutputStreams 而不是 Readers 和 Writers,因为您可能没有理由检查数据,因此您不应该将其放入使用 Readers 和隐含的 byte->char 和 char->byte 转换过程中作家们。编写代理时还有更多微妙之处,但我会等待您的确认后再进行解释。
This condition means that you are going to read br1 all the way to EOS before you ever read anything from br2. Is that what you really intended?
Conversely, if you are stuck at
br2.readLine()
it means two things: (a)br1
is at EOS, and (b) the peer associated withbr2
hasn't sent anything, or at least hasn't sent a line terminated by a newline.Are you perhaps suffering from the common delusion that readLine() returns null when there is no data ready to be read?
Also you are reading lines terminated by newlines, which are removed by the readLine() call, and then writing them out without any newlines, which can hardly be correct.
It appears to me that what you are really writing is a proxy, in which case you need two threads per socket, one reading from A and writing to B, and the other reading from B and writing to A. And if it's a proxy you should use InputStreams and OutputStreams rather than Readers and Writers, as you probably have no reason to inspect the data, and you therefore shouldn't put it through the byte->char and char->byte conversion processes implied by using Readers and Writers. There are further subtleties when writing proxies but I'll wait for your confirmation before elucidating them.
我使用奇偶校验字符的原因是解释流的结尾。否则,仅使用 read() 会使程序永远停止输入(即使在实际发送了所有数据之后)。我按以下方式使用ready():
如果使用ready()有任何问题,请提出建议。
the reason I am using the parity character is to interpret the end of the stream. Otherwise using using just the read() is making the program halt for the input forever (even after the actual had sent all its data). Am using the ready() in the following way:
Kindly suggest if there is any problem of using ready().