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

  • Home
  • SEARCH
  • 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 6954067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:33:30+00:00 2026-05-27T14:33:30+00:00

I create a new connection thread each time a connection with a client is

  • 0

I create a new connection thread each time a connection with a client is made. However on the second time I run a client script, I get an error, how come?

The Client

from multiprocessing.connection import Client
conn = Client(('localhost', 5555), authkey='secret_password')
conn.send('Hello World!')
conn.close()

The Server

import time
from multiprocessing.connection import Listener
from threading import Thread

_threads = []
_listener = Listener(('localhost', 5555), authkey='secret_password')

def start_server_thread():
    global _threads
    _threads.append(Thread(target=threaded_server))
    _threads[-1].daemon = True
    _threads[-1].start()

def threaded_server():
    conn = _listener.accept()
    print str(conn.recv())
    conn.close()
    _listener.close()

if __name__ == "__main__":
    start_server_thread()
    while True:
        time.sleep(1)

The Error

Traceback (most recent call last):
  File "C:\dev\spyker\t2.py", line 3, in <module>
    conn = Client(('localhost', 5555), authkey='secret_password')
  File "C:\Python26\lib\multiprocessing\connection.py", line 143, in Client
    c = SocketClient(address)
  File "C:\Python26\lib\multiprocessing\connection.py", line 263, in SocketClient
    s.connect(address)
  File "<string>", line 1, in connect
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
  • 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-27T14:33:31+00:00Added an answer on May 27, 2026 at 2:33 pm

    You have a few problems with this server code.

    You are closing the listener after the first connection

    You arent looping for more connections

    import time
    from multiprocessing.connection import Listener
    from threading import Thread
    
    _threads = []
    
    def start_server_thread():
        global _threads
        _threads.append(Thread(target=threaded_server))
        _threads[-1].daemon = True
        _threads[-1].start()
    
    def threaded_server():
        while True:
            conn = _listener.accept()
            print str(conn.recv())
            conn.close()
    
    
    if __name__ == "__main__":
        _listener = Listener(('localhost', 5555), authkey='secret_password')
    
        start_server_thread()
        while True:
            time.sleep(1)
    
        _listener.close()
    

    Disclaimer: I think is is kind of a messy way to write the server. I am only posting a fix for your current code 🙂

    Here is a slightly cleaned up version of your same code. Still not 100% ideal but cleaner I think?

    import time
    from multiprocessing.connection import Listener
    from threading import Thread
    
    class Server(Listener):
    
        def __init__(self, *args, **kwargs):
            super(Server, self).__init__(*args, **kwargs)
            self._thread = None
            self._stopping = False
    
    
        def serve(self):
            self._stopping = False
            self._thread = Thread(target=self._serve)
            self._thread.daemon = True
            self._thread.start()
    
        def _serve(self):
            threads = []
            while not self._stopping:
                conn = self.accept()
                t = Thread(target=self.handleConnection, args=(conn,))
                t.start()
                threads.append(t)
    
    
        def stop(self):
            if not self._stopping:
                print "Stopping."
                self._stopping = True
                self._thread.join(3)
                self.close()  
    
        def handleConnection(self, conn):
            print str(conn.recv())
            conn.close()        
    
    
    if __name__ == "__main__":
        listener = Server(('localhost', 5555), authkey='secret_password')
        listener.serve()
    
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt, e:
            listener.stop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since IIS assigns a worker thread for each request I've intention to create new
I create new desktop with CreateDesktop and want to get it's DC & RC.
I am using the TcpClient class in C#. Each time there is a new
In socket programming, you create a listening socket and then for each client that
I'm developing an application which handles client connections. I'm spawning a thread for each
I have a simple class that handles the connection being made between a client
I create new ASP.NET web application that use SMTP to send message. The problem
How to create new PCL file similar to existing MS doc. I have MS
I am attempting to create new membership users in an Ektron CMS400.NET-based website by
I can't create New Project on my Visual Web Developer 2008 Express with SP1.

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.