user2205493
user2205493

Reputation: 31

Recv() Function Hangs After Sending HTTP GET Request in Winsock in C++

I am trying to make a program that uses HTTP in winsock, but I have run into a problem where the recv function just hangs there.

int connect() 
{
WSADATA t_wsa; //WSADATA structure
WORD wVers = 0x0202; //version number
int iError; //error number

wVers = MAKEWORD(2, 2); // Set the version number to 2.2
iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA

if(iError != NO_ERROR || iError == 1)
{
    printf("Error at WSAStartup()\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}

/* Correct version? */
if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2)
{
    printf("Incorrect version\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}

SOCKET sClient;
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sClient == INVALID_SOCKET || iError == 1)
{
    printf("Invalid Socket!\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}
SOCKADDR_IN sinClient;
memset(&sinClient, 0, sizeof(sinClient));

char cIP[50];
strcpy_s(cIP, "98.139.183.24");
sinClient.sin_family = AF_INET;
sinClient.sin_addr.s_addr = inet_addr(cIP); // Where to start server
sinClient.sin_port = htons(80); //Port

if(connect(sClient, (LPSOCKADDR)&sinClient, sizeof(sinClient)) == SOCKET_ERROR)
{
    /* failed at starting server */
    printf("Could not connect ot the server!\n");
    WSACleanup();
    system("PAUSE");
    return 1;
}   

// Now we can send/recv data!
printf("YOU ARE CONNECTED!\r\n");
string buffer;
buffer += "GET / HTTP/1.1\r\n";
buffer += "Host: http://www.yahoo.com/\r\n";
buffer += "Connection: close\r\n\r\n";
const char *cha = buffer.c_str();


int sent;
int response;
sent = send(sClient, cha, sizeof(cha) - 1, 0);

char recvbuf[50000];

response = recv(sClient, recvbuf, 50000, 0);
recvbuf[response] = '\0';
printf("\nReceived data = %s", recvbuf);
WSACleanup();
return(0);
}

"sent" will get printed after the send function, but nothing after recv gets printed.

What am I missing here?

Upvotes: 1

Views: 2044

Answers (1)

hmjd
hmjd

Reputation: 121971

A possible cause is that the send() is not sending the data intended:

sent = send(sClient, cha, sizeof(cha) - 1, 0);

the sizeof(cha) - 1 is actually sizeof(char*) - 1, not the actual length of the data: use buffer.length() instead.

Note that you can construct the std::string with the string literal in a single statement instead of constructing it via several concatentations. However, as the std::string is being used to obtain a const char* only there is no reason for using std::string at all:

const char* buffer = "GET / HTTP/1.1\r\n"
                     "Host: http://www.yahoo.com/\r\n"
                     "Connection: close\r\n\r\n";

sent = send(sClient, buffer, strlen(buffer), 0);

Check the return value of send() and recv() to determine success or failure, particularly recv() as the result is being used to index an array. On failure, recv() returns SOCKET_ERROR which (I think) is -1.


Handling HTTP responses correctly requires significant effort. The receiving code needs to examine the returned HTTP headers to determine how to handle the response content. For example, a HTTP response may be chunked or not. Libraries exist for managing HTTP requests, one is cpp-netlib (which was announced on isocpp.org circa February 2013).

Upvotes: 4

Related Questions