python udp遇到ICMP: Destination unreachable (Port unreachable)
标签:
it |
分类: Python |
嵌入式设备实现了一个udp服务器,在PC端用python实现一个udp客户端和嵌入式设备通信。百度python
udp通信,网上基本上都是类似这样的例子:
http://s8/mw690/001y82MHzy7jXKQkFev47&690udp遇到ICMP: Destination unreachable (Port unreachable)" TITLE="python udp遇到ICMP: Destination unreachable (Port unreachable)" />
udp客户端在用sendto发送完后,调用recvfrom接收数据。
依样画葫芦,写了个客户端,在把数据发到嵌入式设备的时候,客户端程序在recvfrom阻塞住了。
addr = (target, PORT)
udp_socket.sendto(data, addr)
data, addr = udp_socket.recvfrom(BUFFSIZE)
抓包可以看到,遇到了ICMP: Destination unreachable (Port
unreachable)
http://s11/mw690/001y82MHzy7jXLapj2q1a&690udp遇到ICMP: Destination unreachable (Port unreachable)" TITLE="python udp遇到ICMP: Destination unreachable (Port unreachable)" />
分析后觉得接收socket应该绑定端口,在sendto前加入
分析后觉得接收socket应该绑定端口,在sendto前加入
HOST = '127.0.0.1'
udp_socket.bind((HOST, PORT))
程序依旧在recvfrom阻塞住了,HOST = 'localhost'也一样的现象。
直到HOST = gethostbyname(gethostname()),程序终于可以正常工作。
网上的程序服务器和客户端都是在本机运行,所以设置为本地主机地址,运行没问题。
当在不同的计算机间进行udp通信的时候,客户端也需要绑定接收端口,这个时候不能用表示本地主机的localhost或者回路网络接口127.0.0.1,而需要真正的IP地址,如192.9.25.240。

加载中…