recvfrom 返回的地址的第二部分是什么? [英] What is the second part of the address returned by recvfrom?

查看:47
本文介绍了recvfrom 返回的地址的第二部分是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://wiki.python.org/moin/中的那两段代码Udp通讯

服务器:

import socket

UDP_IP="127.0.0.1"
UDP_PORT=5005

sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
sock.bind( (UDP_IP,UDP_PORT) )

while True:
    data, addr = sock.recvfrom( 1024 ) # buffer size is 1024 bytes
    print "received message:", data,"from", addr

客户:

import socket

UDP_IP="127.0.0.1"
UDP_PORT=5005
MESSAGE="Hello, World!"

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE

sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )

在服务器中,我修改了最后一行:

In the server, I modified the last line:

        print "received message:", data,"from", addr

所以它会打印消息发送的地址.在我的 macbook 上,端口似乎是 40000 或 65000 之间的某个随机数(我只是确定它看起来是随机的).

so it prints the address that the message was sent from. On my macbook the port seems to be some random number between 40000 or 65000 (i'm just sure it seems random).

知道这可能是什么吗?

推荐答案

这是一个 临时端口 客户端用来向服务器发送数据.

It's an ephemeral port used by the client to send data to the server.

这篇关于recvfrom 返回的地址的第二部分是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆