diff --git a/echo_client.py b/echo_client.py index 6b2f047..4be43b8 100644 --- a/echo_client.py +++ b/echo_client.py @@ -1,15 +1,15 @@ import socket import sys - def client(msg, log_buffer=sys.stderr): - server_address = ('localhost', 10000) + server_address = ('localhost', 20001) # 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) + sock = socket.socket() print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) # TODO: connect your socket to the server here. - + sock.connect(server_address) # you can use this variable to accumulate the entire message received back # from the server received_message = '' @@ -19,7 +19,8 @@ def client(msg, log_buffer=sys.stderr): try: print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. - + sock.sendall(msg.encode('utf-8')) +# time.sleep(5) # TODO: the server should be sending you back your message as a series # of 16-byte chunks. Accumulate the chunks you get to build the # entire reply from the server. Make sure that you have received @@ -28,15 +29,24 @@ def client(msg, log_buffer=sys.stderr): # Log each chunk you receive. Use the print statement below to # do it. This will help in debugging problems chunk = '' - print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) + chunks = [] + bytes_recd = 0 + while bytes_recd < len(msg): + chunk = sock.recv(2048) + chunks.append(chunk) + bytes_recd = bytes_recd + len(chunk) + + received_message = b''.join(chunks).decode('utf8') + print('received "{0}"'.format(received_message), file=log_buffer) finally: # TODO: after you break out of the loop receiving echoed chunks from # the server you will want to close your client socket. print('closing socket', file=log_buffer) + sock.close() # TODO: when all is said and done, you should return the entire reply # you received from the server as the return value of this function. - + return received_message if __name__ == '__main__': if len(sys.argv) != 2: diff --git a/echo_server.py b/echo_server.py index 44f853a..04dfd3d 100644 --- a/echo_server.py +++ b/echo_server.py @@ -4,23 +4,24 @@ def server(log_buffer=sys.stderr): # set an address for our server - address = ('127.0.0.1', 10000) + address = ('localhost', 20001) # 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_TCP) + sock = socket.socket() # TODO: You may find that if you repeatedly run the server script it fails, # claiming that the port is already used. You can set an option on # your socket that will fix this problem. We DID NOT talk about this # in class. Find the correct option by reading the very end of the # socket library documentation: # http://docs.python.org/3/library/socket.html#example - + #sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # log that we are building a server print("making a server on {0}:{1}".format(*address), file=log_buffer) - # TODO: bind your new sock 'sock' to the address above and begin to listen # for incoming connections - + sock.bind(address) + sock.listen(5) try: # the outer loop controls the creation of new connection sockets. The # server will handle each incoming connection one at a time. @@ -32,7 +33,9 @@ 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 - conn, addr = ('foo', ('bar', 'baz')) +# conn, addr = ('foo', ('bar', 'baz')) + conn, addr = sock.accept() +# conn.run() try: print('connection - {0}:{1}'.format(*addr), file=log_buffer) @@ -46,13 +49,15 @@ def server(log_buffer=sys.stderr): # a placeholder to prevent an error in string # formatting data = b'' - print('received "{0}"'.format(data.decode('utf8'))) + partial = conn.recv(16) + data += partial + print('Received: "{0}"'.format(data.decode('utf8'))) # TODO: Send the data you received back to the client, log # the fact using the print statement here. It will help in # debugging problems. - print('sent "{0}"'.format(data.decode('utf8'))) - + conn.sendall(data) + print('Sent: "{0}"'.format(data.decode('utf8'))) # TODO: Check here to see whether you have received the end # of the message. If you have, then break from the `while True` # loop. @@ -61,21 +66,23 @@ def server(log_buffer=sys.stderr): # message is a trick we learned in the lesson: if you don't # remember then ask your classmates or instructor for a clue. # :) - + if len(data) == 0: + break 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. print( - 'echo complete, client connection closed', file=log_buffer + 'Echo complete, client connection closed.', file=log_buffer ) + conn.close() except KeyboardInterrupt: # TODO: Use the python KeyboardInterrupt 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 + raise print('quitting echo server', file=log_buffer)