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