0

I want to implement a parallel server. The server should acceppt more than one client at the same time. So that the client can send messages to the server.

A serial server is working awesome. I can connect, write, close and can connect again, no problem. Now I want to implement threads. So like: For every new client, there have to be a new thread that handles a TCP socket with one client.

My code for the serial server:

    #!/usr/bin/python           # This is server.py file

import socket               # Import socket module
import time

while True:
   s = socket.socket()         # Create a socket object
   host = socket.gethostname() # Get local machine name
   port = 12345                # Reserve a port for your service.
   s.bind((host, port))        # Bind to the port

   s.listen(5)                 # Now wait for client connection.

   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Please wait...')
   time.sleep(2)
   c.send('Thank you for connecting with your admin. Please write now.')

   while True:
      msg = c.recv(1024)
      if not msg:
         s.close()
         break
      elif msg == "close1234567890":
         print ("Connection with %s was closed by the client." % (addr[0]))
      else:
         print "%s: %s" % (addr[0], msg)

My TRY for the parallel server:

import socket              
import time
import thread


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
s.bind((host, 50999))       
s.listen(5)                

def session(conn, addr):
   while True:
      print 'Got connection from', addr
      conn.send('Please wait...')
      time.sleep(2)
      conn.send('Thank you for connecting with your admin. Please write now.')

      while True:
         msg = conn.recv(1024)
         if not msg:
            s.close()
            break
         elif msg == "close1234567890":
            print ("Connection with %s was closed by the client." % (addr[0]))
         else:
            print "%s: %s" % (addr[0], msg)

while True:
    conn, addr = s.accept()
    try:
       thread.start_new_thread(session(conn, addr))
    finally:
       s.close()

Error: I start the server, no prob. Then I start the Client and everything is normal. I can write and the messages are printed out by the server. Then I start a second client, but in this windows nothing happens. No chance to write from the second client.

Sry, I am absolutely beginner with threads ;)

1 Answer 1

1

This is because you s.close() the socket. Here's modified code:

def session(conn, addr):
    while True:
        print 'Got connection from', addr
        conn.send('Please wait...')
        time.sleep(2)
        conn.send('Thank you for connecting with your admin. Please write now.')

        while True:
            msg = conn.recv(1024)
            if not msg:
                conn.close()
                break
            elif msg == "close1234567890":
                print ("Connection with %s was closed by the client." % (addr[0]))
            else:
                print "%s: %s" % (addr[0], msg)

while True:
    conn, addr = s.accept()
    thread.start_new_thread(session(conn, addr))

s.close()

I've tested it and works well. BTW I change your:

host = socket.gethostname()
s.bind((host, 50999))

to s.bind(('localhost', 50999)). I'm not sure why you need your machine name, and the original code doesn't work at all——there's no point binding hostname to a socket.

Sign up to request clarification or add additional context in comments.

1 Comment

Sry, but it doesn't work for my client code. Maybe it's an error in the client. I did not think, that I have to update this code, too. But maybe...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.