ksbuble committed 692a1c6

Added Keyboard Interrupt to Client, which sends a 'quit' message to the Server. This closes the Connection and shutsdown the Client socket.

Comments (0)

Files changed (2)

 .. conventions.  It will be displayed on the bitbucket source page and
 .. serves as the documentation of the directory.
+To Do
+Prototype for asynchronous plotting with separate processes using sockets
+Still Needs:
+* socket buffer overflow (recv)
+* cleanup thread and socket command line termination
+* error handling. ex. when a client disconnects then server listen continues, network errors
+* hard coded ports - put args.
+* multiple clients (plotting)
 Async Plotter


 import socket
 import cPickle
 class Plot(object):
     def __init__(self):
         from matplotlib import pyplot as plt
         r"""Overload this to effect the actual drawing.  All arguments must be
         pickelable.  This will be called on the client"""
         raise NotImplementedError
     def update(self, *v, **kw):
         r"""Call this with the same arguments required by the overloaded draw
         method.  This will be called on the server."""
                 self.args = None
                 print "Send plot data"
             elif not data or data.startswith("quit"):
+                print "Received 'quit' message from Client"
+        print "close client connection"
         print "Done"
     def update(self, *v, **kw):
         self.args = (v, kw)
 class Client(object):
     def __init__(self, Plot, host='localhost', port=9999):
         print "Trying to connect to port %i..." % (self.port,)
         sock.connect((, self.port))
         print "Connected to port %i." % (self.port,)
+        BUF_SIZE = 4096
         while True:
-            sock.send('get')
-            data = sock.recv(4096)
-            v, kw = cPickle.loads(data)
-            self.plot.draw(*v, **kw)
+            try:
+                sock.send('get')
+                data = sock.recv(BUF_SIZE)
+                print "data from Server is %i bytes long." % (len(data),)
+                v, kw = cPickle.loads(data)
+                self.plot.draw(*v, **kw)
+            except KeyboardInterrupt:
+                sock.send('quit')
+                print "Sent 'quit' to Server."
+                break
+        print "close client socket and shutdown. "
+        sock.close()
+        sock.shutdown(1)
 if __name__ == '__main__':