I am creating a server and client socket in Python. I have managed to send datagrams between them but I need to set the server to constantly listen for UDP messages but I can't work out how. I have seen the serve_forever() function but when I enter it, it tells me that 'NameError: name 'serve_forever' is not defined. Do I need to import something or have I missed something completely. I am quite new to this so wasn't sure if I needed to add anything else in. Here is the beginning of the code:
import socket
serverSocket = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)
UDP_IP = "localhost"
UDP_PORT = 6842
address = ("localhost" , 6842)
serverSocket.bind(address)
serve_forever()
print ("Waiting for client...")
I suspect you can replace serve_forever()
with something this:
while True:
data = serverSocket.recv(65535)
print("Received a UDP packet containing %i bytes of data" % len(data))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.