问题描述
这是我第一次使用P2P,我需要有关设计的一些帮助.
This is my first experience with P2P and i need some help regarding the design.
我正在开发一个简单的Messenger应用程序.我有一个目录服务器,每个用户都在该目录服务器上进行身份验证并宣布一个开放端口,每个用户都可以在该端口上访问.目录服务器维护用户和端口,我可以向目录服务器查询任何特定用户.这部分完成了.第二部分是我认为应该是P2P的聊天.我既可以开始聊天,也可以成为聊天的终点(客户端和服务器)
I am developing a simple messenger application. I have a directory server on which every user authenticates and announces an open port on which every user is reachable. The directory server maintains the users and the ports and I can query the directory server for any specific user. This part is done. The second part is the chat which i think should be P2P. I can start a chat as well as I can be end point of a chat (client as well as server)
让我感到困惑的是如何处理P2P?我是否创建两个不同的套接字?我正在其中一个侦听传入连接的TCP请求,而我正在其中发送TCP请求以开始聊天.
What is confusing me is how do I deal with P2P? Do I create two different sockets? One on which I am listening for TCP requests for incoming connections and another one from which I would send TCP requests to start chat.
在这种情况下,我需要3个套接字,一个用于与服务器通信,另外两个用于P2P吗?
In this case do I need 3 sockets, one to talk with server and two for P2P?
推荐答案
如果要使用P2P,最好使用Java框架,例如JXTA.创建套接字本身可能还不够,因为如果要在LAN之外进行操作,还需要处理更复杂的问题,例如NAT遍历.
If you want to go P2P, you'd better use a framework, such as JXTA for example if you are coding in Java. Creating sockets may not be enough by itself, because there are more complicated issues you need to deal with such as NAT traversal if you are operating beyond your LAN.
似乎您有一个中央对等方(某些服务器).如果它具有公共IP地址,则可以实现类似TURN的体系结构(对等方通过此中央对等方进行通信).如果要在对等方之间直接连接,您正在寻找STUN解决方案,但仍需要中央对等方来促进通信.
It seems like you have a central peer (some of server). If it has a public IP address, then you could implement a TURN-like architecture (peers communicate via this central peer). If you want direct connection between peers, you are looking a STUN solutions, but you still need a central peer to facilitate the communication.
TCP Stun并不容易. UDP不是很复杂,您只需要在NAT中打一个洞即可.现在,请记住,并非始终可以进行NAT遍历(这取决于NAT本身).在这种情况下,备份解决方案将处于STUN模式.
TCP Stun is not easy. UDP is not very complicated, you just need to punch a hole in your NAT. Now, keep in mind that NAT traversal is not always possible (it depends of the NAT itself). In this case, the backup solution in a STUN one.
这篇关于编写一个简单的P2P聊天应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!