Click to See Complete Forum and Search --> : shutdown socket WSAGetLastError 10057


auliac
January 9th, 2010, 11:49 AM
helo friend.....!!!

i try to built an aplication. i got difficult how to break connection between client-server.



void CComputer::OnButtonStart()
{
...
...
sServer = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
...
...
}

// my disconnect function
void CComputer::OnButtonStop()
{
....
....
// try to shutdown
if (shutdown(CComputer::sServer,2)==SOCKET_ERROR)
{
info.Format("socket() failed: %d", WSAGetLastError());
AfxMessageBox(info);
return;
}
....
.....
}


from that code i got error "socket() failed: 10057". as we know, that error message come from my shutdown().

how can i repair it???

thanks...

VictorN
January 9th, 2010, 03:06 PM
FYI: Windows Sockets Error Codes (http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx)

auliac
January 11th, 2010, 08:45 AM
FYI: Windows Sockets Error Codes (http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx)

what must i do with this???

VictorN
January 11th, 2010, 10:02 AM
Error 10057 means WSAENOTCONN (Socket is not connected.)
So why are you trying to shutdown disconnected socket? :confused:

auliac
January 12th, 2010, 08:58 AM
Error 10057 means WSAENOTCONN (Socket is not connected.)
So why are you trying to shutdown disconnected socket? :confused:

it still connected. i knew it from my client aplication. if sServer is a "disconnected socket" my client aplication will knowit.

shutdown(CComputer::sServer,2)

if i call socket like above. is that right method?

VictorN
January 12th, 2010, 09:54 AM
shutdown(CComputer::sServer,2)

if i call socket like above. is that right method?I have no idea what '2' means.
According to MSDN there may be either SD_RECEIVE or SD_SEND or SD_BOTH:shutdown
The Windows Sockets shutdown function disables sends or receives on a socket.
...............
Remarks

The shutdown function is used on all types of sockets to disable reception, transmission, or both.

If the how parameter is SD_RECEIVE, subsequent calls to the recv function on the socket will be disallowed. This has no effect on the lower protocol layers. For TCP sockets, if there is still data queued on the socket waiting to be received, or data arrives subsequently, the connection is reset, since the data cannot be delivered to the user. For UDP sockets, incoming datagrams are accepted and queued. In no case will an ICMP error packet be generated.

If the how parameter is SD_SEND, subsequent calls to the send function are disallowed. For TCP sockets, a FIN will be sent after all data is sent and acknowledged by the receiver.

Setting how to SD_BOTH disables both sends and receives as described above.

The shutdown function does not close the socket. Any resources attached to the socket will not be freed until closesocket is invoked.

To assure that all data is sent and received on a connected socket before it is closed, an application should use shutdown to close connection before calling closesocket. For example, to initiate a graceful disconnect:


Call WSAAsyncSelect to register for FD_CLOSE notification.
Call shutdown with how=SD_SEND.
When FD_CLOSE received, call recv until zero returned, or SOCKET_ERROR.
Call closesocket.
Note The shutdown function does not block regardless of the SO_LINGER setting on the socket.

An application should not rely on being able to reuse a socket after it has been shut down. In particular, a Windows Sockets provider is not required to support the use of connect on a socket that has been shut down.

auliac
January 13th, 2010, 09:44 AM
before i post my thread here, i tried like code below
shutdown(CComputer::sServer,SD_BOTH)

and i got message "SD_BOTH undeclared identifier " from my compiler.

thanks very much for you attention..:D

VictorN
January 13th, 2010, 10:19 AM
before i post my thread here, i tried like code below
shutdown(CComputer::sServer,SD_BOTH)

and i got message "SD_BOTH undeclared identifier " from my compiler.Reading MSDN saves you much time, BTW! :rolleyes:
FROM MSDN article "shutdown":Requirements
Version: Requires Windows Sockets 1.1 or later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.

auliac
January 14th, 2010, 01:57 AM
okay,,, thanks for your help and suggestion....:d

mikoil
January 22nd, 2010, 08:51 AM
And by the way, for the two of you, the "2" at the shutdown() is the actual value of SD_BOTH ! :)

VictorN
January 22nd, 2010, 09:00 AM
And by the way, for the two of you, the "2" at the shutdown() is the actual value of SD_BOTH ! :)Wow! I am you know it now!
However, I think 9 days is too much to look at the SD_BOTH definition in the Winsock2.h file! (see post#8) :D