Thread: read returns junk

  1. #1
    Registered User
    Join Date
    Aug 2010

    read returns junk

    I ve written a simple client server,where a server waits on select for handling multiple client.
    For load purposes I have connected only a single client and sending 200o msgs continuously.
    I have noticed that after some time server reads junk data from the socket.
    Its is only when i apply usleep after write in client,server is able to read correct data.
    I ve tried using option like TCP_NODELAY and fysnc on socket but it does not help.

    I am attaching client and server code files.
    I am stuck in this problem from last 2 days.
    Please help


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Several things to mention for the server (I didn't look at the client)

    1. struct client_data clients[10];
    This is shared between two threads, but is NOT guarded by a mutex. In particular, you make it valid before assigning other fields.

    You could easily avoid all this threading headache by adding listen_fd to the descriptor set monitored by select.

    2. ret = select(0xffff, &fds, NULL,NULL,&timeout);
    Read the manual page for select, and pass a much better parameter than 0xffff
    This will hurt you, checking 1000's of descriptors you have no interest in.

    3. memset(&sock_data,2,sizeof(sock_data));
    Why 2?
    Or more to the point, why not zero?
    Or even "why bother", since you're about to overwrite it anyway.

    4. ret = read(clients[i].fd,&sock_data,sizeof(sock_data));
    You check for <= 0, but what you DON'T check for is ret == sizeof(sock_data)

    For a stream connection, message fragmentation can occur on both the sender and receiver side.
    You try to send

    It is wrong to assume that you will get exactly TWO corresponding read calls at the other end. You might only get "Hel" on the first call. It isn't a failure, but it isn't entirely success either. You need to take this into account and call read again for the remaining bytes you're expecting.

    Likewise, on the send side, sending "Hello" might only manage to send "He" on the first call. You would need to detect this and call send again with the remains of the message ("llo" in this case), possibly repeatedly until the whole message is sent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM
  4. Doing a read while in an intr handler
    By VirtualAce in forum A Brief History of
    Replies: 2
    Last Post: 01-14-2002, 04:20 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM