def recvGetPkt(self, packet): tmpHeader = self.getHeader(packet) seq = tmpHeader.seqNum self.header.ackNum = seq if tmpHeader.ack: self.getBit = 1 else: if self.getBit == 0: content = packet[RxPHeader.headerLen:] uniFilename = self.bytesToString(content) filename = unicodedata.normalize('NFKD', uniFilename).encode('utf-8','ignore') self.getBit = 1 sendTread = SendThread(self, filename) self.threads.append(sendTread) sendTread.start() self.header.get = True self.sendAck() self.header.get = False
def main(): rxpProtocol = None # Handling the argument arg = sys.argv if len(arg) < 3 or len(arg) > 4: print 'Invalid command. Please try again.' sys.exit() # pass the command line arguments try: clientPort = int(arg[1]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < clientPort < 65536: print 'Invalid port number. Please try again.' sys.exit() # Server IP address serverIP = arg[2] if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() # netEmu port number try: netEmuPort = int(arg[3]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < netEmuPort < 65536: print 'Invalid port number. Please try again.' sys.exit() # Dest. port number desPort = clientPort + 1 log = "output-client.txt" clientProtocol = None sendThread = None connThread = None sThread = None #execute user's commend while True: time.sleep(.500) Sinput = raw_input("type connect - to establish connection \n" + "get 'filename' - to download the file from server \n" + "post 'filename' - to upload the file to server \n" + "Window W - to change the window size \n" + "disconnect - to close the connection\n" + 'quit - to quit the application\n') if Sinput.__eq__("connect"): rxpProtocol = RxP(serverIP, netEmuPort, clientPort, desPort, log) clientProtocol = RecvThread(rxpProtocol) clientProtocol.start() rxpProtocol.connect() # get file form server elif "get" in Sinput: if rxpProtocol != None: s = Sinput.split() rxpProtocol.getFile(s[1]) # post file form server elif "post" in Sinput: if rxpProtocol != None: s = Sinput.split() sendThread = SendThread(rxpProtocol, s[1]) sendThread.start() # set the window size elif "window" in Sinput: if rxpProtocol != None: s = Sinput.split() try: window = int(s[1]) except ValueError: print 'Invalid window size. Please try again.' sys.exit() if not 0 < window < 50: print 'Window size too big. Please try again.' sys.exit() print "Set window size to " + str(window) rxpProtocol.setWindowSize(window) #close connection elif Sinput.__eq__("disconnect"): if rxpProtocol != None: rxpProtocol.close() clientProtocol.stop() rxpProtocol.socket.close() rxpProtocol = None elif Sinput.__eq__("quit"): if rxpProtocol: print 'disconnect before quit' else: break
def main(): # set default window size = 2 window = 2 rtpProtocol = None # Handling the argument arg = sys.argv if len(arg) != 3: print 'Invalid command. Please try again.' sys.exit() arg1 = arg[1].split(":") # Server IP address serverIP = arg1[0]; if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() # Dest. port number desPort = int(arg1[1]) if not 0 < desPort < 65536: print 'Invalid port number. Please try again.' sys.exit() window = int(arg[2]) clientProtocol = None sendThread = None connThread = None sThread = None hostAddress = '127.0.0.1' # connect rtpProtocol = rtp(hostAddress, 8889, serverIP, desPort, None, True) clientProtocol = RecvThread(rtpProtocol) clientProtocol.start() rtpProtocol.connect() rtpProtocol.setWindowSize(window) #execute user's commend while True: time.sleep(.500) Sinput = raw_input("get F - to download the file from server \n" + "post G - to upload the file to server \n" + "get-post F G - to download F and upload G at same time \n" + "disconnect - to close the connection\n") # get file and post file from server at a same time if "get-post" in Sinput: if rtpProtocol != None: s = Sinput.split(' ') sendThread = SendThread(rtpProtocol, s[2]) sendThread.start() rtpProtocol.getFile(s[1]) # get file from server elif "get" in Sinput: if rtpProtocol != None: s = Sinput.split() rtpProtocol.getFile(s[1]) # post file form server elif "post" in Sinput: if rtpProtocol != None: s = Sinput.split() sendThread = SendThread(rtpProtocol, s[1]) sendThread.start() #close connection elif Sinput.__eq__("disconnect"): if rtpProtocol != None: rtpProtocol.close() clientProtocol.stop() rtpProtocol.socket.close() rtpProtocol = None break