Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Socket Multiple Clients

So I am working on an iPhone app that requires a socket to handle multiple clients for online gaming. I have tried Twisted, and with much effort, I have failed to get a bunch of info to be sent at once, which is why I am now going to attempt socket.

My question is, using the code below, how would you be able to have multiple clients connected? I've tried lists, but I just can't figure out the format for that. How can this be accomplished where multiple clients are connected at once and I am able to send a message to a specific client?

Thank you!

#!/usr/bin/python           # This is server.py file  import socket               # Import socket module s = socket.socket()         # Create a socket object host = socket.gethostname() # Get local machine name port = 50000                # Reserve a port for your service.  print 'Server started!' print 'Waiting for clients...'  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 while True:    msg = c.recv(1024)    print addr, ' >> ', msg    msg = raw_input('SERVER >> ')    c.send(msg);    #c.close()                # Close the connection 
like image 371
Alec Avatar asked May 30 '12 05:05

Alec


People also ask

Can multiple clients connect to same socket?

Multiple clients can connect to the same port (say 80) on the server because on the server side, after creating a socket and binding (setting local IP and port) listen is called on the socket which tells the OS to accept incoming connections.

How do you send a message to multiple clients in Python?

To take multiple clients simultaneously, you will have to add SocketServer. ForkingMixIn or ThreadingMixIn .


1 Answers

Based on your question:

My question is, using the code below, how would you be able to have multiple clients connected? I've tried lists, but I just can't figure out the format for that. How can this be accomplished where multiple clients are connected at once and I am able to send a message to a specific client?

Using the code you gave, you can do this:

#!/usr/bin/python           # This is server.py file                                                                                                                                                                             import socket               # Import socket module import thread  def on_new_client(clientsocket,addr):     while True:         msg = clientsocket.recv(1024)         #do some checks and if msg == someWeirdSignal: break:         print addr, ' >> ', msg         msg = raw_input('SERVER >> ')         #Maybe some code to compute the last digit of PI, play game or anything else can go here and when you are done.         clientsocket.send(msg)     clientsocket.close()  s = socket.socket()         # Create a socket object host = socket.gethostname() # Get local machine name port = 50000                # Reserve a port for your service.  print 'Server started!' print 'Waiting for clients...'  s.bind((host, port))        # Bind to the port s.listen(5)                 # Now wait for client connection.  print 'Got connection from', addr while True:    c, addr = s.accept()     # Establish connection with client.    thread.start_new_thread(on_new_client,(c,addr))    #Note it's (addr,) not (addr) because second parameter is a tuple    #Edit: (c,addr)    #that's how you pass arguments to functions when creating new threads using thread module. s.close() 

As Eli Bendersky mentioned, you can use processes instead of threads, you can also check python threading module or other async sockets framework. Note: checks are left for you to implement how you want and this is just a basic framework.

like image 114
jcchuks Avatar answered Sep 22 '22 01:09

jcchuks