Using recv

This is a discussion on Using recv within the C Programming forums, part of the General Programming Boards category; I'm learning some socket programming in C and I've noticed something interesting with recv. If on my client code I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Using recv

    I'm learning some socket programming in C and I've noticed something interesting with recv. If on my client code I send a message that's really large, say 2048 bytes, the first time I send it, it only receives 1024 bytes like I would expect. Then, the second time I send the 2048 byte request, it receives 1024 bytes but strlen(input) is output as being 1026 bytes. Where do these extra two bytes come from?

    Code:
    while(1)
    {
    		int acceptsock = accept(sock, NULL, NULL);
    		char* input = (char*) malloc(1024*sizeof (char));
    		printf("receive call got %d\n",recv(acceptsock, input, 1024, 0));
    		printf("Server recieved: %s\n",input);
    		printf("Size of received message: %d\n",strlen(input));
    		//do stuff with input
    		free(input);
                   //more code
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If I had to guess, newline and carriage return.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Why? The same thing is being done twice, shouldn't the results be the same? Where does a newline and carriage return come from?

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How are you getting your input? Do you hit enter? That's what I was guessing. If it's not a string, why are you using strlen on it?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I just send a really long string
    Code:
       char message[2048];
       memset(message,'\0',2049);
       int i = strlen(message);
       while(strlen(message)<2048)
       {
       	message[i]='!';
     	i++;
     	printf("message has length %d\n",strlen(message));
       }
       printf("message to send is %d\n",strlen(message));

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you trying to memset more room than your array holds? My guess is you have a bunch of crazy stuff like that scattered around your code.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    If I suppose I did that to make sure the length is 2048

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't write past the end of your array and expect your program to work right.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I guess you're right. So if I wanted a 2048 long string, I'd need a 2049 sized buffer?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. Anything going beyond 2048 in your current code will be a buffer overflow - I thought you were studying that so that you could avoid it? Obviously didn't work.

    Second, why not do:
    Code:
    char message[2049];
    message[2048] = 0;
    memset(message, '!', 2048);
    // Optionally printf strlen(message).
    Also, unless you also put a zero at the end of the receiving buffer and have enough space to receive ALL of the message, strlen() on the receiving side will continue until it finds a zero. Consider this:
    Code:
    char message[20000];
    memset(message, '$', sizeof(message)-1);
    message[sizeof(message)-1] = 0;
    recv(acceptsock, message, 1024, 0);
    printf("Message len = %d\n", strlen(message));
    If you now recieve 1024 '!', then the message will contain 18975 '$', then a zero - so strlen is 20000. In your case, if you transmit 1024 or more bytes, strlen() will go outside the buffer.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv() returns 0 the second time
    By Overlord in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-10-2009, 04:09 AM
  2. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 01:10 PM
  3. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 04:32 PM
  4. recv() problem
    By Massive in forum C Programming
    Replies: 5
    Last Post: 06-26-2003, 04:58 AM
  5. non blocking recv
    By rohit in forum Linux Programming
    Replies: 4
    Last Post: 03-05-2002, 08:35 PM

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