Direct P2P connection

倾然丶 夕夏残阳落幕 提交于 2019-12-03 06:25:33

问题


This topic resembles this thread

I'm rather new to the topic of network programming, never having done anything but basic TCP/UDP on a single local machine. Now I'm developing an application that will need P2P network support. More specifically I will need the application to connnect and communicate across the internet preferably without the use of a server to do the matchmaking between the clients.

I'm aware and assuming that almost all users are behind a router which complicates the process since neither clients will be able to initialize a direct connection to the other.

I know UPnP is an option to allow port forwarding without having the users configure this manually, but as of now this is not an option. Is they any way to achieve my goal or will I need that server?


回答1:


You'll need a server to exchange IP address and such. As the other thread literally points out, the only way of guaranteeing a connection is to proxy through a server. Most peer to peer systems use UPnP and NAT Hole Punching (this method needs a server relaying port information and only works with UDP) to establish a connection in most cases.

NAT Hole Punching works by both clients establishing a connection to a server, then the both try to connect directly to a port that the other has relayed to the other. Most UDP NAT remember the IP address and port for a short time, so although the data never made it to the other end (not that this matters with UDP) the other client will try to connect a few moments later to that report as the NAT would expect the reply.




回答2:


A very good reading, made just for you :-), is RFC 5128, "State of Peer-to-Peer (P2P) Communication across Network Address Translators (NATs)".




回答3:


Check out the P2P channel with WCF: http://msdn.microsoft.com/en-us/library/cc297274.aspx

It works quite well.




回答4:


Ignoring UPnP (which only works with some routers, unfortunately), and no central server, I'm not sure it would be possible to create a direct connection when both users are behind a NAT.




回答5:


Well to avoid a server to "matchmake" you could do what skype does and set up some peers as relays to others behind NATs. You wll always need some sort of boot strapping mechanism, so a centralized server will probably play into your system somehow (depending on what your developing, of course).



来源:https://stackoverflow.com/questions/1183459/direct-p2p-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!