How to free a port? | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

How to free a port?

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2011
v1jay v1jay is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 February 2011, 1:13 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
How to free a port?

hi all,

Im using a tcp socket for communication. After all the communications I close the socket and bind another socket to the same port. But it shows bind error. I see it is due to the port no being used immediately. But i am closing the first socket before the binding the second one to the same port. Why is the port not set free? How can i do it (set the port free)?

Thank you
Sponsored Links
    #2  
Old 02-09-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
Without seeing your code I can't guess why it's still open, but something must have been left open somewhere. Remember that, if it's a server socket, you've already bound it and can just accept() new connections without reopening it.
Sponsored Links
    #3  
Old 02-10-2011
v1jay v1jay is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 February 2011, 1:13 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Its a client socket.

length=sizeof(clientAddr);
socketFd=createSocket("tcp");

//setting the server address
clientAddr.sin_family=AF_INET;
clientAddr.sin_addr.s_addr=INADDR_ANY;
port=port+((incrementor++)%50);
clientAddr.sin_port=htons(port);
bindSocket(socketFd,&clientAddr,length);

//setting the server address
serverAddr.sin_family=AF_INET;
serverAddr.sin_addr.s_addr=INADDR_ANY;
serverAddr.sin_port=htons(31000);

/*TCP Connection with slave*/
if((connect(socketFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr)))==-1){
printf("Err in connect\n");
exit(0);
}

//request to server
if(send(socketFd,command,strlen(command)+1,0)<0){
printf("Err in Sending read request to server\n");
}
if(bytesReceived=recv(socketFd,reply,sizeof(reply),0)<0){
printf("Err in receiving reply from server\n");
}
shutdown(socketFd,2);
    #4  
Old 02-10-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
shutdown is insufficient, you must close() it after that.
Sponsored Links
    #5  
Old 02-10-2011
v1jay v1jay is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 February 2011, 1:13 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah i tried that too. Din work. Only then i switched to shutdown.
Sponsored Links
    #6  
Old 02-10-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 September 2014, 4:01 PM EDT
Location: Saskatchewan
Posts: 19,392
Thanks: 790
Thanked 3,271 Times in 3,066 Posts
I don't suppose you forked anywhere, leaving a copy in a child process?
Sponsored Links
    #7  
Old 02-10-2011
v1jay v1jay is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 February 2011, 1:13 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
no im using multithreaded programming.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
MountPoint / is 8% with 899.49MB free crossing threshold of 10% free sriniva0 Solaris 3 09-26-2010 09:58 AM
find free port MKSRaja Programming 0 02-25-2005 06:40 AM
how to know whether particular port number is free or not Hitesh Shah UNIX for Dummies Questions & Answers 2 10-10-2002 09:05 AM



All times are GMT -4. The time now is 06:13 AM.