diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc new file mode 100644 index 0000000..7660a16 Binary files /dev/null and b/__pycache__/echo_client.cpython-36.pyc differ diff --git a/__pycache__/tests.cpython-36-PYTEST.pyc b/__pycache__/tests.cpython-36-PYTEST.pyc new file mode 100644 index 0000000..5a2ee82 Binary files /dev/null and b/__pycache__/tests.cpython-36-PYTEST.pyc differ diff --git a/demo_client.py b/demo_client.py old mode 100644 new mode 100755 diff --git a/demo_client_server_behavior.mp4 b/demo_client_server_behavior.mp4 old mode 100644 new mode 100755 diff --git a/demo_server.py b/demo_server.py old mode 100644 new mode 100755 diff --git a/echo_client.py b/echo_client.py old mode 100644 new mode 100755 index a2e9b29..5a360e9 --- a/echo_client.py +++ b/echo_client.py @@ -4,12 +4,15 @@ def client(msg, log_buffer=sys.stderr): - server_address = ('localhost', 10000) + server_address = ('127.0.0.1', 20000) # 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(family=socket.AF_INET, + type=socket.SOCK_STREAM, + proto=socket.IPPROTO_TCP) + 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 @@ -20,7 +23,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. - + print(msg) + sock.sendall(bytes(msg, 'utf8')) # 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,8 +32,9 @@ 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 = '' + chunk = sock.recv(16) print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) + received_message += chunk.decode('utf8') except Exception as e: traceback.print_exc() sys.exit(1) @@ -37,9 +42,10 @@ def client(msg, log_buffer=sys.stderr): # 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__': diff --git a/echo_server.py b/echo_server.py old mode 100644 new mode 100755 index 299f009..eda5943 --- a/echo_server.py +++ b/echo_server.py @@ -5,10 +5,12 @@ def server(log_buffer=sys.stderr): # set an address for our server - address = ('127.0.0.1', 10000) + address = ('127.0.0.1', 20000) # 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) # 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 @@ -21,6 +23,9 @@ def server(log_buffer=sys.stderr): # TODO: bind your new sock 'sock' to the address above and begin to listen # for incoming connections + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(address) + sock.listen(1) try: # the outer loop controls the creation of new connection sockets. The @@ -33,7 +38,7 @@ 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 = sock.accept() try: print('connection - {0}:{1}'.format(*addr), file=log_buffer) @@ -46,18 +51,21 @@ 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 = b'' - print('received "{0}"'.format(data.decode('utf8'))) + data = conn.recv(16) # 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'))) + if data: + conn.sendall(data) + print('sent "{0}"'.format(data.decode('utf8')), file=log_buffer) + else: + break # TODO: Check here to see whether you have received the end # of the message. If you have, then break from the `while True` # loop. - # + # Figuring out whether or not you have received the end of the # message is a trick we learned in the lesson: if you don't # remember then ask your classmates or instructor for a clue. @@ -69,6 +77,7 @@ def server(log_buffer=sys.stderr): # 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. + sock.close() print( 'echo complete, client connection closed', file=log_buffer ) @@ -78,8 +87,9 @@ def server(log_buffer=sys.stderr): # 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 print('quitting echo server', file=log_buffer) + sock.close() + return if __name__ == '__main__': diff --git a/tests.py b/tests.py old mode 100644 new mode 100755