recv() - doesn't block?

This is a discussion on recv() - doesn't block? within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, Consider the following code: Code: #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> int main(int ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Prague, CZ
    Posts
    4

    recv() - doesn't block?

    Hi,
    Consider the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    	struct addrinfo *res, hints;
    	int cnt;
    	char buffer[] = "GET /intl/en/ HTTP/1.1\r\nHost: www.google.com\r\nX-Header: test\r\n\r\n";
    	char buffer_in[1024];
    
    	memset(&hints, 0, sizeof hints);
    	hints.ai_family = AF_INET;
    	hints.ai_socktype = SOCK_STREAM;
    
    	getaddrinfo("www.google.com", "80", &hints, &res);
    	cnt= socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    	connect(cnt, res->ai_addr, res->ai_addrlen);
    
    	send(cnt, buffer, 2048, 0);
    	while (1){
    		if (recv(cnt, buffer_in, 1024, 0) == 0)
    			printf("error :P ");
    		else
    			printf("%s\n", buffer_in);
    	}
    	close(cnt);
    	exit(EXIT_SUCCESS);
    }
    As far as I can see, socket wasn't set to be non-blocking. However, after receiving data from host, recv() starts returning 0 without waiting for any data. Why is that? In fact, it would be great if this function could wait for some data...
    Thanks in advance for your answers.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    1) The size you're passing to send() is crap. Pass the size of the data you want to send, not an arbitrary number. Additionally, this is a buffer overflow.

    2) The server is probably closing the connection from the invalid data that you're sending. Also, recv() returns <0 on error, 0 on socket close.

    3) You risk a buffer overflow by printf()'ing buffer_in without null-terminating it. If you actually get 1024 bytes of data that contains no nulls (not unlikely from a website), then it'll overflow. recv() one less than your buffer size, and write a null at buffer[recv_return_value].

    4) Memory leak in *res.

    5) Check your return codes on send(), connect() and getaddrinfo() - do they succeed?
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 01:18 AM

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