def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socker Echo Server ') parser.add_argument('-l', '--localport', help='local sock352 UDP port', required=True) args = vars(parser.parse_args()) local_port = int(args['localport']) serverSock = sock352.socket() serverSock.bind(('', local_port)) # forever waiting for message while True: #Read from UDP socket into message, client address message, clientAddress = serverSock.recvfrom(MAX_SIZE) serverSock.sendto(message, clientAddress)
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Client') parser.add_argument('-f', '--filename', help='File to Send', required=False) parser.add_argument('-d', '--destination', help='Destination IP Host', required=True) parser.add_argument('-p', '--port', help='remote sock352 port', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) # get the arguments into local variables args = vars(parser.parse_args()) filename = args['filename'] destination = args['destination'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 1111 # open the file for reading if (filename): try: filesize = os.path.getsize(filename) fd = open(filename, "rb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the client uses for the underlying UDP # sockets. If we are running the client and # server on the same machine, these ports # need to be different. If they are running on # different machines, we can re-use the same # ports. if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) s = sock352.socket() s.connect((destination, port)) longPacker = struct.Struct("!L") fileLenPacked = longPacker.pack(filesize) s.send(fileLenPacked) bytes_to_send = filesize start_stamp = time.clock() file_contents = fd.read() totalsent = 0 # make sure we sent the whole fragment totalsent = s.send(file_contents) if (totalsent == 0): raise RuntimeError("socket broken") end_stamp = time.clock() lapsed_seconds = end_stamp - start_stamp if (lapsed_seconds > 0.0): print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize / lapsed_seconds) / (1024 * 1024))) else: print("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds)) fd.close() s.close()
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Server') parser.add_argument('-f', '--filename', help='Filename to Receiver', required=False) parser.add_argument('-p', '--port', help='CS 352 Socket Port (optional for part 1)', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) args = vars(parser.parse_args()) # open the file for writing filename = args['filename'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 1111 if (filename): try: fd = open(filename, "wb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the server uses for the underlying UDP # sockets. If we are running the client and # server on different machines, these ports # need to be different, otherwise we can # use the same ports if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) s = sock352.socket() # set the fragment size we will read on # FRAGMENTSIZE = 4096 # binding the host to empty allows reception on # all network interfaces s.bind(('', port)) s.listen(5) # when accept returns, the client is connected (s2, address) = s.accept() # this receives the size of the file # as a 4 byte integer in network byte order (big endian) longPacker = struct.Struct("!L") long = s2.recv(4) fn = longPacker.unpack(long) filelen = fn[0] #print("Filelen: ", filelen) # # the MD5 computes a unique hash for all the data # mdhash = md5.new() # bytes_to_receive = filelen start_stamp = time.clock() # main loop to receive the data from the client # while (bytes_to_receive > 0): # if (bytes_to_receive >= FRAGMENTSIZE): # fragment = s2.recv(FRAGMENTSIZE) # else: # fragment = s2.recv(bytes_to_receive) # mdhash.update(fragment) # bytes_to_receive = bytes_to_receive - len(fragment) # fd.write(fragment) file = s2.recv(filelen) # mdhash.update(file) fd.write(file) end_stamp = time.clock() lapsed_seconds = end_stamp - start_stamp # finish computing the MD5 hash # local_digest = mdhash.digest() # receive the size of the remote hash # dl = longPacker.unpack(s2.recv(4)) # digestlen = dl[0] # remote_digest = s2.recv(digestlen) # # check is the size matches # if (len(remote_digest) != digestlen): # raise RuntimeError("socket error") # # compare the two digests, byte for byte # for i, server_byte in enumerate(local_digest): # client_byte = remote_digest[i] # if (client_byte != server_byte): # print( "digest failed at byte %d %c %c " % (i,client_byte,server_byte)) if (lapsed_seconds > 0.0): print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " % (filelen, lapsed_seconds, (filelen / lapsed_seconds) / (1024 * 1024))) else: print("server1: received %d bytes in %d seconds, inf MB/s " % (filelen, lapsed_seconds)) fd.close() s2.close()
import sock352 import time sock352.init(0, 0) s = sock352.socket() s.connect(('127.0.0.1', 12467)) print(time.time())
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Server') parser.add_argument('-f', '--filename', help='Filename to Receiver', required=False) parser.add_argument('-p', '--port', help='CS 352 Socket Port (optional for part 1)', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) args = vars(parser.parse_args()) # open the file for writing filename = args['filename'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 1111 if (filename): try: fd = open(filename, "wb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the server uses for the underlying UDP # sockets. If we are running the client and # server on different machines, these ports # need to be different, otherwise we can # use the same ports if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) s = sock352.socket() # binding the host to empty allows reception on # all network interfaces s.bind(('', port)) s.listen(5) # when accept returns, the client is connected (s2, address) = s.accept() # this receives the size of the file # as a 4 byte integer in network byte order (big endian) longPacker = struct.Struct("!L") long = s2.recv(4) fn = longPacker.unpack(long) filelen = fn[0] start_stamp = time.clock() file = s2.recv(filelen) fd.write(file) end_stamp = time.clock() lapsed_seconds = end_stamp - start_stamp if (lapsed_seconds > 0.0): print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " % (filelen, lapsed_seconds, (filelen / lapsed_seconds) / (1024 * 1024))) else: print("server1: received %d bytes in %d seconds, inf MB/s " % (filelen, lapsed_seconds)) fd.close() s2.close()
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Client') parser.add_argument('-f', '--filename', help='File to Send', required=False) parser.add_argument('-d', '--destination', help='Destination IP Host', required=True) parser.add_argument('-p', '--port', help='remote sock352 port', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) # get the arguments into local variables args = vars(parser.parse_args()) filename = args['filename'] destination = args['destination'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 1300 # open the file for reading if (filename): try: filesize = os.path.getsize(filename) fd = open(filename, "rb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the client uses for the underlying UDP # sockets. If we are running the client and # server on the same machine, these ports # need to be different. If they are running on # different machines, we can re-use the same # ports. if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) # create a socket and connect to the remote server s = sock352.socket() s.connect((destination, port)) # send the size of the file as a 4 byte integer # to the server, so it knows how much to read FRAGMENTSIZE = 8192 longPacker = struct.Struct("!L") fileLenPacked = longPacker.pack(filesize) s.send(fileLenPacked) # use the MD5 hash algorithm to validate all the data is correct mdhash = md5.new() # loop for the size of the file, sending the fragments bytes_to_send = filesize start_stamp = time.clock() while (bytes_to_send > 0): fragment = fd.read(FRAGMENTSIZE) mdhash.update(fragment) totalsent = 0 # make sure we sent the whole fragment while (totalsent < len(fragment)): sent = s.send(fragment[totalsent:]) if (sent == 0): raise RuntimeError("socket broken") totalsent = totalsent + sent bytes_to_send = bytes_to_send - len(fragment) end_stamp = time.clock() lapsed_seconds = end_stamp - start_stamp # this part send the lenght of the digest, then the # digest. It will be check on the server digest = mdhash.digest() # send the length of the digest long = len(digest) digestLenPacked = longPacker.pack(long) sent = s.send(digestLenPacked) if (sent != 4): raise RuntimeError("socket broken") # send the digest sent = s.send(digest) if (sent != len(digest)): raise RuntimeError("socket broken") if (lapsed_seconds > 0.0): print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize / lapsed_seconds) / (1024 * 1024))) else: print("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds)) fd.close() s.close()
def main(): # parse all the arguments to the client global fd parser = argparse.ArgumentParser(description='CS 352 Socket Server') parser.add_argument('-f', '--filename', help='Filename to Receiver', required=False) parser.add_argument('-p', '--port', help='CS 352 Socket Port (optional for part 1)', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) args = vars(parser.parse_args()) # open the file for writing filename = args['filename'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 1111 if (filename): try: fd = open(filename, "wb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the server uses for the underlying UDP # sockets. If we are running the client and # server on different machines, these ports # need to be different, otherwise we can # use the same ports if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) s = sock352.socket() # set the maximum fragment size we will read on MAXFRAGMENTSIZE = 16384 # binding the host to empty allows reception on # all network interfaces s.bind(('', port)) s.listen(5) # when accept returns, the client is connected (s2, address) = s.accept() # this receives the size of the file # as a 4 byte integer in network byte order (big endian) longPacker = struct.Struct("!L") long = s2.recv(4) fn = longPacker.unpack(long) filelen = fn[0] # the MD5 computes a unique hash for all the data bytes_to_receive = filelen start_stamp = time.perf_counter() random.seed(a=352) # main loop to receive the data from the client while bytes_to_receive > 0: size = random.randrange(1, MAXFRAGMENTSIZE) if bytes_to_receive >= size: # pick a random size to receive fragment = s2.recv(size) else: fragment = s2.recv(bytes_to_receive) bytes_to_receive = bytes_to_receive - len(fragment) fd.write(fragment) end_stamp = time.perf_counter() lapsed_seconds = end_stamp - start_stamp if lapsed_seconds > 0.0: print("server1: received %d bytes in %0.6f seconds, %0.6f MB/s " % (filelen, lapsed_seconds, (filelen / lapsed_seconds) / (1024 * 1024))) else: print("server1: received %d bytes in %d seconds, inf MB/s " % (filelen, lapsed_seconds)) fd.close() s2.close()
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Client') parser.add_argument('-f','--filename', help='File to Send', required=False) parser.add_argument('-d','--destination', help='Destination IP Host', required=True) parser.add_argument('-p','--port', help='remote sock352 port', required=False) parser.add_argument('-u','--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v','--udpportTx', help='UDP port to use for sending', required=False) parser.add_argument('-k','--keyfile', help='keyfile', required=True) # get the arguments into local variables args = vars(parser.parse_args()) filename = args['filename'] destination = args['destination'] udpportRx = args['udpportRx'] keyfilename = args['keyfile'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 2 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 5555 # open the file to send to the server for reading if (filename): try: filesize = os.path.getsize(filename) fd = open(filename, "rb") usefile = True except: print ( "error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the client uses for the underlying UDP # sockets. If we are running the client and # server on the same machine, these ports # need to be different. If they are running on # different machines, we can re-use the same # ports. if (udpportTx): sock352.init(udpportTx,udpportRx) else: sock352.init(udpportRx,udpportRx) # load a keychain from a file keysInHex = sock352.readKeyChain(keyfilename) # create a socket and connect to the remote server s = sock352.socket() s.connect((destination,port),sock352.ENCRYPT) # Comment above and uncomment below to toggle encryption #s.connect((destination,port)) #mesure the start stamp start_stamp = time.clock() #load the whole file into memory whole_file = fd.read() #mesure its length filesize = len(whole_file) longPacker = struct.Struct("!L") fileLenPacked = longPacker.pack(filesize); s.send(fileLenPacked) sent = s.send(whole_file) if (sent != filesize): raise RuntimeError("socket broken") end_stamp = time.clock() lapsed_seconds = end_stamp - start_stamp if (lapsed_seconds > 0.0): print ("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize/lapsed_seconds)/(1024*1024))) else: print ("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds)) fd.close() s.close()
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Client') parser.add_argument('-f', '--filename', help='File to Echo', required=False) parser.add_argument('-d', '--destination', help='Destination IP Host', required=True) parser.add_argument('-p', '--remoteport', help='remote sock352 UDP port', required=False) parser.add_argument('-l', '--localport', help='local sock352 UDP port', required=True) # get the arguments into local variables args = vars(parser.parse_args()) filename = args['filename'] destinationIP = args['destination'] remote_port = args['remoteport'] local_port = args['localport'] # open the file for reading if (filename): try: filesize = os.path.getsize(filename) fd = open(filename, "rb") except: print ("error opening file: %s" % (filename)) exit(-1) else: pass # create a socket and connect to the remote server s = sock352.socket() dest_addr = (destinationIP, int(remote_port)) # use the MD5 hash algorithm to validate all the data is correct mdhash_sent = md5.new() mdhash_recv = md5.new() # a lines of lines to echo back lines = fd.readlines() rtt_times = [] # for each line, take a time-stamp, send and recive the line, update the list of RTT times, # and then update the MD5 hash of the sent and received data zero_stamp = time.clock() for line in lines: start_stamp = time.clock() send = s.sendto(line, dest_addr) recv_data, from_addr = s.recvfrom(len(line)) end_stamp = time.clock() lapsed_seconds = float(end_stamp - start_stamp) rtt_times.append(lapsed_seconds) # update the sent and received data mdhash_sent.update(line) mdhash_recv.update(recv_data) # this allows use to time the entire loop, not just every RTT # Allows estimation of the protocol delays final_stamp = end_stamp # this part send the lenght of the digest, then the # digest. It will be check on the server digest_sent = mdhash_sent.digest() digest_recv = mdhash_recv.digest() # compute RTT statisticis rtt_min = min(rtt_times) rtt_max = max(rtt_times) rtt_ave = float(sum(rtt_times)) / float(len(rtt_times)) total_time = final_stamp - zero_stamp print ("echo_client: echoed %d messages in %0.6f millisec min/max/ave RTT(msec) %.4f/%.4f/%.4f " % (len(lines), total_time * 1000, rtt_min * 1000, rtt_max * 1000, rtt_ave * 1000)) # compare the two digests, byte for byte failed = False; for i, sent_byte in enumerate(digest_sent): recv_byte = digest_recv[i] if (sent_byte != recv_byte): print("echo_client: digest failed at byte %d diff: %c %c " % (i, sent_byte, recv_byte)) failed = True; if (not failed): print("echo_client: digest succeeded") s.close() fd.close()
def main(): # parse all the arguments to the client parser = argparse.ArgumentParser(description='CS 352 Socket Client') parser.add_argument('-f', '--filename', help='File to Send', required=False) parser.add_argument('-d', '--destination', help='Destination IP Host', required=True) parser.add_argument('-p', '--port', help='remote sock352 port', required=False) parser.add_argument('-u', '--udpportRx', help='UDP port to use for receiving', required=True) parser.add_argument('-v', '--udpportTx', help='UDP port to use for sending', required=False) # get the arguments into local variables args = vars(parser.parse_args()) filename = args['filename'] destination = args['destination'] udpportRx = args['udpportRx'] if (args['udpportTx']): udpportTx = args['udpportTx'] else: udpportTx = '' # the port is not used in part 1 assignment, except as a placeholder if (args['port']): port = args['port'] else: port = 5555 # open the file to send to the server for reading if (filename): try: filesize = os.path.getsize(filename) fd = open(filename, "rb") usefile = True except: print("error opening file: %s" % (filename)) exit(-1) else: pass # This is where we set the transmit and receive # ports the client uses for the underlying UDP # sockets. If we are running the client and # server on the same machine, these ports # need to be different. If they are running on # different machines, we can re-use the same # ports. if (udpportTx): sock352.init(udpportTx, udpportRx) else: sock352.init(udpportRx, udpportRx) # create a socket and connect to the remote server s = sock352.socket() s.connect((destination, port)) BUFFERSIZE = 268288 longPacker = struct.Struct("!L") fileLenPacked = longPacker.pack(filesize) s.send(fileLenPacked) mdhash = md5() bytes_to_send = filesize start_stamp = time.process_time() while (bytes_to_send > 0): buff = fd.read(BUFFERSIZE) mdhash.update(buff) totalsent = 0 while (totalsent < len(buff)): sent = s.send(buff[totalsent:]) if (sent == 0): raise RuntimeError("broken socket") totalsent = totalsent + sent bytes_to_send = bytes_to_send - len(buff) end_stamp = time.process_time() lapsed_seconds = end_stamp - start_stamp digest = mdhash.digest() long = len(digest) digestLenPacked = longPacker.pack(long) sent = s.send(digestLenPacked) if (sent != 4): raise RuntimeError("socket broken") sent = s.send(digest) if (sent != len(digest)): raise RuntimeError("socket broken") if (lapsed_seconds > 0.0): print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize / lapsed_seconds) / (1024 * 1024))) else: print("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds)) fd.close() s.close()
import sock352 import struct sock352.init('8000', '3221') mysocket = sock352.socket() givenHeader = mysocket.connect(("localhost", 1111)) x = struct.unpack("!12B", givenHeader) print(x)