From 919c3f4c803e4a85c1ffc07610287af10fd2e880 Mon Sep 17 00:00:00 2001 From: Jim Blackburn Date: Mon, 16 Jul 2018 22:37:32 -0700 Subject: [PATCH 1/5] Added more functional code, including kludge to fix "Address already in use" error. Tests still don't pass --- README.md | 0 __pycache__/echo_client.cpython-36.pyc | Bin 0 -> 1169 bytes __pycache__/tests.cpython-36-PYTEST.pyc | Bin 0 -> 1780 bytes demo_client.py | 0 demo_client_server_behavior.mp4 | Bin demo_server.py | 0 echo_client.py | 19 +++++++++++++------ echo_server.py | 22 +++++++++++++++------- tests.py | 0 9 files changed, 28 insertions(+), 13 deletions(-) mode change 100644 => 100755 README.md create mode 100644 __pycache__/echo_client.cpython-36.pyc create mode 100644 __pycache__/tests.cpython-36-PYTEST.pyc mode change 100644 => 100755 demo_client.py mode change 100644 => 100755 demo_client_server_behavior.mp4 mode change 100644 => 100755 demo_server.py mode change 100644 => 100755 echo_client.py mode change 100644 => 100755 echo_server.py mode change 100644 => 100755 tests.py 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 0000000000000000000000000000000000000000..4417d91e9a766341a8b3c22a3dbb37db8fe62824 GIT binary patch literal 1169 zcmYjQ&2HO95Z)!1zxt`;^pFHCO1KSB9b&~vffPki#E?}WXk!VM1H0^jpt&Q7l1K{6 zm0>ATQ9!+>@6mVZ6YRAo^$~LFtdzD|ay|~{XTOwyzsTNVsJ}!h za|BK}ose+%q#@nD8=(PoWcDdHxcM6m8CaZ+4KV^6?arEVYix>ZZig;-;ObcJyfY|4 z&FAi#ZZ(Iy4@p=71{H1Lv_~4=206)jV(14h2jwA33#I%Uf!9`sBqM@CQM7Jy^PRae zCF5*lEsfvF68SaT(j3|7cTikzug$H+TN;`Teg014&WbG-^Oug$~pZ$dUf8uRFz(*^+VLVyl9`kR+U%X z?nURa6J55ty2vz_Sa~6`SSr5+sgsK->b=g7kxs-7s=ZEh(SFlwpPrpvD0Y+PTsa>a zQw&SIRW^sD;Gn$rT>?{?7P<1Ih!ePplduA&2h^njt5Vk#k1_|Vpo-=ISOUcyCaR7F+mpJ;f5fCnr&2=O0V9%m3m%A7#1SS$v&%Gy#2 u{RxRrHGHN)PiPOmz*psdn-zREf$#K3SbmJ)Vmq!`KXz#yTdrf%KK%y|qBW!d literal 0 HcmV?d00001 diff --git a/__pycache__/tests.cpython-36-PYTEST.pyc b/__pycache__/tests.cpython-36-PYTEST.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a9e3e806745b436d0e45e6e5422ac95b43f1019d GIT binary patch literal 1780 zcmb_dUymC#5VyU%$tKMeIPTisn^h_xQtv_q>H{j}N_8M4bgH1`N_bf9t(|0Z8*i|k zgKQIC(l5hT@+%TjzXDIpcymX}74d>4|JfOj=QqC@dq3XVYX5QTn;*|3LVhP3Erj(U zbn`t7f(TlWKJ_yj2jj3G!I~9~NweRiwLb30Uf=4sz9J$J;Y%Vy*&4T3LI0ZUi0F*z zZ{d4uMZki7N4kwya5g#aQrF0eTq>L4_eEo{ID~FKgh6`meI{r>5KIIwNk0^!h+vLH zLo{J-h#1anxz15GDxXSYAE!oEcPy+7AC#Kgk>nC|+(>;cHBXh`Yrp6V8?Zp$hW;43 zDPWj)ELqSMU9uCR`XRA&%+A_+K*=(oq!vz_Ki*Ou)w6Ov!O4JU@ zL@GPT?yO2C)$i8Ll1Nr`6tO z^`15SYWFe+K3Bui@)!4($K61`3wYeM)Ij+5=%-m) zXp9vU9aO{@cCL|T7PZK2J#6EKn}{#*&Mvr0kj`76GdaK1d|A#kH(9BrzYa#8OU36h)rP0TlB0Hz!tH=> zkqd6@Y%ur_ru7amvdOUCMZpam{=w`GPY*(Vjo90;^(wLNT_yI4<#D&^;v`Y&L?(%A zC&{D~vjW$hB*DzDHO(XuWtJqqH1th`hS}V}=aFLi^+jo$7`|c}viPmI?WO2ylIAK& zy4PKNI?D^2E8{wc(|L0FwU@xHgaSIK>n|_WftEk(+)CHF6uIVwtPRGVl~lvu0jeyi zpEduKXncoVJX1M5hX!7ar!eAk?&0V?)P{)YO@X-hLCwZPytL*zMngKJLt`2T+aJXA EFMp}##{d8T literal 0 HcmV?d00001 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..4418f1b --- a/echo_client.py +++ b/echo_client.py @@ -4,12 +4,17 @@ 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.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(server_address) + sock.connect(server_address) # you can use this variable to accumulate the entire message received back # from the server @@ -20,7 +25,7 @@ def client(msg, log_buffer=sys.stderr): 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 @@ -28,8 +33,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 except Exception as e: traceback.print_exc() sys.exit(1) @@ -37,9 +43,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..469af1d --- 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,19 @@ 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'' + 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. - print('sent "{0}"'.format(data.decode('utf8'))) + conn.sendall(data) + print('sent "{0}"'.format(data.decode('utf8')), file=log_buffer) # 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 +75,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 +85,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 From 456b9fa3ccd33e5ce92954da44d967a507cb8d54 Mon Sep 17 00:00:00 2001 From: Jim Blackburn Date: Mon, 16 Jul 2018 23:10:34 -0700 Subject: [PATCH 2/5] Made some improvements and cleaned out some bad code. --- __pycache__/echo_client.cpython-36.pyc | Bin 1169 -> 1169 bytes __pycache__/tests.cpython-36-PYTEST.pyc | Bin 1780 -> 1780 bytes echo_client.py | 5 ++--- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc index 4417d91e9a766341a8b3c22a3dbb37db8fe62824..4b7364a1adf40ddd561004e410e58c5fdd1d156a 100644 GIT binary patch delta 15 WcmbQpIgyjin3tDpV$(*pZWaI@7z677 delta 15 WcmbQpIgyjin3tE!xnUz)Hwyq9Is=;k diff --git a/__pycache__/tests.cpython-36-PYTEST.pyc b/__pycache__/tests.cpython-36-PYTEST.pyc index a9e3e806745b436d0e45e6e5422ac95b43f1019d..92445e771a6096c1e040b4f75a78985264739526 100644 GIT binary patch delta 16 Xcmeyu`-PX?n3tF9N0aYH_P1;RG42J^ delta 16 Xcmeyu`-PX?n3tF9nS{?q_P1;RF7E|a diff --git a/echo_client.py b/echo_client.py index 4418f1b..14a4019 100755 --- a/echo_client.py +++ b/echo_client.py @@ -12,8 +12,6 @@ def client(msg, log_buffer=sys.stderr): proto=socket.IPPROTO_TCP) print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - sock.bind(server_address) sock.connect(server_address) # you can use this variable to accumulate the entire message received back @@ -25,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(msg.decode()) # 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 From 5d750d5ac699ced2ac9835ce8ceae5e0bb7cd1dd Mon Sep 17 00:00:00 2001 From: Jim Blackburn Date: Mon, 16 Jul 2018 23:15:35 -0700 Subject: [PATCH 3/5] Client now gets response from server, but server still spins. --- echo_client.py | 4 ++-- echo_server.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/echo_client.py b/echo_client.py index 14a4019..5a360e9 100755 --- a/echo_client.py +++ b/echo_client.py @@ -24,7 +24,7 @@ def client(msg, log_buffer=sys.stderr): print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. print(msg) - sock.sendall(msg.decode()) + 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 @@ -34,7 +34,7 @@ def client(msg, log_buffer=sys.stderr): # do it. This will help in debugging problems chunk = sock.recv(16) print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) - received_message += chunk + received_message += chunk.decode('utf8') except Exception as e: traceback.print_exc() sys.exit(1) diff --git a/echo_server.py b/echo_server.py index 469af1d..33ec57a 100755 --- a/echo_server.py +++ b/echo_server.py @@ -63,7 +63,8 @@ def server(log_buffer=sys.stderr): # TODO: Check here to see whether you have received the end # of the message. If you have, then break from the `while True` # loop. - + if data is None: + break # 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. From 61c6dc9cfdc36c7975b2faaa8fdc098fe0b271b4 Mon Sep 17 00:00:00 2001 From: Jim Blackburn Date: Mon, 16 Jul 2018 23:21:37 -0700 Subject: [PATCH 4/5] Server script no longer spins, but still not receiving long messages --- __pycache__/echo_client.cpython-36.pyc | Bin 1169 -> 1143 bytes __pycache__/tests.cpython-36-PYTEST.pyc | Bin 1780 -> 1780 bytes echo_server.py | 10 ++++++---- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc index 4b7364a1adf40ddd561004e410e58c5fdd1d156a..7660a166bce155889af676fc5a515e3479ec9ad1 100644 GIT binary patch delta 384 zcmY*SO-sW-5S>Xj&32R2*i)#~eu!WXf>KdI{0ZVs4}zfGv^16o=_)iPc*v;-4`na? z2_C$8(jVau$Ui7}*P|On#DRIkdo%CNto%V)*Kyd(^yuu9&e$Wq&z`&YiIyP(FQp`& zPtFD5W?&?cVS?Ov&=3SE7(${4rl1LMnteMIFY|JSsi8bfd5nOm9sW5J@~iDZN=_l% zAt0)C4|@$c3?Rfs3(XSF~j%7#|QWqGI_cxLZ94E>(S%_8AqG|G+3iSWnrNtf+z!v(Lkb!gBoE{(ggf7IFQIKhLRN0UQMiROzh;~ zP`f!fy89YFLEivhz|{*ZdXn#VzI^8!&W{`_q@igfeCk}~l#qPDclgkL=2_ZY2^0i~Wz1EYh^A17Z1e^fJ2OBC#mLnh!=fYWrc!;__N%iD zPu;X_42;p8L6>2SZ`p7!S{F)?iw=bSCUFerOa|l0l*Oc{pZ0tD@k#qER(pEC+dl8L zk6Nv6jIT!)jgy0kWf=n&y$a8;5f#PZS{4Wp%m=cBKps@cfD9F^f-Ks#=v~yyGJm74 r+A3dmagG8 Date: Mon, 16 Jul 2018 23:29:56 -0700 Subject: [PATCH 5/5] Final commit for this moment. Client now sends data and receives it back just fine, but not longer messages. Will look more into this tomorrow. --- echo_server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/echo_server.py b/echo_server.py index 01f30e0..eda5943 100755 --- a/echo_server.py +++ b/echo_server.py @@ -52,7 +52,6 @@ def server(log_buffer=sys.stderr): # a placeholder to prevent an error in string # formatting 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