0

The following code runs a socket server on a thread. The client socket sends 'client: hello' to the server, and the server socket receives and replies 'server: world'.

import socket
import threading

def server():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('127.0.0.1', 12345))
    sock.listen(1)
    req, addr = sock.accept()
    print req.recv(1024)
    req.sendall('server: world')

def client():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('127.0.0.1', 12345))
    sock.sendall('client: hello')
    print sock.recv(1024)

def main():
    t = threading.Thread(target=server)
    t.start()
    client()

if __name__ == '__main__':
    main()

It runs ok as expected the first time, but from the second time, if you do not wait for a good few seconds for the server to release the socket, and if you try this on a Linux machine or Mac (Windows do not get it somehow) you will run into this error:

Traceback (most recent call last):
  File "socket_send_receive.py", line 24, in <module>
    main()
  File "socket_send_receive.py", line 21, in main
    client()
  File "socket_send_receive.py", line 14, in client
    sock.connect(('127.0.0.1', 12345))
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/home/cxuan/python/2.6/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/home/cxuan/python/2.6/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "socket_send_receive.py", line 6, in server
    sock.bind(('127.0.0.1', 12345))
  File "<string>", line 1, in bind
error: [Errno 98] Address already in use

I am looking for some insight into why this is happening and if it is possible to be genuinely resolved or what best practice should be adopted.

I know already using this option can be a workaround thanks to the other posts here on stackoverflow.

socket.SO_REUSEADDR

1 Answer 1

4

When a socket is closed, it ends up in a state called STATE_WAIT (see this diagram). While the socket is in this state, no one else can use the same address (ip-number/port pair) unless the SO_REUSEADDR option is set on the socket.

See e.g. the Wikipedia article on TCP for more information about how TCP works and the different states.

1
  • Nice answer! It gives a lot of insight. The threading is a red herring. Originally I had the client and server code split into two files,and ran them in two processes.The server released the socket immediately, but when put them together like above it did not happen that quickly. However, the test was only on a Mac. After reading your answer, I tried it on a Linux box, the same error happened! It's interesting that sockets on different platforms have slightly different behaviors. Though subtle, it could cause problems if code is not carefully written following the mainstream principles.
    – Xuan
    Mar 14, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.