From f740209cc02c31a2210b3978e8ec064610cbb54c Mon Sep 17 00:00:00 2001 From: duane-wright Date: Sun, 12 Jan 2014 23:13:47 -0800 Subject: [PATCH 1/2] Update echo_client.py --- assignments/session01/echo_client.py | 37 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/assignments/session01/echo_client.py b/assignments/session01/echo_client.py index 61616c36..c30bed69 100644 --- a/assignments/session01/echo_client.py +++ b/assignments/session01/echo_client.py @@ -4,17 +4,31 @@ def client(msg, log_buffer=sys.stderr): server_address = ('localhost', 10000) + + # 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 + + # 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. + + sock.sendall(msg) # 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 @@ -24,13 +38,26 @@ def client(msg, log_buffer=sys.stderr): # # 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) + + # chunk = '' + + while True: + + chunk = sock.recv(16) + + print >>log_buffer, 'received "{0}"'.format(chunk) + + if len(chunk) == 0: + break + finally: # TODO: after you break out of the loop receiving echoed chunks from # the server you will want to close your client socket. print >>log_buffer, 'closing socket' - + + sock.close() + + sys.exit(1) if __name__ == '__main__': if len(sys.argv) != 2: @@ -39,4 +66,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) From 3a3a667db7790ce9ce1aae9b875b389f8a9e7d20 Mon Sep 17 00:00:00 2001 From: duane-wright Date: Sun, 12 Jan 2014 23:14:59 -0800 Subject: [PATCH 2/2] Update echo_server.py --- assignments/session01/echo_server.py | 46 ++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/assignments/session01/echo_server.py b/assignments/session01/echo_server.py index 217380fb..1fd80513 100644 --- a/assignments/session01/echo_server.py +++ b/assignments/session01/echo_server.py @@ -7,16 +7,28 @@ def server(log_buffer=sys.stderr): 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 + + # sock = None + + sock = socket.socket( + socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_IP) + # 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.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. @@ -28,7 +40,11 @@ def server(log_buffer=sys.stderr): # 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') + + # addr = ('bar', 'baz') + + conn, addr = sock.accept() + try: print >>log_buffer, 'connection - {0}:{1}'.format(*addr) @@ -41,12 +57,24 @@ def server(log_buffer=sys.stderr): # following line with your code. It's only here as # a placeholder to prevent an error in string # formatting - data = '' + + # 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 len(data) > 0: + conn.sendall(data) + + else: + break + finally: # TODO: When the inner loop exits, this 'finally' clause will @@ -54,16 +82,22 @@ def server(log_buffer=sys.stderr): # created above when a client connected. Replace the # call to `pass` below, which is only there to prevent # syntax problems - pass + + # pass + + conn.close() 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 + + # pass + + sock.close() if __name__ == '__main__': server() - sys.exit(0) \ No newline at end of file + sys.exit(0)