Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all! I am a rookie programmer in Linux C. I need a way to send data to multiple Servers from a single client, at one time. It is not ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5

    Unhappy TCP client server Application

    Hi all!

    I am a rookie programmer in Linux C. I need a way to send data to multiple Servers from a single client, at one time.

    It is not the same as broadcasting, I need to send lists to 4 servers from a single client.

    I have come across multiple clients- single server, but am at a loss as how to do the above.

  2. #2
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5
    Also, I have been trying to send the list from one client to one server. The problem is, I am able to send the list using write() function, but the server just closes the connection thereafter....

    If anyone could help me debug my program, I would appreciate it.

    while(1)
    {

    socket_to_sender=accept(receiver_socket,0,0);
    if(socket_to_sender<0)
    {
    perror("UNABLE TO ACCEPT MESSAGE\n");
    exit(1);
    }
    printf("AFTER ACCEPT: CONNECTION IS ESTABLISHED\n");
    msglength=read(socket_to_sender, (char *)ranlist,790*;
    if(msglength<0)
    {
    perror("UNABLE TO READ\n");
    exit(1);
    }

    else printf("CLIENT REQUEST RECEIVED:\n"); The server runs till this point, but just hangs thereafter, and client gives the error, connection closed by peer.

    for(l=0;l<789;l++) //Calculate the sum locally
    for(m=l+1;m<790;m++)
    sum+=(64-hammingdistance(ranlist[l],ranlist[m]))*(64-hammingdistance(ranlist[l],ranlist[m]));

    printf("The sum is: %ld", sum);
    sleep(10);

    /* if(write(socket_to_sender,"pong\0",5)<0)
    {
    perror("UNABLE TO WRITE DATA\n");
    exit(1);
    }
    */
    if(close(socket_to_sender)<0)
    {
    perror("UNABLE TO CLOSE THE CONNECTION\n");
    exit(1);
    }

    }

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Well, the first thing you have to do is make your code readable by placing it inside CODE tags. To do that, highlight your code and click the octothorpe (#) icon just above the editing window. See how more readable this is? See how it brought your 790*8) back to life instead of having that silly smiley obliterate the 8?
    Code:
            while(1)
            {
    
                    socket_to_sender=accept(receiver_socket,0,0);
                    if(socket_to_sender<0)
                    {
                            perror("UNABLE TO ACCEPT MESSAGE\n");
                            exit(1);
                    }
                    printf("AFTER ACCEPT: CONNECTION IS ESTABLISHED\n");
                    msglength=read(socket_to_sender, (char *)ranlist,790*8);
                    if(msglength<0)
                    {
                            perror("UNABLE TO READ\n");
                            exit(1);
                    }
    
                    else    printf("CLIENT REQUEST RECEIVED:\n");  The server runs till this point, but just hangs thereafter, and client gives the error, connection closed by peer.
    
                             for(l=0;l<789;l++)   //Calculate the sum locally
                                  for(m=l+1;m<790;m++)
                                          sum+=(64-hammingdistance(ranlist[l],ranlist[m]))*(64-hammingdistance(ranlist[l],ranlist[m]));
    
                            printf("The sum is: %ld", sum);
                            sleep(10);
    
                    /*      if(write(socket_to_sender,"pong\0",5)<0)
                            {
                                    perror("UNABLE TO WRITE DATA\n");
                                    exit(1);
                            }
                    */
                    if(close(socket_to_sender)<0)
                    {
                            perror("UNABLE TO CLOSE THE CONNECTION\n");
                            exit(1);
                    }
    
            }
    Now. Does the server get to just before the CLIENT REQUEST RECEIVED, or after?

    If after, maybe your server gets hung up in the doubly nested loop.

    I'm not worried about the 300,000 or so times that inner line will be called. But how complicated is function hammingdistance()? And are you sure that ranlist is really 6320 bytes long?

    And have you looked at msglength? What if you only got 80 bytes back instead of all 6320?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5

    Re

    Well, the hamming distance function is standard function that I picked up from wikipedia,.

    Code:
    unsigned hamdist(unsigned x, unsigned y)
    {
      unsigned dist = 0, val = x ^ y;
     
      while(val)
      {
        ++dist; 
        val &= val - 1;
      }
     
      return dist;
    }
    I removed the /**/ around the last write statement, and found out that:

    1. the server prints CLIENT REQUEST RECEIVED and hangs,... I printed the msglenght in the same line, and got all 6320 bytes.

    As for the double loop, it works fine on the client side.

    Also, my server receives the list, sends some response back to the client, and hangs...

    My client side does this for receiving response:
    Code:
            if(strcmp(buf,"pong"))
                    printf("REPLY MESSAGE FROM THE SERVER:%s\n",buf);
           else
           printf("INVALID RESPONSE FROM SERVER\n");
    
            if(close(sender_socket)<0)
            {
                    perror("CANNOT CLOSE THE CONNECTION");
                    exit(0);
            }
            printf("CLIENT:EXIT\n");
            exit(0);
    The client prints invalid response and exits, while the server hangs.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You should never assume that you've received all the bytes you intend to on a TCP read(); you should check the exact result of the read(), both on the server side and the client side, every single time.

    Same for write().

    If you're not getting "pong\0" on the client side, it would be a useful clue to see just what you do get. (And, since you're following the above advice, you'll already be checking to see that the result of the read() call is 5.)

    It would be worthwhile to check just where your server is hanging up. If it's hanging up on the close(), you may wish to scroogle the following search terms, just for kicks and grins:
    Code:
    tcp shutdown close
    Sorry I can't be more help at this time. :(
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5
    Yes, the read value is 5, and the message received is pong.

    But, now I am not receiving all the 6320 bytes. I dont know why, but the number is 1448.

    I still cannot figure out why the server hangs indefinitely.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    now I am not receiving all the 6320 bytes.
    There is never any assurance that you'll get as many bytes as you want before the read() returns. If you don't get all of the bytes the first time around, you have to loop back and read() some more, until you get all the bytes you want, or until you get 0 (meaning the connection has been dropped at the other end before sending you enough bytes) or until you get -1 (meaning error).

    This should be done on every read(), both on the client and the server side, even if you only expect five bytes.

    This should also be done on every write(), both on the client and the server side. Continue to check for 0 or -1 returned for any single write().

    Failure to do this for every network read() and write() is a bug (assuming you aren't doing non-blocking I/O; if you are, it's more complicated).
    I still cannot figure out why the server hangs indefinitely.
    Once you've fixed all known bugs (see above), the next step is to ask yourself where the server hangs indefinitely. On what statement in the source code?
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...