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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:53+00:00 2026-06-14T21:02:53+00:00

Currently I have a socket server that can only accept ONE connection. Any second

  • 0

Currently I have a socket server that can only accept ONE connection. Any second connection, it just hangs and not do anything.

The server can get the message send from one client. I have the server to send back confirmation only for now.

server.py:

import socket, sys

# some vars
HOST = "localhost";
PORT = 4242;

# create the socket
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM);

# bind the socket to port
server_addr = (HOST, PORT);
print >>sys.stderr, "starting server %s on port %s" % (HOST, PORT);
soc.bind(server_addr);

# check for connections
soc.listen(1);

while True:
    # wait for a connection
    connection, client_address = soc.accept();
    try:
        # since just test
        # just send back whatever server receives
        while True:
            data = connection.recv(16);
            if data:
                connection.sendall(str(client_address[1]) + " said " + data);
    finally:
        connection.close();

client.py:

import socket, sys, thread

# some vars
HOST = "localhost";
PORT = 4242;

# create the socket
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM);

# connect to server/port
server_addr = (HOST, PORT);
print >>sys.stderr, "connecting to %s on port %s" % (HOST, PORT);
soc.connect(server_addr);

# try to send data over
while True:
    try:
        # send the message
        msg = raw_input("Message: ");
        soc.sendall(msg);

        # check if there is response
        amt_recd = 0;
        amt_expd = len(msg);

        while amt_recd < amt_expd:
            data = soc.recv(16);
            amt_recd += len(data);
            print >>sys.stderr, data, "\n";
    finally:
        msg = '';
  • 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-14T21:02:54+00:00Added an answer on June 14, 2026 at 9:02 pm

    There is no exit condition from this infinite loop in the server:

    while True:
        data = connection.recv(16)
        if data:
            connection.sendall(str(client_address[1]) + " said " + data)
    

    If the client closes the connection data will be empty, but it will still continue looping. To fix:

    while True:
        data = connection.recv(16)
        if not data:
            break
        connection.sendall(str(client_address[1]) + " said " + data)
    

    Also, even after fixing this the server can only handle one connection at a time. If you desire to service multiple clients at once, you’ll need to use select.select or spin off threads for each client connection.

    As an aside, Python does not require semicolons at the end of statements.

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

Sidebar

Related Questions

I have an applet that communicates with a servlet using Http (Not sockets). Currently,
I currently have one project that currently contains multiple packages. These packages make up
I have a java server that has to communicate over a TCP socket to
I have a socket.io server using redis called server.js that fires up a node
Currently we have an Applet that interacts with our hardware and server via FTP,
I've written a server that accepts a socket connection on a secondary port for
Currently i have a node.js and socket.io application in development on my local machine
Currently, I have a while loop for listening the port. When a socket accepta
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty

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.