Open Hard- & Software [ DokuWiki WebSites MediaWiki NextCloud ]
Unidirektionales Senden und Empfangen von UDP-Datenpaketen (NoBlocking)
SourceCode PcUdpClient.py, arbeitet zusammen mit PcUdpServer.py oder Esp32UdpServer.py:
# import time import socket as SKT # UDP_IPADDRESS = '192.168.178.33' # '192.168.178.33' UDP_IPPORT = 5005 # #----------------------------------------------------------- if ('__main__' == __name__): # print('*** UdpClient: begin') # Socket = SKT.socket(SKT.AF_INET, SKT.SOCK_DGRAM) Socket.bind((UDP_IPADDRESS, UDP_IPPORT)) Socket.setblocking(0) Loop = True while (Loop): try: Data, Address = Socket.recvfrom(1024) if (b'Q' == Data): Loop = False except SKT.error: pass else: print(Data) finally: # debug print('-') time.sleep(0.1) # Socket.close() # print('*** UdpClient: end') #
SourceCode PcUdpServer.py, arbeitet zusammen mit PcUdpClient.py oder Esp32UdpClient.py:
# import time import socket as SKT # UDP_IPADDRESS = '192.168.178.255' UDP_IPPORT = 5005 # #----------------------------------------------------------- if ('__main__' == __name__): # print('*** UdpServer: begin') # Socket = SKT.socket(SKT.AF_INET, SKT.SOCK_DGRAM) for I in range(0, 10): print('.') Socket.sendto(b'Hello', (UDP_IPADDRESS, UDP_IPPORT)) time.sleep(1.0) # Socket.sendto(b'Q', (UDP_IPADDRESS, UDP_IPPORT)) Socket.close() print('*** UdpServer: end') #