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 );
}

Recommended Answers

All 12 Replies

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.

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.

if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************

try using

if( iResult == SOCKET_ERROR ){//*******HERES THE PROBLEM***************

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

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.

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.

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?

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

What ip should I write? and what port?

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).

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.