Main Page | Report this Page
Linux Forum Index  »  Linux Development  »  Proper way to close a socket
Page 1 of 1    

Proper way to close a socket

Author Message
Lorenzo Thurman
Posted: Sat Aug 18, 2007 11:18 pm
Guest
I'm playing around with socket programming and have found a few
tutorials in the 'net, but I have a question that they don't seem to answer:
What's the proper way to close a connection? I use close(fd), but if I
try to restart the server app, I get the error that the socket is in
use. netstat confirms the port is in TIME_WAIT and it eventually becomes
available again, but most server apps (apache, mysql, etc) can be
restarted without such a delay. How do I accomplish this?
TIA
 
Jerry McBride
Posted: Sun Aug 19, 2007 3:15 pm
Guest
Lorenzo Thurman wrote:

Quote:
I'm playing around with socket programming and have found a few
tutorials in the 'net, but I have a question that they don't seem to
answer:
What's the proper way to close a connection? I use close(fd), but if I
try to restart the server app, I get the error that the socket is in
use. netstat confirms the port is in TIME_WAIT and it eventually becomes
available again, but most server apps (apache, mysql, etc) can be
restarted without such a delay. How do I accomplish this?


What language???


Under python, you can close and reuse the same socket almost immediately...


#!/usr/bin/python

try:
import socket
except ImportError:
print "Error imorting socket"
pass

# create an InterNET, STREAMing socket (aka TCP/IP)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# allow quick restart and reuse of server socket
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


--


Jerry McBride
 
Lorenzo Thurman
Posted: Sun Aug 19, 2007 6:43 pm
Guest
Jerry McBride wrote:
Quote:
Lorenzo Thurman wrote:

I'm playing around with socket programming and have found a few
tutorials in the 'net, but I have a question that they don't seem to
answer:
What's the proper way to close a connection? I use close(fd), but if I
try to restart the server app, I get the error that the socket is in
use. netstat confirms the port is in TIME_WAIT and it eventually becomes
available again, but most server apps (apache, mysql, etc) can be
restarted without such a delay. How do I accomplish this?


What language???


Under python, you can close and reuse the same socket almost immediately...


#!/usr/bin/python

try:
import socket
except ImportError:
print "Error imorting socket"
pass

# create an InterNET, STREAMing socket (aka TCP/IP)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# allow quick restart and reuse of server socket
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


Sorry I wasn't clear, in C.
 
Lorenzo Thurman
Posted: Sun Aug 19, 2007 6:56 pm
Guest
Lorenzo Thurman wrote:
Quote:
I'm playing around with socket programming and have found a few
tutorials in the 'net, but I have a question that they don't seem to
answer:
What's the proper way to close a connection? I use close(fd), but if I
try to restart the server app, I get the error that the socket is in
use. netstat confirms the port is in TIME_WAIT and it eventually becomes
available again, but most server apps (apache, mysql, etc) can be
restarted without such a delay. How do I accomplish this?
TIA
Ah, it looks like:

int shutdown(fd, how) is the way to go.
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Thu Dec 03, 2009 11:57 am