From d90d17c5c7fba4400914dc4f2b43506e07fb4abc Mon Sep 17 00:00:00 2001 From: nclegg Date: Wed, 8 Jan 2014 14:40:06 -0800 Subject: [PATCH 1/2] updated client and server --- assignments/session01/echo_client.py | 41 ++++++++++--------- assignments/session01/echo_server.py | 60 +++++++++++----------------- 2 files changed, 44 insertions(+), 57 deletions(-) diff --git a/assignments/session01/echo_client.py b/assignments/session01/echo_client.py index 61616c36..bdc38457 100644 --- a/assignments/session01/echo_client.py +++ b/assignments/session01/echo_client.py @@ -4,31 +4,30 @@ def client(msg, log_buffer=sys.stderr): server_address = ('localhost', 10000) - # TODO: Replace the following line with your code which will instantiate - # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) + print >>log_buffer, 'connecting to {0} port {1}'.format(*server_address) - # TODO: connect your socket to the server here. + sock.connect(server_address) - # this try/finally block exists purely to allow us to close the socket - # when we are finished with it try: print >>log_buffer, 'sending "{0}"'.format(msg) - # TODO: send your message to the server here. - - # TODO: the server should be sending you back your message as a series - # of 16-byte chunks. You will want to log them as you receive - # each one. You will also need to check to make sure that - # you have received the entire message you sent __before__ - # closing the socket. - # - # Make sure that you log each chunk you receive. Use the print - # statement below to do it. (The tests expect this log format) - chunk = '' - print >>log_buffer, 'received "{0}"'.format(chunk) + + sock.sendall(msg) + + buffer_size = 16 + done = False + + while not done: + chunk = sock.recv(buffer_size) + print >>log_buffer, 'received "{0}"'.format(chunk) # ORIGINAL + + # test last bytes received + if len(chunk) < buffer_size: + done = True + finally: - # TODO: after you break out of the loop receiving echoed chunks from - # the server you will want to close your client socket. + sock.close() print >>log_buffer, 'closing socket' @@ -39,4 +38,4 @@ def client(msg, log_buffer=sys.stderr): sys.exit(1) msg = sys.argv[1] - client(msg) \ No newline at end of file + client(msg) diff --git a/assignments/session01/echo_server.py b/assignments/session01/echo_server.py index 217380fb..ac3bd968 100644 --- a/assignments/session01/echo_server.py +++ b/assignments/session01/echo_server.py @@ -5,65 +5,53 @@ def server(log_buffer=sys.stderr): # set an address for our server address = ('127.0.0.1', 10000) - # TODO: Replace the following line with your code which will instantiate - # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None - # TODO: Set an option to allow the socket address to be reused immediately - # see the end of http://docs.python.org/2/library/socket.html + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) + + + # allow imediate socket reuse http://docs.python.org/2/library/socket.html + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # log that we are building a server print >>log_buffer, "making a server on {0}:{1}".format(*address) - # TODO: bind your new sock 'sock' to the address above and begin to listen - # for incoming connections + sock.bind(address) + sock.listen(1) try: - # the outer loop controls the creation of new connection sockets. The - # server will handle each incoming connection one at a time. while True: print >>log_buffer, 'waiting for a connection' - # TODO: make a new socket when a client connects, call it 'conn', - # at the same time you should be able to get the address of - # the client so we can report it below. Replace the - # following line with your code. It is only here to prevent - # syntax errors - addr = ('bar', 'baz') + conn, addr = sock.accept() + try: print >>log_buffer, 'connection - {0}:{1}'.format(*addr) - # the inner loop will receive messages sent by the client in - # buffers. When a complete message has been received, the - # loop will exit while True: - # TODO: receive 16 bytes of data from the client. Store - # the data you receive as 'data'. Replace the - # following line with your code. It's only here as - # a placeholder to prevent an error in string - # formatting - data = '' + + data = conn.recv(16) print >>log_buffer, 'received "{0}"'.format(data) + # TODO: you will need to check here to see if any data was # received. If so, send the data you got back to # the client. If not, exit the inner loop and wait # for a new connection from a client + + if not data: + break + conn.sendall(data) finally: - # TODO: When the inner loop exits, this 'finally' clause will - # be hit. Use that opportunity to close the socket you - # created above when a client connected. Replace the - # call to `pass` below, which is only there to prevent - # syntax problems - pass + conn.close() + print >>log_buffer, 'closing socket' + + except KeyboardInterrupt: - # TODO: Use the python KeyboardIntterupt exception as a signal to - # close the server socket and exit from the server function. - # Replace the call to `pass` below, which is only there to - # prevent syntax problems - pass + sock.close() + print >>log_buffer, 'connection closed' if __name__ == '__main__': server() - sys.exit(0) \ No newline at end of file + sys.exit(0) From fb354f8c80acceee86356b075e4452769cd532c8 Mon Sep 17 00:00:00 2001 From: nclegg Date: Wed, 8 Jan 2014 14:54:45 -0800 Subject: [PATCH 2/2] rm of some TODOs --- assignments/session01/echo_client.py | 2 +- assignments/session01/echo_server.py | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/assignments/session01/echo_client.py b/assignments/session01/echo_client.py index bdc38457..2cdcc182 100644 --- a/assignments/session01/echo_client.py +++ b/assignments/session01/echo_client.py @@ -20,7 +20,7 @@ def client(msg, log_buffer=sys.stderr): while not done: chunk = sock.recv(buffer_size) - print >>log_buffer, 'received "{0}"'.format(chunk) # ORIGINAL + print >>log_buffer, 'received "{0}"'.format(chunk) # test last bytes received if len(chunk) < buffer_size: diff --git a/assignments/session01/echo_server.py b/assignments/session01/echo_server.py index ac3bd968..20ad8d2a 100644 --- a/assignments/session01/echo_server.py +++ b/assignments/session01/echo_server.py @@ -9,7 +9,7 @@ def server(log_buffer=sys.stderr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) - # allow imediate socket reuse http://docs.python.org/2/library/socket.html + # allow imediate socket re-use http://docs.python.org/2/library/socket.html sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # log that we are building a server @@ -28,14 +28,8 @@ def server(log_buffer=sys.stderr): print >>log_buffer, 'connection - {0}:{1}'.format(*addr) while True: - data = conn.recv(16) print >>log_buffer, 'received "{0}"'.format(data) - - # TODO: you will need to check here to see if any data was - # received. If so, send the data you got back to - # the client. If not, exit the inner loop and wait - # for a new connection from a client if not data: break @@ -44,8 +38,6 @@ def server(log_buffer=sys.stderr): finally: conn.close() print >>log_buffer, 'closing socket' - - except KeyboardInterrupt: sock.close()