|
-
May 23rd, 2012, 09:21 AM
#1
closesocket()
I see different examples.
Do I need to close both sockets or only the first one if I dont need them anymore?
Code:
if ((sock2 = accept(Sock1, (struct sockaddr *)&ClientAddr, &paddr_sz)) == INVALID_SOCKET)
{
printf("Accept error = %d", WSAGetLastError());
closesocket(Sock1);
return 1;
}
...
//Close the socket cleanly
closesocket(sock2);
closesocket(Sock1);
-
May 23rd, 2012, 09:42 AM
#2
Re: closesocket()
You only have to close the client socket after you send and recv your data.
You also want to follow MSDNs preferred method for shutting down communications.
At minimum:
Code:
//accept() here
//Recv Client Data
//Send back response to client
//Graceful shutdown
shutdown(sock2, SD_SEND)
//Close the socket cleanly
closesocket(sock2);
With More Debugging:
Code:
if (shutdown(clientSocket, SD_SEND) == SOCKET_ERROR)
{
OutputDebugString("shutdown() Error");
}
if (closesocket(clientSocket) == SOCKET_ERROR)
{
OutputDebugString("closesocket() Error");
}
ahoodin
To keep the plot moving, that's why.

-
May 24th, 2012, 01:50 PM
#3
Re: closesocket()
Thank you.
Its the first time i hear about the shutdown(). Not many people use this function.
-
May 25th, 2012, 09:01 AM
#4
Re: closesocket()
Sure, also you don't shut down the server socket, only the client socket. The server socket stays open to handle future communications. The accept() function will actually take the socket connection from the Server Socket and move it over to the client socket. The server socket acts like a post office, rerouting your mail to your mailbox (like the client socket), so closing it would impede future communications.
HTH,
ahoodin
To keep the plot moving, that's why.

-
May 25th, 2012, 09:09 AM
#5
Re: closesocket()
ahoodin
To keep the plot moving, that's why.

-
May 25th, 2012, 09:57 AM
#6
Re: closesocket()
Thanks man, appreciate the help!
-
May 25th, 2012, 11:58 AM
#7
Re: closesocket()
The MSDN link you gave me talking about the client
"Gets FD_READ and calls recv to get any response data sent by server ."
FD_READ is a windows message. Im only using a console program.
Do you still need to use shutdown(clientSocket, SD_SEND) in a console application?
-
May 25th, 2012, 12:21 PM
#8
Re: closesocket()
Yes. Keep in mind it is still a Windows Console!
ahoodin
To keep the plot moving, that's why.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|