Skip to content

6.2 UDP Client Server

1. Simple UDP Client-Server

Here’s a simple UDP client-server example for use in Cisco Packet Tracer using the IoT scripting environment (such as a generic PC or IoT device).

1.2 βœ… Server Code (UDP Server)

from udp import *
from time import *
def onUDPReceive(ip, port, data):
print("received from "
+ ip + ":" + str(port) + ":" + data)
def main():
socket = UDPSocket()
socket.onReceive(onUDPReceive)
print(socket.begin(1235))
count = 0
while True:
count += 1
socket.send("192.168.1.1", 1235, "hello " + str(count))
sleep(5)
if __name__ == "__main__":
main()

1.2 βœ… Client Code (UDP Client)

from udp import *
from time import *
def onUDPReceive(ip, port, data):
print("received from "
+ ip + ":" + str(port) + ":" + data)
def main():
socket = UDPSocket()
socket.onReceive(onUDPReceive)
print(socket.begin(1235))
count = 0
while True:
count += 1
socket.send("192.168.1.2", 1235, "hello " + str(count))
sleep(5)
if __name__ == "__main__":
main()

1.3 πŸ›  Notes:

  • These scripts should be placed on appropriate IoT/PC devices.
  • Make sure the IP addresses match your network setup.
  • Use UDPSocket, not TCP.
  • Unlike TCP, no connection setup is needed β€” the client can start sending immediately.