def __init__(self,Server_Address=(socketLib.findByDomain("T2"),80)): self.lowerLayer = physicalLayer.PhysicalLayer() self.timeLimit=30 myAddress = (socketLib.findByDomain("T2"),84) signal.signal(signal.SIGALRM,self.alarmHandler) socket, AF_INET, SOCK_DGRAM, self.timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout self.receivedmsg=[] self.Server_Address = Server_Address ## receivingThr = thread(target=self.receiveMessage) ## receivingThr.start() ## lock = threading.Lock() with socket(AF_INET,SOCK_DGRAM) as self.sock: self.sock.bind(myAddress) self.sock.settimeout(10) # 10 second timeout print ("UDP_Client started for UDP_Server at IP address {} on port {}".format( self.Server_Address[0],self.Server_Address[1])) # Auto sending first message self.sendStrMessage("Hello Server!") # Recieve response ## self.receiveMessage() while True: ## try: time.sleep(2) timeout=time.time()+self.timeLimit while self.receivedmsg==[]: #and time.time()< timeout: self.receiveMessage() time.sleep(.1) for i in range(len(self.receivedmsg)): print (self.receivedmsg[i]) self.receivedmsg=[] self.userInput() ## self.receiveMessage() ## print('went to recive messages') ## except Exception as e: ## print('reach here') ## print(e) ## self.receiveMessage() print("UDP_Client ended")
def __init__(self, Server_Address=(socketLib.findByDomain("T2"), 80)): self.lowerLayer = physicalLayer.PhysicalLayer() self.timeLimit = 30 myAddress = (socketLib.findByDomain("T2"), 84) signal.signal(signal.SIGALRM, self.alarmHandler) socket, AF_INET, SOCK_DGRAM, self.timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout self.receivedmsg = [] self.Server_Address = Server_Address ## receivingThr = thread(target=self.receiveMessage) ## receivingThr.start() ## lock = threading.Lock() with socket(AF_INET, SOCK_DGRAM) as self.sock: self.sock.bind(myAddress) self.sock.settimeout(10) # 10 second timeout print( "UDP_Client started for UDP_Server at IP address {} on port {}" .format(self.Server_Address[0], self.Server_Address[1])) # Auto sending first message self.sendStrMessage("Hello Server!") # Recieve response ## self.receiveMessage() while True: ## try: time.sleep(2) timeout = time.time() + self.timeLimit while self.receivedmsg == []: #and time.time()< timeout: self.receiveMessage() time.sleep(.1) for i in range(len(self.receivedmsg)): print(self.receivedmsg[i]) self.receivedmsg = [] self.userInput() ## self.receiveMessage() ## print('went to recive messages') ## except Exception as e: ## print('reach here') ## print(e) ## self.receiveMessage() print("UDP_Client ended")
def __init__(self,Server_Address=(socketLib.findByDomain("T3"),80)): myAddress = (socketLib.findByDomain("T3"),84) socket, AF_INET, SOCK_DGRAM, timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout self.Server_Address = Server_Address with socket(AF_INET,SOCK_DGRAM) as sock: sock.bind(myAddress) sock.settimeout(2.0) # 2 second timeout print ("UDP_Client started for CN_Echo_Server at IP address {} on port {}".format( Server_Address[0],Server_Address[1]) ) while True: str_message_to_send = input("Enter message to send to echo server:\n") if not str_message_to_send: break sent_bytearray_message = bytearray(str_message_to_send,encoding="UTF-8") bytes_sent = sock.sendto(sent_bytearray_message, self.Server_Address) print ("{} bytes sent".format(bytes_sent)) address_received = ("a.b.c.d",(-1)) while Server_Address != address_received: try: bytearray_message_received, address_received = sock.recvfrom(1500) if address_received != self.Server_Address: print( "\n message from {} ignored".format(address_received)) except timeout: continue str_message_received = bytearray_message_received.decode("UTF-8") print ("\n{} bytes received".format(len(bytearray_message_received))) print ("message received:\n{}".format(str_message_received)) print ("UDP_Client ended")
def __init__(self, IP=socketLib.findByDomain("T3"), port=80): socket, AF_INET, SOCK_DGRAM, timeout = ( socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout, ) with socket(AF_INET, SOCK_DGRAM) as sock: sock.bind((IP, port)) sock.settimeout(2.0) # 2 second timeout print("UDP Server started on IP Address {}, port {}".format(IP, port)) while True: try: bytearray_msg, address = sock.recvfrom(1024) source_IP, source_port = address print( "\n{} byte message received from ip address {}, port {}:".format( len(bytearray_msg), source_IP, source_port ) ) print("\n" + bytearray_msg.decode("UTF-8")) lenx = sock.sendto(bytearray_msg, address) print("\n{} byte message echoed") except timeout: continue
def __init__(self, IP=socketLib.findByDomain("T3"), port=80): socket, AF_INET, SOCK_DGRAM, timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout with socket(AF_INET, SOCK_DGRAM) as sock: sock.bind((IP, port)) sock.settimeout(2.0) # 2 second timeout print("UDP Server started on IP Address {}, port {}".format( IP, port)) while True: try: bytearray_msg, address = sock.recvfrom(1024) source_IP, source_port = address print( "\n{} byte message received from ip address {}, port {}:" .format(len(bytearray_msg), source_IP, source_port)) print("\n" + bytearray_msg.decode("UTF-8")) lenx = sock.sendto(bytearray_msg, address) print("\n{} byte message echoed") except timeout: continue
def __init__(self,IP=socketLib.findByDomain("T3"),port=80): while True: self.run_the_chat(IP,port)