So we were given an assignment for software today. It's going to be used to teach us promala and spin and good multi-threaded software practices. The overall goal is to make a simple GUI based multi-user instant messager system in C++.
I haven't used allot of concurrency before so I thought I would investigate by trying some stuff out in python. Also I haven't used sockets directly before - so wanted to have a look at them.
Firstly taking a read of the Socket Programming HOWTO guide - there is plenty of information there. And also handy is the Socket documentation for python.
For now I just want a echoing server that can service multiple clients at once.
So the server will be the difficult part - might as well dive in there!
Firstly creating a socket and echoing any data it recieves is pretty easy with this loop:
And the rest of the script that actually starts these threads, minus the imports:
Now that wasn't too painful at all, the client is even easier. It needs to establish a connection via the socket with the server, then pass and receive data. Easy as:
So starting up the server:
brian@brian-hitlab:~/projects/python/instantmess/src$ python server.py
INFO:root:Logger enabled
INFO:root:Server started
DEBUG:root:Created a socket on the server
DEBUG:root:Server Socket has been bound. Server is ready to accept connections
DEBUG:root:Main thread of server listening for new connections
At this point the server just waits for a client to be started, so lets do that in another terminal:
brian@brian-hitlab:~/projects/python/instantmess/src$ python client.py
INFO:root:Logger enabled
INFO:root:Client started
Client Name: client1
DEBUG:root:Created a socket on the client
DEBUG:root:Client socket connected
DEBUG:root:Sending Client name
DEBUG:root:Sent data, trying to receive now
DEBUG:root:Data received 'client1'
And then the loop:
INFO:root:Start a loop. 'quit' will quit
>Hi
Hi
>this is a test
this is a test
>sweet
sweet
>quit
DEBUG:root:Closing socket
Received 'quit'
During all this the server was spitting out lots as well:
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "Hi" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "this is a test" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "sweet" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Connection to client '('127.0.0.1', 50535)' complete. Breaking connection
INFO:root:Finished background servicing of client: ('127.0.0.1', 50535)
Okay time for lunch.
I haven't used allot of concurrency before so I thought I would investigate by trying some stuff out in python. Also I haven't used sockets directly before - so wanted to have a look at them.
Firstly taking a read of the Socket Programming HOWTO guide - there is plenty of information there. And also handy is the Socket documentation for python.
For now I just want a echoing server that can service multiple clients at once.
So the server will be the difficult part - might as well dive in there!
Firstly creating a socket and echoing any data it recieves is pretty easy with this loop:
while True:Now wraping this up in a thread than can be started at any time is straighforward. And for good measure I'll add some logging in at the same time.
data = self.conn.recv(1024)
if not data:
break
self.conn.send(data)
class AsyncEcho(threading.Thread):
def __init__(self, conn, addr):
threading.Thread.__init__(self)
self.conn = conn
self.addr = repr(addr) # Note just storing string for identification purposes
def run(self):
logging.info("Starting to run thread for client: %s" % self.addr)
while True:
logging.debug("server waiting to receive from client: %s" % self.addr)
data = self.conn.recv(1024)
if not data:
logging.debug("Connection to client '%s' complete. Breaking connection" % self.addr)
break
logging.debug('Server received: "%s" from client "%s", sending back...' % (data,self.addr))
self.conn.send(data)
logging.info('Finished background servicing of client: %s' % self.addr)
And the rest of the script that actually starts these threads, minus the imports:
logging.info("Server started")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.debug("Created a socket on the server")
s.bind((HOST, PORT))
logging.debug("Server Socket has been bound. Server is ready to accept connections")
while True:
s.listen(5) # We want to queue up at most 5 requests
logging.debug("Main thread of server listening for new connections")
conn, addr = s.accept()
logging.info('New connection by <%s>' % repr(addr))
logging.debug("Socket Object: %s" % repr(conn))
background = AsyncEcho(conn, addr)
logging.info("Created a new thread object to service this client, about to start it.")
background.start()
logging.info('Started the thread, the main program continues to run in foreground.')
Now that wasn't too painful at all, the client is even easier. It needs to establish a connection via the socket with the server, then pass and receive data. Easy as:
clientName = raw_input("Client Name: ")Now we might as well have a loop for the client so it acts more like a command line:
HOST = 'localhost' # The remote host, could be differant to server...
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.debug("Created a socket on the client")
s.connect((HOST, PORT))
logging.debug("Client socket connected")
logging.debug("Sending Client name")
s.send(clientName)
logging.debug("Sent data, trying to receive now")
data = s.recv(1024)
logging.debug("Data received '%s'" % data)
logging.info("Start a loop. 'quit' will quit")
while True:
data = raw_input(">")
if data == "quit":
break
s.send(data)
data2 = s.recv(1024)
print(data2)
So starting up the server:
brian@brian-hitlab:~/projects/python/instantmess/src$ python server.py
INFO:root:Logger enabled
INFO:root:Server started
DEBUG:root:Created a socket on the server
DEBUG:root:Server Socket has been bound. Server is ready to accept connections
DEBUG:root:Main thread of server listening for new connections
At this point the server just waits for a client to be started, so lets do that in another terminal:
brian@brian-hitlab:~/projects/python/instantmess/src$ python client.py
INFO:root:Logger enabled
INFO:root:Client started
Client Name: client1
DEBUG:root:Created a socket on the client
DEBUG:root:Client socket connected
DEBUG:root:Sending Client name
DEBUG:root:Sent data, trying to receive now
DEBUG:root:Data received 'client1'
And then the loop:
INFO:root:Start a loop. 'quit' will quit
>Hi
Hi
>this is a test
this is a test
>sweet
sweet
>quit
DEBUG:root:Closing socket
Received 'quit'
During all this the server was spitting out lots as well:
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "Hi" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "this is a test" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Server received: "sweet" from client "('127.0.0.1', 50535)", sending back...
DEBUG:root:server waiting to receive from client: ('127.0.0.1', 50535)
DEBUG:root:Connection to client '('127.0.0.1', 50535)' complete. Breaking connection
INFO:root:Finished background servicing of client: ('127.0.0.1', 50535)
Okay time for lunch.