UDP数据包到NAT后面的PC [英] UDP packets to PC behind NAT

查看:90
本文介绍了UDP数据包到NAT后面的PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能更像是一个网络问题,而不是Python问题,但

如果有人用Python的套接字完成这个问题会很高兴

模块。除了一个通常从c.l.py获取更多信息而不是

其他地方:)


我有一个静态公共服务器IP和NAT后面的客户端。我希望
能够将UDP数据包从服务器发送到客户端。所以我需要做的是打开一个漏洞。在NAT中,让服务器知道它可以发送数据包的客户端的目标IP和端口。


现在我已经读过你的某个地方了可以在

相同端口上运行TCP和UDP。不确定这是否属实。它是一个合理的解决方案

启动从客户端到服务器的TCP连接,并以某种方式

(?)让服务器弄清楚客户端如何连接?然后

通过相同的(IP,端口)向客户端发送UDP?

This is probably more of a networking question than a Python one, but
it would be nice to know if someone has done this with Python''s socket
module. And besides one usually gets more information from c.l.py than
anywhere else :)

I have a server with a static "public" IP and a client behind a NAT. I
would like to send UDP packets from the server to the client. So what I
need to do is open up a "hole" in the NAT and let the server know the
target IP and port of the client where it can send its packets.

Now I have read somewhere that you can have TCP and UDP running on the
same port. Not sure if this is true. Would it be a reasonable solution
to initiate a TCP connection from the client to the server and somehow
(?) let the server figure out how the client is connecting? And then
send UDP to client over the same (IP, port)?

推荐答案

Janto Dreijer écrit:
Janto Dreijer a écrit :

这可能更像是一个网络问题,而不是Python问题,但是

如果有人知道的话会很高兴用Python的套接字

模块做到这一点。除了一个通常从c.l.py获取更多信息而不是

其他地方:)


我有一个静态公共服务器IP和NAT后面的客户端。我希望
能够将UDP数据包从服务器发送到客户端。所以我需要做的是打开一个漏洞。在NAT中,让服务器知道它可以发送数据包的客户端的目标IP和端口。


现在我已经读过你的某个地方了可以在

相同端口上运行TCP和UDP。不确定这是否属实。它是一个合理的解决方案

启动从客户端到服务器的TCP连接,并以某种方式

(?)让服务器弄清楚客户端如何连接?然后

通过相同的(IP,端口)向客户端发送UDP?
This is probably more of a networking question than a Python one, but
it would be nice to know if someone has done this with Python''s socket
module. And besides one usually gets more information from c.l.py than
anywhere else :)

I have a server with a static "public" IP and a client behind a NAT. I
would like to send UDP packets from the server to the client. So what I
need to do is open up a "hole" in the NAT and let the server know the
target IP and port of the client where it can send its packets.

Now I have read somewhere that you can have TCP and UDP running on the
same port. Not sure if this is true. Would it be a reasonable solution
to initiate a TCP connection from the client to the server and somehow
(?) let the server figure out how the client is connecting? And then
send UDP to client over the same (IP, port)?



启动从客户端到服务器的UDP连接,并让

服务器将UDP数据包发回到你在

" recvfrom"结果。

Initiate an UDP connection from the client to the server and have the
server send back the UDP packets to the address you get in the
"recvfrom" result.


太棒了!我还没有在实际的服务器上测试它,但我认为它有效。

谢谢!

我更喜欢TCP连接解决方​​案,如果有效的话会发一个。


server.py

========
来自套接字导入的
*

打印监听

UDPSock =套接字(AF_INET,SOCK_DGRAM)

UDPSock.bind((localhost,1234))#对外界的可见性

payload,addr = UDPSock.recvfrom(1024)

print"来自%s的消息:%s" %(`addr`,payload)

UDPSock =套接字(AF_INET,SOCK_DGRAM)#open UDP套接字

result = UDPSock.sendto("你的公共地址是%s" ;%`addr`,addr)


client.py

=====
来自套接字导入的
* br />
UDPSock = socket(AF_INET,SOCK_DGRAM)#open UDP socket

result = UDPSock.sendto(" what'是我的公共地址?",(" localhost" ,

1234))

有效载荷,addr = UDPSock.recvfrom(1024)

打印有效载荷


结果:

====

收听来自(''127.0.0.1'',32787)的
消息:什么''我的公共地址?


您的公共地址是(''127.0.0.1'',32787)

Awesome! I haven''t tested it on the actual server but I think it works.
Thanks!
I prefer a TCP connection solution and will post one if it works.

server.py
========
from socket import *
print "listening"
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(("localhost", 1234)) # visibility to outside world
payload, addr = UDPSock.recvfrom(1024)
print "message from %s: %s" % (`addr`, payload)
UDPSock = socket(AF_INET, SOCK_DGRAM) # open UDP socket
result = UDPSock.sendto("your public address is %s" % `addr`, addr)

client.py
=====
from socket import *
UDPSock = socket(AF_INET, SOCK_DGRAM) # open UDP socket
result = UDPSock.sendto("what''s my public address?", ("localhost",
1234))
payload, addr = UDPSock.recvfrom(1024)
print payload

results:
====
listening
message from (''127.0.0.1'', 32787): what''s my public address?

your public address is (''127.0.0.1'', 32787)


2006-09-15,Christophe< ch ************* @ free.frwrote:
On 2006-09-15, Christophe <ch*************@free.frwrote:

启动UDP连接从客户端到服务器和

让服务器发回UDP包来到你的地址

进入recvfrom结果。
Initiate an UDP connection from the client to the server and
have the server send back the UDP packets to the address you
get in the "recvfrom" result.



没有UDP连接这样的东西,所以我不会理解你的'b $ b'建议。


-

格兰特爱德华兹格兰特哇!通过MEER biz doo
在SCHOIN
...

visi.com

There''s no such thing as a "UDP connection", so I don''t
understand what you''re suggesting.

--
Grant Edwards grante Yow! By MEER biz doo
at SCHOIN...
visi.com


这篇关于UDP数据包到NAT后面的PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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