user_abh
user_abh

Reputation: 367

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. 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.

Upvotes: 0

Views: 1999

Answers (3)

user_abh
user_abh

Reputation: 367

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:

//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().

Upvotes: 0

user207421
user207421

Reputation: 311031

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. 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.

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.

Upvotes: 0

djna
djna

Reputation: 55947

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.

   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. Hence userInput2 never will receive a value.

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? 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.

Upvotes: 2

Related Questions