1
votes

I am facing this problem of Binding to socket. 1st instance works properly i.e. socket() returns success and hence forth bind() and listen(), accept() and hence recv() - All fine till here. 2nd instance throw error while binding "Address already in use"

I went through all the post earlier on this and i dont see any specific solution provided on the same.

My code is as below :-

if((status = getaddrinfo(NULL,"8080",&hints,&servinfo))!=0){
        ALOGE("Socket:: getaddrinfo failed %s\n",strerror(errno));
        return NULL;
    }

    server_sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
    if(server_sockfd == -1) {
        ALOGE("Socket:: Scoket System Call failed %s\n",strerror(errno));
        return NULL;
    }

    if ((setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int))) < 0)
    {
        ALOGE("Socket:: setsockopt failed %s\n",strerror(errno));
        return NULL;
    }

    ret = bind(server_sockfd, servinfo->ai_addr,servinfo->ai_addrlen);
    if(ret!=0) {
        ALOGE("Socket:: Error Binding on socket %s\n",strerror(errno));
        return NULL;
    }

This code runs on android platform.

I have properly closed each session before opening a new session as below :-

ret = shutdown(client_sockfd,0);
if(ret != 0)
    ALOGE("Socket:: Shutdown Called%s\n",strerror(errno));

I tried with close as well but it did not work.

Surprisingly the error does not disappear even when we try to open the socket after long time (as per TIME_WAIT logic)

Could anyone please guide me to proper call or API or Logic(in code and not on command line apart from directly killing the process) to handle this situation ?

2
You are (incorrectly) closing an accepted client socket, but are you also closing the listening server socket? You cannot bind a new TCP socket to the same IP/port that a previous socket is still bound to. Why are you trying to bind multiple sockets to the same IP/port in the first place?Remy Lebeau
Thanks for your comments. Yes i call close in both client and server socket. The logic behind the same is my server thread is part of an application which is launched and based on successful launch i create the server thread.This thread need not unnecessarily be active even after my application is closed.To handle this condition i have tried to close the socket when my pthread_exit is called. As far as client is concerned i assume its OK to call close when client is done with its work. Please correct me wrong if my logic seems incorrect.user5729621
You are clearly not closing everything correctly. Something is still open. Please provide a Minimal, Complete, and Verifiable example showing how you a managing all of the sockets.Remy Lebeau
Where is the code that is calling close() on server_sockfd? Where is the code that is calling close() on client_sockfd? Again, please provide an MCVE showing all relevant code related to your sockets.Remy Lebeau
Thanks @RemyLebeau I think i got the problem. I was closing the server socket fd which i got from accept() but was not closing server actual socket() fd Now it seems to work. Thanks for your information. Will keep not of posting etiquette.user5729621

2 Answers

1
votes

A socket is one half a channel of communication between two computers over a network on a particular port. (the other half is the corresponding socket on the other computer)

Error is very clear I suppose in this case. As mentioned Address already in use, so the the socket you are trying to connect in the second attempt is already used (port was already occupied) -> maybe due to first socket connection.

To investigate further check another SO question here and here

0
votes

You can't share a TCP listening port between two processes even with SO_REUSEADDR.

NB shutdown() does not close a TCP session. It half-closes it. You have to close the socket.