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.