diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc new file mode 100644 index 0000000..e6d0451 Binary files /dev/null and b/__pycache__/echo_client.cpython-36.pyc differ diff --git a/echo_client.py b/echo_client.py index 6b2f047..6344f1e 100644 --- a/echo_client.py +++ b/echo_client.py @@ -6,37 +6,25 @@ 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) print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) - # TODO: connect your socket to the server here. - - # you can use this variable to accumulate the entire message received back - # from the server + sock.connect(server_address) received_message = '' - - # this try/finally block exists purely to allow us to close the socket - # when we are finished with it try: print('sending "{0}"'.format(msg), file=log_buffer) - # 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. Accumulate the chunks you get to build the - # entire reply from the server. Make sure that you have received - # the entire message and then you can break the loop. - # - # 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) + sock.sendall(msg.encode('utf8')) + while True: + chunk = sock.recv(16) + received_message = received_message + chunk.decode('utf8') + print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) + if len(chunk) < 16: + print("Reached EOM") + 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('closing socket', file=log_buffer) - - # 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. - + sock.close() + print('Closing Socket', file=log_buffer) + print("Message Recieved: {}".format(received_message)) + return received_message if __name__ == '__main__': if len(sys.argv) != 2: @@ -46,3 +34,4 @@ def client(msg, log_buffer=sys.stderr): msg = sys.argv[1] client(msg) +#client('hello my name is dan and I work in a button factory') \ No newline at end of file diff --git a/echo_server.py b/echo_server.py index 44f853a..2881eaa 100644 --- a/echo_server.py +++ b/echo_server.py @@ -7,76 +7,44 @@ 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 - # 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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 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 - + try: + sock.bind(address) + except socket.error: # aborts program if there is an error + print("Unable to create socket.") + sock.close() + exit() + + sock.listen(1) + print("Socket is listening for connections.") 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('waiting for a connection', file=log_buffer) - - # 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 - conn, addr = ('foo', ('bar', 'baz')) + print('Waiting for a connection...', file=log_buffer) + conn, addr = sock.accept() try: - print('connection - {0}:{1}'.format(*addr), file=log_buffer) - - # the inner loop will receive messages sent by the client in - # buffers. When a complete message has been received, the - # loop will exit + print('Connection - {0}:{1}'.format(*addr), file=log_buffer) 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 = b'' + data = conn.recv(16) 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. + 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. - # - # 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. - # :) - + if len(data) < 16: + print("Reached EOM") + break + sock.close() 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 ) - 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 - print('quitting echo server', file=log_buffer) + sock.close() + print() + print('User Inturrupt: Closing Socket.', file=log_buffer) if __name__ == '__main__': diff --git a/echo_server_clone.py b/echo_server_clone.py new file mode 100644 index 0000000..72a67b3 --- /dev/null +++ b/echo_server_clone.py @@ -0,0 +1,91 @@ +import socket +import sys + + +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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + # 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 + + # log that we are building a server + print("making a server on {0}:{1}".format(*address), file=log_buffer) + sock.bind(address) + sock.listen(3) + # TODO: bind your new sock 'sock' to the address above and begin to listen + # for incoming connections + + 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('waiting for a connection', file=log_buffer) + + # 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 + conn, addr = sock.accept() + try: + print('connection - {0}:{1}'.format(*addr), file=log_buffer) + + # 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 = conn.recv(16) #b'' + if not data: + print("No data was received") + break + + print('received "{0}"'.format(data.decode('utf8'))) + conn.sendall(data, file=log_buffer) # I am guessing on that log buffer... + # 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'))) + + # 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. + # :) + + finally: + sock.close() + # 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 + ) + + 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 + sock.close() + print('quitting echo server', file=log_buffer) + + +if __name__ == '__main__': + server() + sys.exit(0)