1

I am trying to make a server to store data. I have some problem when I start he server it works good but then if i close it and start it again within say 30 sec the port is used.

I used netstat -antu

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.2:2300          127.0.0.1:35412         TIME_WAIT  
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 172.17.16.21:22         10.240.0.252:39012      ESTABLISHED

I then added some prints and conditions to my code.

> /* bind serv information to mysocket */
>     int mybind = bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
>     if(mybind == -1)
>     {
>             printf("ERROR! binding port with signal [%d]\n",mybind);
>             close(mysocket);
>             return EXIT_SUCCESS;
>     }

Is there some way to free my port? As it is now I close the server with ctrl+c so i guess the easiest would be to free the port when i run the program instead of freeing it before the program closes.

1
  • You might sniff around server.c to see how it builds a handler to listen for a control-c (SIGINT) and what it does when it receives same. #include <signal.h> seems significant. Oct 3 '16 at 17:11

You must log in to answer this question.

Browse other questions tagged .