Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:35:54+00:00 2026-05-31T09:35:54+00:00

The following code runs a socket server on a thread. The client socket sends

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:35:56+00:00Added an answer on May 31, 2026 at 9:35 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create a new thread that runs the following code: public static void startServer()
The following code runs fine under Magento 1.6 but raises a Mage_Core_Exception (message: 'Cannot
i've got the following code which runs a bat file. the bat file then
The following piece of code runs fine when parallelized to 4-5 threads, but starts
I have the following jQuery code which runs when I'm clicking an option in
I have the following SQL code that runs against a Change Request database. Each
The following code works & runs perfectly. public class Complex { private int real,
The following code converts a PDF page into a JPEG. It runs as expected
I am having the strangest time with the following PHP code. The require_once runs
Why the following code runs so.... slow....... ? <html><body><script type=text/javascript> var i = 0;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.