recv function freezes

i've been working same code as in this tutorial:
http://www.youtube.com/watch?v=IY5IyX_KFJk

code is identical to that one

but when it comes to call recv() function my program freezes (nothing has been displayed on window)
It's like its waiting for data, and data never comes..

any help?
why recv function is not working properly?
We say it blocks. It's waiting to receive data from the other enbd of the connection.
But on my second program I get msg "Connection was found" and then again (cuz it's infinite loop), "Waiting for connection"
I even checked with breakpoint send function and it was done normally

What should I do? :[]
It might be easier to look at your code than watch some video of it. Any chance of posting the comms parts?
Actually in that video he never checks return value of send function, lol...
I get error "WSAENOTCONN"
On msdn it says: "socket is not connected", but also " Any other type of operation might also return this error"
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAENOTCONN

here's main for server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int main ()
{
	long answer;
	WSAData wsaData; 
	WORD DLLVERSION= MAKEWORD (2,1);

	answer  = WSAStartup (DLLVERSION, &wsaData);

	SOCKADDR_IN addr;
	int addrlen = sizeof (addr);

	SOCKET sListen, sConnect;

	sConnect = socket (AF_INET, SOCK_STREAM, NULL);

	addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
	addr.sin_family = AF_INET;
	addr.sin_port	= htons(1234U);

	sListen = socket (AF_INET, SOCK_STREAM, NULL);

	bind (sListen,(SOCKADDR*)&addr, sizeof (addr));

	listen (sListen, SOMAXCONN);

	while (1)
	{
		cout<<"waiting for connection"<<endl;
		addrlen = sizeof (addr);
		if (sConnect  = accept (sListen, (SOCKADDR*)&addr, &addrlen))
		{
			char str[50] = "msg";

			cout<<"connection found!"<<endl;
			cout<<"Enter your message!: ";
			cin>>str;
			answer = send (sConnect, str, strlen(str)+1, NULL);
			
			if(answer<=0)
			{
				cout<<"Error Sending a message"<<endl;
				cout<<"Error level is"<<WSAGetLastError ()<<endl;
			}
			else
			{
				cout<<"answer returned: "<<answer<<", sending was successful"<<endl;
			}
			gets (str);
			cout<<"Do You want to continue with operation?"<<endl;
			cin>>str;
			if(!strcmp(str, "n") || !strcmp(str, "N"))
			{
				break;
			}
		}
	}

	return 0;
}


and here's main for client:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main ()
{
	char quest[50];

	long answer;
	WSAData wsaData;
	WORD DLLVersion = MAKEWORD (2,1);
	WSAStartup (DLLVersion, &wsaData);

	SOCKADDR_IN addr;
	SOCKET sConnect;

	sConnect = socket (AF_INET, SOCK_STREAM, NULL);


	addr.sin_addr.s_addr  = inet_addr("127.0.0.1");
	addr.sin_family	= AF_INET;
	addr.sin_port	= htons (1234u);
	
	cout<<"Do you want to recieve message?\n";
	cin>>quest;
	while (!strcmp(quest, "y") || !strcmp (quest, "Y"))
	{
		
		char msg[250];
		string strmsg;
		connect (sConnect, (SOCKADDR * )&addr, sizeof (addr));

		answer = recv(sConnect, msg, sizeof (msg), NULL);

		if ( answer > 0 )
		{
			cout<<"Bytes received: "<< answer<<endl;
			strmsg = msg;
			cout<<strmsg;
		}
		else
		{
			cout<<"Error level is: "<<answer<<endl;
		}

		getchar ();

		cout<<"Do you want to recieve another message?\n";
		cin>>quest;
	}

	cout<<endl;
	system ("pause");
	closesocket (sConnect);
	WSACleanup ();

	return 0;
}
Last edited on
I would expect to see addrlen initialised in the loop before accept(). That aside it looks ok.

You have both the server and client prompting for user input on stdin. It's entirely possible that either the client or the server is waiting for you to type something and press enter.

I suggest you hardcode the messages that need to be send and remove all that prompting stuff. It's just a distraction and doesn't help you understand anyting.
well it helps me to understand better whole process (and makes program for me moar understandable)

anyway error is in the send (); function
can anyone tell me how to fix that? :[]
closed account (G309216C)
Tell you what I will be nice and give code to make a safe Send():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int SafeSend(SOCKET s, char* buf, int buflen)
{
	int sendlen = 0;
	int totalsend = 0;
	int remaining = buflen;

	while(sendlen != buflen)
	{
		sendlen = send(s, &buf[totalsend], remaining, 0);

		if(sendlen == SOCKET_ERROR)
		{
			return SOCKET_ERROR;
		}

		totalsend = totalsend + sendlen;
		remaining = sendlen - totalsend;
	}
return 0;
}



Of course as Nagle's Algorithm should be always be running this should mean the whole content is received in a single packet else if still this would work.

Of course you can refine it even more via using WSPSend() Hook for more error checking.

But beware do not use nagle's algorithm keeping in mind that if it is a Game server it may cause fluctuation in Bandwidth.
Last edited on
okay, I fixed send function
(and updated server code, and the problem was that I was checking is sConnect is equal to return value of accept, instead of assigning accept's return value to sConnect socket)
it returns how many bytes of data have been sent (tested it with sending few msgs and each time return value was correct)

(also I tried sending a message(some random string) without prompting for message like shown in code, but it didn't work)

now the only problem remains is that recv function still blocks :/

NVM, FIXED
THANKS FOR HELP EVERYONE
Last edited on
Topic archived. No new replies allowed.