bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("127.0.0.1", 20001)
bufferSize = 1024
# Create a UDP socket at client side
# Send to server using created UDP socket
udpsocket.sendto(bytesToSend, serverAddressPort)
msgFromServer = udpsocket.recvfrom(bufferSize)
msg = "Message from Server {}".format(msgFromServer[0])
print(msg)
Following code for the UDPModule (after editing to shorten):
import threading from threading import Thread
class UDPModule(): import socket global udpsocket udpsocket= socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) def init(self): True
class ServerThread (Thread): def init(self): Thread.init(self) def run(self): Server().trysend()
class ClientThread (Thread): def init(self): Thread.init(self) def run(self): Client().tryreceive()
class Server(): def init(self): True def trysend(self): localIP = "127.0.0.1"
class Client(): def init(self): True def tryreceive(self): msgFromClient = "Hello UDP Server"
New Threads
server = ServerThread() client = ClientThread()
Start new Threads
server.start() client.start()