closing sockets on linux and windows
gcc 4.4.4 c89 Visual Studio VC++ 2008
I am writing a cross platform client server application. It will run on both linux and windows.
However, I am just wondering what I have done for closing the sockets is correct. I close the file descriptor. However, if there is a problem with closing it. What is the best way to handle this. Maybe some data is still being sent or received?
Many thanks for any advice,
if(close(sockfd) == -1)
{
#if defined ( _WIN32 )
fprintf(stderr, "[ %d ] [ %s ] [ %s ] [ %d ]\n",
WSAGetLastError(), strerror(errno), __func__, __LINE__);
#elif开发者_运维知识库( __linux__ )
fprintf(stderr, "[ %s ] [ %s ] [ %d ]\n", strerror(errno), __func__, __LINE__);
#endif
return CS_FAILURE;
}
Well, for one you need to use closesocket on Windows, not close.
In terms of handling the error, look at the possible error codes and decide which of those you want to handle: http://msdn.microsoft.com/en-us/library/ms737582(VS.85).aspx
"A successful WSAStartup call must occur before using this function" probably means a bug in your app, not something you can handle on the fly, so good logging is a good idea.
精彩评论