Newbie sockets question/problem

This is a discussion on Newbie sockets question/problem within the Networking/Device Communication forums, part of the General Programming Boards category; Im having trouble understanding what/why the following is happening The server sends the following 6 bytes data to the client. ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Newbie sockets question/problem

    Im having trouble understanding what/why the following is happening

    The server sends the following 6 bytes data to the client.

    Send - Server
    Code:
    //Send - Server
    		
    int RetVal;
    char pd[6]; //Packet Data
    
    pd[0] = 'A';
    pd[1] = 'B';
    pd[2] = 'C';
    pd[3] = 'D';
    pd[4] = 'E';
    pd[5] = 'F';
    
    RetVal = send(remoteConnection->clientsocketfd,pd,6,NULL);
    The client reads only 3 bytes of the available 6 bytes sent.

    Recv - Client
    Code:
    //Recv - Client
    		
    char rd[8]; //Received Data
    
    recv(remoteConnection->socketfd,(char*) rd,3,NULL);
    
    printf("\n0 Byte = %c",rd[0]);
    printf("\n1 Byte = %c",rd[1]);
    printf("\n2 Byte = %c",rd[2]);
    printf("\n3 Byte = %c",rd[3]);
    printf("\n4 Byte = %c",rd[4]);
    printf("\n5 Byte = %c",rd[5]);
    printf("\n6 Byte = %c",rd[6]);
    printf("\n7 Byte = %c",rd[7]);
    Output
    Code:
    //Output 
    		
    0 Byte = D
    1 Byte = E
    2 Byte = F
    3 Byte = ¶ //Null
    4 Byte = ¶ //Null
    5 Byte = ¶ //Null
    6 Byte = ¶ //Null
    7 Byte = ¶ //Null
    The problem is when the received data is read its the last 3 bytes of data.
    I was expecting it to be the first 3 bytes to be 'A','B' and 'C' of the data that was sent
    not the last 3 which is 'D','E' and 'F'.

    What Iím trying to do is as described in Beej's Guide to Network Programming Chapter 6.5. Son of Data Encapsulation

    Where the length of a packet is put as a header as a first byte. The receiver receives the first few bytes then knows from the header how much more bytes he needs to read to get the full packet.

    But by receiving the last few bytes instead of the first few Im having trouble to do this because the packet length (which is the first byte) isnít read.

    By the way im using WinSocks

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Is it possible that you are calling recv() twice? The first call gets the first 3 bytes, and the second call gets the next 3 which is what you are printing out.

    Usually, when you are receiving data off a socket, you should try and fill the receive buffer completely to minimize the number of trips your code makes down to the network stack. Something more like:

    Code:
    char rd[1024]; //Received Data
    
    int bytesReceived = recv(remoteConnection->socketfd,(char*) rd,sizeof(rd),NULL);
    Use the value returned by recv() to know how many bytes you've received, and then parse through the buffer for all the data you need.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Actually... Reading it twice would explain allot....

    Ill check it out tommorow

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Yeap I was receiving multiple times. All is fine now.

    So what youíre saying is try to receive as much data as the buffer allows and to make a big buffer?

    How much data should I be send()íing at one time? Im trying to make a client server file transfer protocol.

    It would go like.

    Code:
    For(;;;){
        Server sends packet X
        Client verifies receiving packet X 
    }
    But how many bytes should X be?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends somewhat on the link, but I would suspect that the difference in overhead between sending 1000 bytes and 5000 bytes is negligable. Sending really small packets [e.g. 50 bytes] is of course not at all productive. Sending HUGE packets over a slow/unreliable networks is definitely bad. Sending large packets over Ethernet will probably lead to "fragmentation", meaning that the packet is split into smaller portions before it's actually going out on the 'net. Traditional Ethernet uses a total packet size of 1536 bytes, but the actual size of the content is shorter than that, because of the header.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Excellent thanks!

    I was planning on resizing the packet size dynamically depending on what type of through put the transfer is getting.

    Say the transfer is going at 52k/s pack size can be 1024 bytes but if itís only going at 2k/s it will be 40 bytes.

    Is this a good idea?

    Also I was thinking after 10 or so packets the client should let the server know itís received all the data.

    Or should I not worry about that because TCP makes sure that the receiving socket receives all the bytes that have been sent?

    Am I right in thinking TCP takes care of the bit level of making sure data is sent but I should keep track at the packet level?

    One last question. What happens if the server is on a fast connection and sends out say 5MB in a couple of seconds all at once but the client is on a really slow connection downloading at 2K/s. Where does the data sit? Does it just jam the pipe of the client? Or does TCP use some kind of smarts to not let the 5MB of data leave the server until the client receives some then sends it bit by bit?

    Sorry for all the questions. I should really get a book for Christmas.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    TCP will guarantee delivery for you. You will need to send some kind of marker to let the receiver know when the transfer is complete though. This can be done in a few different ways. The easiest way is to send the file size before you start sending the data so the client knows when the transfer is done. This also allows the client to display a progress bar of some kind to the user.

    What happens if the server is on a fast connection and sends out say 5MB in a couple of seconds all at once but the client is on a really slow connection downloading at 2K/s. Where does the data sit?
    You don't need to worry about this. Just make sure you read the return values from the send() command. Just because you try to send() 3000 bytes doesn't mean that 3000 actually gets sent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 02:43 PM
  2. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 01:20 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 05:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21