tags:

views:

38

answers:

1

Hello,

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;
    }
+3  A: 

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.

twk
I forgot to add the closesocket for windows. Just finished compiling on linux. Thanks.
robUK