UDP打洞(c ++ / winsock) [英] UDP Hole Punching (c++/winsock)

查看:370
本文介绍了UDP打洞(c ++ / winsock)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stackoverflow用户!



我有一个应用程序必须处理p2p,这就是我打开UDP打孔。但我遇到了麻烦与实施。希望,你可以给我一些提示。



我有服务器,它工作完美,介绍客户端彼此,但客户端不能连接可能是因为我的小exp使用套接字。因此,客户端代理是:


  1. 创建udp套接字( socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

  2. 通过 sendto 功能向服务器发送消息

  3. 使用 recvfrom locker功能获取答案从服务器

在这3个步骤后,我得到对等端点。接下来,我尝试以两种方式连接客户:



Way1


  1. 使用同一套接字通过 sendto 函数将数据发送到对等体,但传递另一个 sockaddr

  2. 使用 recvfrom 锁定功能进行侦听(此时我收到 WSAECONNRESET 错误)

Way2


  1. 创建新套接字






  2. < b

    这样,一个客户端绑定失败,另一个客户端在侦听时出现错误 WSAEADDRINUSE WSAECONNRESET 。我显然做错了事,你的帮助将高度赞赏。先感谢。



    想分享一篇关于UDP Hole Punching的文章,以帮助那些对这项技术有新意的人: http://www.brynosaurus.com/pub/net/p2pnat/

    解决方案

    如果您阅读 recvfrom()的文档 ,它说:


    WSAECONNRESET



    虚拟电路已重置远程端执行硬或中止关闭。应用程序应关闭套接字;它不再可用。 在UDP数据包套接字上,此错误表示以前的发送操作导致了ICMP端口无法访问邮件


    这意味着您对 sendto()的调用失败。这是有道理的,如果一个或两个客户端在路由器后面。根据你的描述(和缺乏代码),你实际上没有执行任何打孔打开路由器,允许客户端到客户端数据包通过。您只向您的服务器发送了一条消息,允许客户端到服务器和服务器到客户端的数据包通过。如在您链接到的文章。你实际上是做文章说的吗?


    stackoverflow users!

    I have an app that has to deal with p2p, and that's how I get to UDP Hole punching. But I ran into troubles with implementation. Hope, you can give me some tips.

    I've got server, which works perfect and introduces clients to eachother, but clients can't connect probably because of my small exp working with sockets. So, client algo is:

    1. Create udp socket (socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);)
    2. Send message to server via sendto function
    3. Use recvfrom locker function to get an answer from server

    After those 3 steps I get peer endpoint. Next, I was trying to connect clients in two ways:

    Way1:

    1. Use the same socket to send data to peer via sendto function, but passing another sockaddr
    2. Listen with recvfrom locker function (And at that point I'm getting WSAECONNRESET error)

    Way2:

    1. Create new socket
    2. Bind it
    3. Use it to send data to peer
    4. Listen

    In that way one client fails on binding and another fails on listening with errors WSAEADDRINUSE and WSAECONNRESET. I'm obviously doing something wrong and your help would be highly appreciated. Thanks in advance.

    P.S. Wanna share a good article about UDP Hole Punching in order to help those, who is new to this technique: http://www.brynosaurus.com/pub/net/p2pnat/

    解决方案

    If you read the documentation for recvfrom(), it says:

    WSAECONNRESET

    The virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket; it is no longer usable. On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message.

    Which means your call to sendto() is failing. That makes sense if one or both clients are behind a router. Based on your description (and lack of code), you are not actually performing any hole punching to open up the router(s) to allow client-to-client packets to pass through. You have only sent a message to your server, which allows for client-to-server and server-to-client packets to pass through. A few more packet exchanges between each client and the server are required to perform the hole punching on each end, as described in detail in the article you linked to. Are you actually doing what the article says to do?

    这篇关于UDP打洞(c ++ / winsock)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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