I have two simple Python files: client.py and server.py. The client simply sends the text that you enter to the server through a UDP socket.
The assigned and listened port 21567 , BUT ... reading the line:
print "\nReceived message '", data,"' from ", addr
addr appears in server.py as something similar to this: ('127.0.0.1', 60471)
Now I don’t understand why this seemingly random port is being reported, 60471 is random every time the script is run. Can anyone shed some light on this question, why doesn't it say 21567 as set in the code? Thanks!
The contents of the Python script file are as follows:
client.py
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
def_msg = "===Enter message to send to server===";
print "\n",def_msg
while (1):
data = raw_input('>> ')
if not data:
break
else:
if(UDPSock.sendto(data,addr)):
print "Sending message '",data,"'....."
UDPSock.close()
server.py
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"' from ", addr
UDPSock.close()