Python UDP套接字端口随机,尽管分配

发布于 2024-08-16 16:41:28 字数 1482 浏览 2 评论 0原文

我有两个简单的 Python 文件:client.pyserver.py。客户端只需通过 UDP 套接字将您键入的文本发送到服务器。

分配和侦听的端口是21567,但是...行读取:

print "\nReceived message '", data,"' from ", addr

server.py中输出addr看起来像this: ('127.0.0.1', 60471)

现在我不明白为什么会报告这个看似随机的端口,每次运行脚本时 60471 都是随机的。谁能解释一下这个问题,为什么它不像代码中设置的那样说 21567 ?谢谢!

Python脚本文件内容如下:

client.py

# Client program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
    data = raw_input('>> ')
    if not data:
        break
    else:
        if(UDPSock.sendto(data,addr)):
            print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

server.py

# Server program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
    data,addr = UDPSock.recvfrom(buf)
    if not data:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", data,"' from ", addr

# Close socket
UDPSock.close()

I have two simple Python files: client.py and server.py. The client simply sends the text you type to the server, via UDP socket.

The port assigned and listened to is 21567, BUT... the line reading:

print "\nReceived message '", data,"' from ", addr

in server.py outputs the addr to be something looking like this: ('127.0.0.1', 60471)

Now I don't understand why this seemingly random port is reported, the 60471 is random everytime the script is run. Can anyone please shed some light on this matter, why is it not saying 21567 like set in the code? Thanks!

The Python script file contents are as follows:

client.py

# Client program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

def_msg = "===Enter message to send to server===";
print "\n",def_msg

# Send messages
while (1):
    data = raw_input('>> ')
    if not data:
        break
    else:
        if(UDPSock.sendto(data,addr)):
            print "Sending message '",data,"'....."

# Close socket
UDPSock.close()

server.py

# Server program

from socket import *

# Set the socket parameters
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
    data,addr = UDPSock.recvfrom(buf)
    if not data:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", data,"' from ", addr

# Close socket
UDPSock.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2024-08-23 16:41:28

60471是客户端端口,21567是服务器端口。它们不能相同:任何 IP 流量都必须声明其源地址和端口以及目标地址和端口。客户端端口通常是 32768 到 65535 范围内的随机数。addr 告诉您客户端的地址。

这样做是为了让多个客户端与同一服务器通信(即 IP 地址和端口组合),并且可以使用客户端端口号消除流的歧义,即使使用 UDP/IP 等无连接协议也是如此。

60471 is the client's port and 21567 is the server's port. They can't be the same: Any IP traffic has to declare its source address and port, and its destination address and port. The client port is usually a random number in the range 32768 to 65535. addr is telling you the client's address.

This is done so you can have multiple clients talking to the same server (i.e. IP address and port combination), and the streams can be disambiguated using the client port numbers, even with a connectionless protocol like UDP/IP.

酸甜透明夹心 2024-08-23 16:41:28

您正在打印的端口是发送者的端口。客户端的端口始终是随机的,标准操作系统机制。就像网络服务器的端口是80一样,但是当你的计算机连接到服务器时,你每次都会以随机端口退出。

The port you are printing is that of the sender. The client's port is always random, stardard operating system mechanism. Just like a web server's port is 80, but when your computer connects to a server, you exit with a random port every time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文