LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   port reuse problem :TCP socket (https://www.linuxquestions.org/questions/linux-newbie-8/port-reuse-problem-tcp-socket-643934/)

sweetytweety 05-21-2008 11:30 PM

port reuse problem :TCP socket
 
hi,
socket programming im using infinte loop to send and receive between client and server.so if i want to quit from server i have to use ctrl+c or(ctrl+z) to come out. Then next time i try to run the program , im getting the error "Could not bind listener socket to port". Im closing my socket at the end of progrm. (if i press ctrl+c,my socket should close so that i can reuse the same port for anytime i want). how to make the port reusable?

digvijay.gahlot 05-22-2008 04:05 AM

You quit the server by CTRL+C/CTRL+Z so the port remains open.

What is generally done is that we pass messages from client to server. At the end of the program we send some string or some token to indicate that we want to terminate.
A simple algo will be
SERVER::
while(1)
{
receive message from client;
if( message says "end")
break;
else process the message;
}
close(socket);

CLIENT::
while(1)
{
Get the message user wants to send to server, enter "end" to exit.
send the message to the server.
if(message is "end")
break;
}
close(socket);

By this sockets close gracefully. By CTRL+C/CTRL+Z program ends without closing the sockets.


All times are GMT -5. The time now is 11:36 PM.