Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi, I'm trying to play around a little with winsock, and I have this problem, where my recv() function doesn't seem to receive.

C++
char buf[1000];
	do
	{
		gets(buf);
		if ((int)buf[0] == 0)
			send(mysock, "\r\n", 2, 0);
		else
			send(mysock, buf, strlen(buf), 0);
		
		recv(mysock, buf, sizeof(buf), 0);
		printf(buf);

	} while (strcmp(buf,"~") != 0);


I connect with this to google for example. After I send my request the recv() function gets my program stuck, and doesn't get the packet that was sent back to me from the server. What is the problem?
Posted
Updated 5-Sep-12 3:04am
v2

1 solution

When connecting to a HTTP server (and I think you connected to one when saying Google) you must send valid requests to get an answer. This includes a trailing CR-LF pair (see RFC 2616[^]). The gets() function will remove the entered new line character. So the server receives your data but waits for the terminating CR-LF pair to process the request and answer.

But even when you add sending a CR-LF pair after your entered command, your code will probably fail. You are passing the length of the entered string as buffer size to your recv() call. You should read one of the many tutorials about Winsock to implement a receive function that handles received data of variable length.

You should also always check the return values of the send() and recv() calls.
 
Share this answer
 
Comments
pasztorpisti 5-Sep-12 8:51am    
As far as I know gets() and its variants don't remove the newline characters but they convert between the system-dependent newline (CRLF) and C newlines (LF). If you read in with gets()/fgets() you will only see LF instead of CRLF. According to the standard HTTP servers have to handle only CRLF newlines but some of them work well with LF only newlines. In this example we send mixed newlines, both CRLF and LF, but a server that handles LF only characters might deal with this.
Jochen Arndt 5-Sep-12 9:00am    
I have checked it before posting (http://msdn.microsoft.com/en-us/library/2029ea5f.aspx):
The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character.
pasztorpisti 5-Sep-12 9:11am    
Indeed. Then he can send a double newline so that part is OK.
DanDv 5-Sep-12 9:09am    
The send() is alright. I send the string "GET /" and after that "\r\n" and I do get a valid HTTP response. I sniffed the traffic to google using Wireshark and google sends me its home page. About the strlen(buf) on the recv() you are right, it was just a try and I forgot to change it back before I asked the question here. However it still doesn't work even if I write sizeof(buf). Can the buffer be bigger than the content-length? If so, how do I know the content-length of the response?
pasztorpisti 5-Sep-12 9:13am    
You should put the recv() outside after the loop. And yes, the received data can be easily bigger than your buffer, you should check the return values of send() and recv().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900