0

I have this piece of code where a server socket is created and is set to listen on a particular port number say 5005. Now once the accept socket function returns the socket that gets created is copied into the m_Socket variable and finally i shutdown the server socket named SocServer which was created locally.

Now my question Is it possible that the SocServer(created initially) and m_Socket(copied when accept returns) get the same number say 1500.

struct sockaddr_in   ServerSock;                        // Socket address structure to bind the Port Number to listen to

    char *localIP ;

    SOCKET SocServer;

    //To Set up the sockaddr structure
    ServerSock.sin_family = AF_INET;
    ServerSock.sin_addr.s_addr = INADDR_ANY    
    ServerSock.sin_port = htons(PortNumber);//port number of 5005

    // To Create a socket for listening on PortNumber
    if(( SocServer = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
    {
        return FALSE;
    }

    //To bind the socket with wPortNumber
    if(bind(SocServer,(sockaddr*)&ServerSock,sizeof(ServerSock))!=0)
    {
        return FALSE;
    }

    // To Listen for the connection on wPortNumber
    if(listen(SocServer,SOMAXCONN)!=0)
    {
        return FALSE;
    }

    // Structure to get the IP Address of the connecting Entity
    sockaddr_in insock;
        int insocklen=sizeof(insock);
        //To accept the Incoming connection on the wPortNumber
    m_Socket=accept(SocServer,(struct sockaddr*)&insock,&insocklen);
     //delete the server socket
     if(SocServer != INVALID_SOCKET)
    {
        //To close and shutdown the Socserver
        shutdown(SocServer, 2 );      
        closesocket(SocServer);
    }

is it possible that Socserver and m_socket are the same because

as per my code the socket connection is established and for some other reason it gets closed and in TCPView it shows established for a while and then no connection at all.

Note: This happens only in some machines and is not reproducible always. Can any other network related issue be the cause.

ckv
  • 10,539
  • 20
  • 100
  • 144
  • 1
    are you worried that your m_Socket might be closed when you call shutdown on the Socserver? – default Jun 15 '10 at 17:04
  • 1
    are you calling WSACleanup to soon somewhere? I have tried to describe socket programming here as well: http://stackoverflow.com/questions/2843277/c-winsock-p2p/2920787#2920787 – default Jun 15 '10 at 17:20
  • @Michael I cant really complain too much about the code either. It is a legacy piece of code that i am working on currently. I dont call WSACleanup too soon. So that is ruled. And yes i am worried that m_Socket might be closed when i call the shutdown on the SocServer. – ckv Jun 16 '10 at 04:35
  • Could you find the call that tells you that the socket connection gets closed? You should be able to find it by getting the return value `SOCKET_ERROR` of the blocking calls (`accept, send, recv`) and then calling `WSAGetLastError()`). Post that here and I think someone will figure it out. – default Jun 16 '10 at 06:19
  • None of the accept,send, recv functions are failing or in othe r words throwing up SOCKET_ERROR. They are succeeding. – ckv Jun 16 '10 at 07:27

2 Answers2

2

Are you certain that the client who is connecting to your server did not close the connection? Also, you did not provide any function which uses the m_Socket so i cannot tell you if there is any problem while handling the incoming connection. I do not think that m_socket and SocServer may end up the same.

PeterK
  • 6,287
  • 5
  • 50
  • 86
  • You mean the m_Socket and SocServer can never be the same right?? – ckv Jun 15 '10 at 11:07
  • 1
    Thats right, since SocServer (should) be a valid socket at the time of accept, the m_Socket must be different (otherwise that would be a VERY strange behaviour of accept). – PeterK Jun 15 '10 at 11:15
  • Yes i am curious to know more about this strange behaviour because we seriously doubt this occurence – ckv Jun 15 '10 at 12:35
1

In this code:

 m_Socket=accept(SocServer,(struct sockaddr*)&insock,&insocklen);
 if(SocServer != INVALID_SOCKET)

why do you call accept() with what may apparently be a bad socket? And do you test the value you get back from accept() anywhere?

  • 2
    @viswanathan Well, do - it might be an error code rather than a socket. In general, you should check the return value of every single socket API function that you call, and issue appropriate diagnostics on error. –  Jun 15 '10 at 11:07
  • @Neil as part of code cleanup i shall check for the validity of the socket. Meanwhile can m_Socket and SocServer be the same. – ckv Jun 15 '10 at 11:09
  • @Neil The SocServer validation is checked in above code as below if(( SocServer = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) { return FALSE; } Any other validity you are referring to? – ckv Jun 15 '10 at 14:52