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 8156987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:07:40+00:00 2026-06-06T17:07:40+00:00

I am learning multithreading and sockets on Python but I came across a problem.

  • 0

I am learning multithreading and sockets on Python but I came across a problem. I am trying to create a server which would receive and print data from multiple clients on the same computer. Here is my server code so far:

import socket
import threading

def conn(HOST, PORT, BUFSIZE):
    serversock = RecvConnection(HOST, PORT) 
    serversock.listen(BUFSIZE)

class RecvConnection(object):

    def __init__(self,host, port, sock=None):
        if sock == None:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock

        self.sock.bind((host, port))    

    def listen(self, buffsize):
        self.sock.listen(5)
        clisock, (remhost, remport) = self.sock.accept()
        while 1:
            print clisock.recv(buffsize)

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024


threading.Thread(target=conn, args=(HOST, PORT, BUFSIZ,)).start()

The problem is that only one instance of my client file seems to be treated by my server, so it shows only the data sent by the 1st instance of my client. Here is my client code:

import socket
import threading

class CreateConnection(object):

    def __init__(self, sock=None):
        if sock is None:
            # Create a socket
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock

    def connect(self, host, port):
        # Connect to host, port
            self.sock.connect((host, port))

class Communication(CreateConnection):

    def sendMsg(self, msg):
        self.sock.send(msg)

HOST = 'localhost'
PORT = 21567

cltsock = Communication()
cltsock.connect(HOST, PORT)
while 1:
    msg = raw_input('> ')
    cltsock.sendMsg(msg)

Anyone could help me to make this happen ?
Thanks !

  • 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-06-06T17:07:41+00:00Added an answer on June 6, 2026 at 5:07 pm

    When the server accepts a connection from a client, it should start a thread to handle that connection and then loop around to wait for the next client.

    [edit]

    Here’s an example of what I mean:

    def listen(self, buffsize):
        self.sock.listen(5)
    
        while True:
            # Listen for a new client.
            clisock, (remhost, remport) = self.sock.accept()
    
            # Start listening to the new client.
            t = threading.Thread(target=self._handle_client, args=(clisock, buffsize))
            t.start()
    
    def _handle_client(self, clisock, buffsize):
        while True:
            received = clisock.recv(buffsize)
            if not received:
                # The client has closed the connection.
                break
    
            print received
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning Ruby. Needed to create a hash of arrays. This works... but I don't
I'm learning about multithreading, but after reading some tutorials I'm sort of confused. I
Learning C at University. This is not a homework, but I was trying to
Learning some VBA. So far, I've constructed this piece of code which should allow
learning the basics of Python, now ready for a bigger challange... it didn't take
I'd like to start learning multithreading in C++. I'm learning it in Java as
I am learning about threading and multithreading..so i just created a small application in
Learning CasperJS Trying to understand why the following is not displaying my results in
Learning spine.js I completed both the tutorials no problem, seems like a great framework,
(learning c#) I have a C# WPF application which when a certain form is

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.