Thread: Socket Send Help

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Socket Send Help

    Hi All,

    I read that the return value of the send() function is the amount of bytes of the message that was sent. Reading a tutorial I found on the internet there was this comment on send()

    Remember, if the value returned by send() doesn't match the value in len (size of the message you sent), it's up to you to send the rest of the string.
    Well i cant find any information on the internet about sending the rest of the data, even the examples on msdn just use the send() function without checking that the bytes sent is equal to the length of the message.

    How do i send the rest of a message if send() only sends part of it?

    Thanks

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You just send the rest of the data - this really isn't documentable. By calling send() again, except with the rest of the data instead of the complete set that you passed before. Example, if you call:
    Code:
    send(some_socket, "abcdef", 6, 0);
    ..and that returns three, your next call to send should be:
    Code:
    send(some_socket, "def", 3, 0);
    Usually you use the return value from a previous send to advance some pointer, pop data out of a buffer, etc.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    int send_all(int socket, char *data, int len)
    {
        int nbytes;
    
        while(len)
        {
            nbytes = send(socket, data, len, 0);
            if(nbytes < 0 && errno != EINTR)
            {
                /* There was an error */
                return -1;
            }
            data += nbytes;
            len -= nbytes;
        }
        return 0;
    }
    The part in red is UNIX-specific.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to send a struct over a socket
    By Anddos in forum C++ Programming
    Replies: 11
    Last Post: 06-27-2009, 02:56 PM
  2. sending n no of char data uning send() function,
    By thebrighter in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2007, 12:26 PM
  3. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  4. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:49 AM
  5. CString conversion for socket send()
    By WaterNut in forum C++ Programming
    Replies: 13
    Last Post: 04-27-2005, 04:55 PM