44
55def server (log_buffer = sys .stderr ):
66 # set an address for our server
7- address = ('127.0.0.1 ' , 10000 )
7+ address = ('localhost ' , 20001 )
88 # TODO: Replace the following line with your code which will instantiate
99 # a TCP socket with IPv4 Addressing, call the socket you make 'sock'
10- sock = None
10+ #sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
11+ sock = socket .socket ()
1112 # TODO: You may find that if you repeatedly run the server script it fails,
1213 # claiming that the port is already used. You can set an option on
1314 # your socket that will fix this problem. We DID NOT talk about this
1415 # in class. Find the correct option by reading the very end of the
1516 # socket library documentation:
1617 # http://docs.python.org/3/library/socket.html#example
17-
18+ #sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1819 # log that we are building a server
1920 print ("making a server on {0}:{1}" .format (* address ), file = log_buffer )
20-
2121 # TODO: bind your new sock 'sock' to the address above and begin to listen
2222 # for incoming connections
23-
23+ sock .bind (address )
24+ sock .listen (5 )
2425 try :
2526 # the outer loop controls the creation of new connection sockets. The
2627 # server will handle each incoming connection one at a time.
@@ -32,7 +33,9 @@ def server(log_buffer=sys.stderr):
3233 # the client so we can report it below. Replace the
3334 # following line with your code. It is only here to prevent
3435 # syntax errors
35- conn , addr = ('foo' , ('bar' , 'baz' ))
36+ # conn, addr = ('foo', ('bar', 'baz'))
37+ conn , addr = sock .accept ()
38+ # conn.run()
3639 try :
3740 print ('connection - {0}:{1}' .format (* addr ), file = log_buffer )
3841
@@ -46,13 +49,15 @@ def server(log_buffer=sys.stderr):
4649 # a placeholder to prevent an error in string
4750 # formatting
4851 data = b''
49- print ('received "{0}"' .format (data .decode ('utf8' )))
52+ partial = conn .recv (16 )
53+ data += partial
54+ print ('Received: "{0}"' .format (data .decode ('utf8' )))
5055
5156 # TODO: Send the data you received back to the client, log
5257 # the fact using the print statement here. It will help in
5358 # debugging problems.
54- print ( 'sent "{0}"' . format (data . decode ( 'utf8' )) )
55-
59+ conn . sendall (data )
60+ print ( 'Sent: "{0}"' . format ( data . decode ( 'utf8' )))
5661 # TODO: Check here to see whether you have received the end
5762 # of the message. If you have, then break from the `while True`
5863 # loop.
@@ -61,21 +66,23 @@ def server(log_buffer=sys.stderr):
6166 # message is a trick we learned in the lesson: if you don't
6267 # remember then ask your classmates or instructor for a clue.
6368 # :)
64-
69+ if len (data ) == 0 :
70+ break
6571 finally :
6672 # TODO: When the inner loop exits, this 'finally' clause will
6773 # be hit. Use that opportunity to close the socket you
6874 # created above when a client connected.
6975 print (
70- 'echo complete, client connection closed' , file = log_buffer
76+ 'Echo complete, client connection closed. ' , file = log_buffer
7177 )
78+ conn .close ()
7279
7380 except KeyboardInterrupt :
7481 # TODO: Use the python KeyboardInterrupt exception as a signal to
7582 # close the server socket and exit from the server function.
7683 # Replace the call to `pass` below, which is only there to
7784 # prevent syntax problems
78- pass
85+ raise
7986 print ('quitting echo server' , file = log_buffer )
8087
8188
0 commit comments