954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Winsock - weird problem with shutdown( socket, SD_SEND )

First..Im very beginner with winsock and network programing..
Im doing a simple app that can act or as server or as client. What Im doing is that I open the app twice in the same computer, in one I choose server, and in the other client.

With the server part, everything is fine, but with the client, when I shutdown the socket for sending, shutdown returns SOCKET_ERROR, I cant figure out why, the most weird thing is that WSAGetLastError() returns 0(zero)! That doesnt make sense..

Also, what Im doing is that if an error occur, I close the socket and quit the app, but if I just ignore that shutdown error, everythig is fine..

It doesnt make sense, its like shutdown should not return any error..

Heres the client part of my code:

//send data:
	iResult = send( ConnectSocket, sendBuffer, (int)strlen(sendBuffer), 0 );	//return size of bytes sent

	if( iResult == SOCKET_ERROR ){

		MessageBox( NULL, TEXT("send() failed"),
					TEXT(" winsock error"), MB );
		closesocket( ConnectSocket );
		return;
	}
	TCHAR buffer[100];
	wsprintf( buffer, TEXT(" number of bytes sent: %i "), iResult );

	TextOut( hdc, x, y, buffer, (int)strlen(buffer) );
	y+=20;
	//---------------------------------------------------


	//stop sending:
	iResult = shutdown( ConnectSocket, SD_SEND );	//it still receive!
	if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************

		iResult = WSAGetLastError();//****RETURNS ZERO????************

		TextOut( hdc, x, y, TEXT(" shutdown() failed"), (int)strlen(TEXT(" shutdown() failed")) );
		y+=20;

		//closesocket( ConnectSocket );//****COMMENTING THIS, WORKS FINE**
		//return;
	}
	//---------------------------------------------------


	//receive data until server close connection:
	do{
		iResult = recv( ConnectSocket, recvBuffer, 1000, 0 );	//return size of bytes received

		if( iResult > 0 ){

			wsprintf( buffer, TEXT(" number of bytes received: %i "), iResult );//display bytes received
			TextOut( hdc, x, y, buffer, (int)strlen(buffer) );
			y+=20;

			TextOut( hdc, x, y, recvBuffer, iResult );			//display data received
			y+=20;

		}else if( iResult == 0 ){

			TextOut( hdc, x, y, TEXT("Connection Closed!"), (int)strlen(TEXT("Connection Closed!")) );
			y+=40;

		}else{

			TextOut( hdc, x, y, TEXT(" recv() failed"), (int)strlen(TEXT(" recv() failed")) );
			y+=40;
		}

	}while( iResult > 0 );
	//---------------------------------------------------

	ReleaseDC( hmainwnd, hdc );


	//stop receive and close socket:
	closesocket( ConnectSocket );
}
Icebone1000
Junior Poster in Training
50 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

The shutdown function disables sends or receives on a socket and you are using the ConnectSocket after the shutdown function to receive info.
The shutdown function is normally used before the close function so that you stop the stream flow before you close the socket.

Cosmin871
Newbie Poster
23 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 
The shutdown function disables sends or receives on a socket and you are using the ConnectSocket after the shutdown function to receive info. The shutdown function is normally used before the close function so that you stop the stream flow before you close the socket.


That doesnt explains anything man, Im following the msdn samples btw, and thats exactly what it does, you shutdown just for sending, but you still can receive, and Im get the error AT the function, not after.

Icebone1000
Junior Poster in Training
50 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************

try using

if( iResult == SOCKET_ERROR ){//*******HERES THE PROBLEM***************
Cosmin871
Newbie Poster
23 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

how can I send messages in global. like skype does?

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

You must know the socket you want to send the message:
iResult = send( SOCKET s, char *buff, int len, int flag );
so if you want to send a message to multiple users you must send the message to every user -> socket.

Cosmin871
Newbie Poster
23 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

I can send messages on lan but I cann't send messages on global on internet. what should I do have you got a source of how can I send messages on global.

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

I can send messages on lan on local but I cann't send messages on internet on global. please help me. do you have a source of sending messages on global on internet?

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

if you are using sockets there should be no problem sending messages over the internet, and if it works over LAN it should work for everything, maybe the IP was not correct

Cosmin871
Newbie Poster
23 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

What ip should I write? and what port?

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

When you created the server socket you mentioned the port of listening, you have to connect with the client at that port, if you want to connect with a client to the same machine your IP is "localhost" or if it's other machine it should be it's IP ( e.g. "192.168.0.65).

Cosmin871
Newbie Poster
23 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

someone told me for global connection you should know your globalhost IP,what should I do to know my globalhost IP. my localhost is 192.168.1.33. but what is my globalhost I don't know.

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

and if you know. how to tie to my personal computer on server?

lashatt2
Light Poster
36 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: