Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: The purpose of this exercise is to re-enforce the concepts introduc

ID: 3638470 • Letter: D

Question

Description:

The purpose of this exercise is to re-enforce the concepts introduced by your author in chapter 3 and by your instructor in class regarding the operation of TCP and connection oriented communication between processes (in this case, client and server). The exercise focuses on example 3.2 (and to some extent example 3.1) in your text and the alterations to it suggested in class.

Do the following:

There should be another part to this ... but I'm having trouble getting consistent behavior and I don't want you to be wandering around in the dark on this. Basically, what I want is for your client not to read ... and then capture the traffic on wireshark and interpret what you see. I'll have to refine the story a little.  

#!/usr/bin/env python
# Foundations of Python Network Programming - Chapter 3 - tcp_deadlock.py
# TCP client and server that leave too much data waiting
'''
Importing sysand socket. The expression creates the INET, streaming socket.
'''
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

'''Giving the host address and the port number'''
HOST = '127.0.0.1'
PORT = 1060

'''The if block '''
if sys.argv[1:] == ['server']:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)

while True:
print 'Listening at', s.getsockname()
sc, sockname = s.accept() # accept connection from outside
print 'Processing up to 1024 bytes at a time from', sockname
n = 0
while True:
message = sc.recv(1024)
if not message:
break
sc.sendall(message.upper()) # send it back uppercase
n += len(message)
print ' %d bytes processed so far' % (n,),
sys.stdout.flush()
print
sc.close()
print 'Completed processing'

elif len(sys.argv) == 3 and sys.argv[1] == 'client' and sys.argv[2].isdigit():

bytes = (int(sys.argv[2]) + 15) // 16 * 16 # round up to // 16
message = 'capitalize this!' # 16-byte message to repeat over and over

print 'Sending', bytes, 'bytes of data, in chunks of 16 bytes'
s.connect((HOST, PORT))

sent = 0
while sent < bytes:
s.sendall(message)
sent += len(message)
print ' %d bytes sent' % (sent,),
sys.stdout.flush()

print
s.shutdown(socket.SHUT_WR)

print 'Receiving all the data the server sends back'

received = 0
while True:
data = s.recv(42)
if not received:
print 'The first data received says', repr(data)
received += len(data)
if not data:
break
print ' %d bytes received' % (received,),

s.close()

else:
print >>sys.stderr, 'usage: tcp_deadlock.py server | client '

Explanation / Answer

import socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) '''Giving the host address and the port number''' HOST = '127.0.0.1' PORT = 1060 '''The if block ''' if sys.argv[1:] == ['server']: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((HOST, PORT)) s.listen(1) while True: print 'Listening at', s.getsockname() sc, sockname = s.accept() # accept connection from outside print 'Processing up to 1024 bytes at a time from', sockname n = 0 while True: message = sc.recv(1024) if not message: break sc.sendall(message.upper()) # send it back uppercase n += len(message) print ' %d bytes processed so far' % (n,), sys.stdout.flush() print sc.close() print 'Completed processing' elif len(sys.argv) == 3 and sys.argv[1] == 'client' and sys.argv[2].isdigit(): bytes = (int(sys.argv[2]) + 15) // 16 * 16 # round up to // 16 message = 'capitalize this!' # 16-byte message to repeat over and over print 'Sending', bytes, 'bytes of data, in chunks of 16 bytes' s.connect((HOST, PORT)) sent = 0 while sent >sys.stderr, 'usage: tcp_deadlock.py server | client '