简体   繁体   English

C和java之间的套接字通信

[英]Socket communication between C and java

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. 我有一个客户端和一个服务器都运行在C.我的任务是介绍java程序,其中我创建一个服务器到C客户端和一个客户端到C服务器。 I am successful in trying to get the connections set up properly. 我成功地尝试正确设置连接。 However the problem is in communicating the data between both C programs. 但问题是在两个C程序之间传递数据。 Below is what I have written in my java program: 以下是我在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 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. 在调试上述内容时,可以看到执行停留在第二个while语句,这意味着输入流正在等待C客户端的输入。 I am using BufferedReader and BufferedWriter for the streams. 我正在为流使用BufferedReader和BufferedWriter。 The C client and server are using send and recv functions to communicate. C客户端和服务器正在使用send和recv函数进行通信。 Kindly help with any inputs to make the java program help both the C programs communicate with each other as they do without this. 请帮助任何输入,以使Java程序帮助C程序彼此通信,就像它们没有这样做。

Have you correctly considered the effect of Java's "short circuit" or operator? 您是否正确地考虑过Java的“短路”或运营商的影响?

With || 随着|| if the first clause is true the second is never evaluated. 如果第一个子句为真,则从不评估第二个子句。

   while(
        (userInput1=br1.readLine())!=null ||
        (userInput2=br2.readLine())!=null) {

So you successfully read 所以你成功阅读了

 userInput1=br1.readLine())!=null

and immediately enter your processing, then come back to while and read the next line into userInput1 again. 并立即进入您的处理,然后返回到while并再次读取下一行到userInput1。 Hence userInput2 never will receive a value. 因此userInput2永远不会收到值。

You need separate logic like 你需要单独的逻辑

    read first line
    read second line 

But exactly what should you do when reading line2 and a the data is not ready? 但是,在读取第2行并且数据尚未就绪时,您应该怎么做? Try again? 再试一次? Is the line you read next the expected line2 or a new line1? 您读取的行是下一个预期的line2还是新的line1? This is quite tricky to get right. 为了做到这一点,这非常棘手。

I would prefer not to rely on two separate readlines in my protocol. 我宁愿不依赖我的协议中的两个单独的readlines。

while((userInput1=br1.readLine())!=null||(userInput2=br2.readLine())!=null){

This condition means that you are going to read br1 all the way to EOS before you ever read anything from br2. 这种情况意味着在从br2读取任何内容之前,您将一直读取br1到EOS。 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 with br2 hasn't sent anything, or at least hasn't sent a line terminated by a newline. 相反,如果你被困在br2.readLine()它意味着两件事:(a) br1在EOS,和(b)与br2相关联的对等体没有发送任何东西,或者至少没有发送一条线路终止通过换行符。

Are you perhaps suffering from the common delusion that readLine() returns null when there is no data ready to be read? 当没有数据准备好被读取时,你是否正在遭受readLine()返回null的常见错觉?

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. 此外,您正在读取由换行符终止的行,这些换行符由readLine()调用删除,然后在没有任何换行符的情况下将其写出,这几乎不正确。

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. 在我看来,你真正写的是代理,在这种情况下你需要每个插槽两个线程,一个从A读取并写入B,另一个从B读取并写入A.如果它是代理你应该使用InputStreams和OutputStreams而不是Readers和Writers,因为您可能没有理由检查数据,因此您不应该通过使用Readers和Writers隐含的byte-> char和char->字节转换过程。 There are further subtleties when writing proxies but I'll wait for your confirmation before elucidating them. 在编写代理时还有一些细微之处,但在阐明代理之前我会等待你的确认。

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). 否则只使用read()会使程序永远停止输入(即使在实际发送了所有数据之后)。 Am using the ready() in the following way: 我用以下方式使用ready():

//The proxy client
while(true){
    if(br1.ready()){
        while((temp1=br1.read())!=(int)par)
            userInput1=userInput1+(char)temp1;
        System.out.println("Input to Actual Server: " + userInput1);
        bw1.write(userInput1);
        bw1.flush();
        System.out.flush();
        userInput1="";
        temp1=0;
        }
        if(br2.ready()){
            while((temp2=br2.read())!=(int)par)
                userInput2=userInput2+(char)temp2;
            System.out.println("Response from Actual Server: " + userInput2);
            userInput2=userInput2+par;
            bw2.write(userInput2);
            bw2.flush();
            System.out.flush();
            userInput2="";
            temp2=0;
        }
}

//The proxy server
while(true){
     if(br1.ready()){
         while((temp1=br1.read())!=(int)par)
                         userInput1=userInput1+(char)temp1;
         System.out.println("Input from Actual Client: " + userInput1);
         userInput1=userInput1+par;
         bw1.write(userInput1);
         bw1.flush();
         System.out.flush();
         userInput1="";
         temp1=0;
     }
     if(br2.ready()){
         while((temp2=br2.read())!=(int)par)
                userInput2=userInput2+(char)temp2;
         System.out.println("Response to Actual Client: " + userInput2);
         bw2.write(userInput2);
         bw2.flush();
         System.out.flush();
         userInput2="";
         temp2=0;
     }
}

Kindly suggest if there is any problem of using ready(). 如果使用ready()有任何问题,请建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM