- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个使用 TCP 连接进行通信的服务器-客户端程序。多个客户端可以同时连接到服务器。我想在这个系统上实现tcp hole punching。
在客户端,它调用公共(public)服务器来查找我的服务器的公共(public) ip、端口。然后连接到它。
但是在服务器端它必须打开一个端口来连接到公共(public)服务器,并且它还必须在同一个端口上接受客户端连接请求。
我要做的是打开一个套接字并绑定(bind)到端口X,然后连接到公共(public)服务器,然后将这个套接字更改为监听状态以接受传入连接一段时间,然后开始连接到公共(public)服务器一次又一次。
这是正确的方法吗?
编辑:我有另一个想法。就是开一个新的端口,连接公用服务器。主服务器端口照常监听传入连接。当客户端要连接时,公共(public)服务器将通过新端口告诉我的服务器。它将阻止主端口监听传入连接,而是连接到客户端进行打洞。然后它连接到公共(public)服务器,它将服务器公共(public) ip 地址转发给客户端,并像往常一样返回监听传入连接。然后客户端将使用此地址连接到已打开 TCP 漏洞的服务器。
最佳答案
最好有两个套接字,并保持服务器和客户端之间的独立连接。
m_nServerTCPSocket- 用于与服务器连接和监听套接字
m_nPeerPrivateTCPSocket- 连接对端(公共(public)地址)
m_nPeerPublicTCPSocket- 与对等点连接(如果其他对等点在同一网络中,则为私有(private)地址)
m_nConnectedPeerTCPSocket-> 一旦与其他对等方连接,您就会获得此套接字。
while(end_client)
{
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_ZERO(&fdExcept);
if (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdExcept);
}
if (pControlMgr->GetConnectionMgr()->GetServerTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),&fdExcept);
}
if (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket()>0)
{
FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdRead);
FD_SET (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),&fdExcept);
}
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
nSelectRetVal = select(NULL,&fdRead,NULL,&fdExcept,&tv);
if (nSelectRetVal>0)
{
int nRecvRetVal = 0;
/* TCP Server Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdRead ))
{
try
{
pRecvBuffer = new char[TCP_RECV_SIZE];
nRecvRetVal = recv(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),
pRecvBuffer,TCP_RECV_SIZE,
0);
int n = WSAGetLastError();
if (nRecvRetVal>0)
{
int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )
{
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
return 0 ;
}
}
}
}
catch (...)
{
if ( NULL != pRecvBuffer )
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
}
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
} /* TCP Server Socket handling */
int n;
/* TCP Exception Server Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetServerTCPSocket(), &fdExcept ))
{
/*FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr().GetServerTCPSocket (),&fdExcept);*/
n = WSAGetLastError();
//return 0;
}
if (FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),&fdRead))
{
sockaddr_in addrConnectedPeer;
int nAddrLen =sizeof(addrConnectedPeer) ;
int nConnectedSock = accept( pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(),
(sockaddr*)&addrConnectedPeer,
&nAddrLen);
int n1 = WSAGetLastError();
if (nConnectedSock>0)
{
pControlMgr->GetConnectionMgr()->SetConnectedTCPSocket(nConnectedSock);
int n = pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket();
continue;
}
}
/* TCP Exception Listening Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket(), &fdExcept ))
{
FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr()->GetListeningTCPSocket (),&fdExcept);
//return 0;
} /* TCP Exception Listening Socket handling */
/* Connected Peer TCP Read Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdRead ))
{
try
{
pRecvBuffer = new char[TCP_RECV_SIZE];
nRecvRetVal = recv (pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),
pRecvBuffer,TCP_RECV_SIZE,
0);
if (nRecvRetVal>0)
{
int nPeerNameRetVal = getpeername(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(),(sockaddr*)&addrRemotePeer,&nSockAddrLen);
if ( pControlMgr->HandlePacket(pRecvBuffer,addrRemotePeer)== -1 )
{
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
return 0 ;
}
}
}
}
catch (...)
{
if ( NULL != pRecvBuffer )
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
}
//FD_CLR(pControlMgr->GetConnectionMgr().GetConnectedTCPSocket(),&fdRead);
if ( NULL != pRecvBuffer)
{
delete [] pRecvBuffer;
pRecvBuffer = NULL;
}
} /* Peer TCP Read Socket handling */
/* TCP Exception Connected Socket handling */
if ( FD_ISSET(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket(), &fdExcept ))
{
/*FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdRead);
FD_CLR(pControlMgr->GetConnectionMgr()->GetConnectedTCPSocket (),&fdExcept);
return 0;*/
n = WSAGetLastError();
}
创建套接字的逻辑
int CConnectionMgr::CreateSocket(const int nSockType)
{
//TODO: Add code here
if (InitWinSock() == -1)
{
return -1;
}
SetLocalIPAddress();
m_nListeningTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if ( GetListeningTCPSocket() <0 )
return -1;
if (BindSocket(GetListeningTCPSocket())<0)
return -1;
int nListenRet = listen(GetListeningTCPSocket(),SOMAXCONN);
if (nListenRet!=0)
{
return -1;
}
m_nPeerPrivateTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if (GetPeerPrivateTCPSocket()<0)
return -1;
if (BindSocket(GetPeerPrivateTCPSocket())<0)
return -1;
m_nPeerPublicTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if ( GetPeerPublicTCPSocket()<0)
return -1;
if (BindSocket(GetPeerPublicTCPSocket())<0)
return -1;
m_nServerTCPSocket = socket(AF_INET, SOCK_STREAM ,nSockType );
if (GetServerTCPSocket()<0)
return -1;
if (BindSocket(GetServerTCPSocket())<0)
return -1;
return 1;
}
关于networking - 如何控制peer上的socket[TCP Hole Punching],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13118144/
来历及功能 peer.exe进程程序文件是由北京光芒时代国际传媒网络技术有限公司为其在飞速网发布的PC客户端:RaySource程序的一个组成部分。被描述为“Grid Service”,其功能主要
希望你们一切都好 我是 webRTC 的初学者,如果我的问题感觉像菜鸟,我很抱歉,但我想知道是否有任何正确的方法来关闭对等点之间的连接,尤其是使用 simple-peer.js,期待您的精彩回复 这是
我在使用 Windows 8 商店应用程序时遇到了一些问题。我的问题很简单: 我在一台电脑上有一个 Windows 8 商店应用程序,在另一台电脑上有相同的应用程序。个人电脑不在同一个网络中,但有互联
我正在从事一个尝试设置 PayPal 点对点支付的项目。然而,自适应支付 API 似乎是当前 PayPal API 的一个非常旧的版本,最近没有更新。 我是否遗漏了什么,或者 API 的这一部分是否仍
我的应用程序使用 GKSession 和 GKSessionModePeer。它必须处理任意连接和断开连接的点,因为这是一个长时间运行的应用程序,用户应该能够进入后台并稍后返回。这在大多数情况下都很好
我在 Google Meet 上开会,看到你可以打开实时字幕。他们实际上有一个演示 here关于如何进行实时语音到文本,所以这一点不会让我感到困惑。 我也一直想尝试使用 WebRTC(我相信 Goog
我正在尝试在聊天模块中共享图像/视频。我已经提到了Sample代码,但找不到任何帮助。 我已经提到过http://quickblox.com/modules/chat/它说通过插入我们全功能的聊天模块
我想让 WiFi peer-2-peer 中的一组连接过程对用户来说是无缝的。有办法吗? 蓝牙是怎么做到的? 最佳答案 基本上有两种方法可以做到这一点,我在 WIFI DIRECT, CONNECTI
这是我原来问题的后续:Qt Server+Client App: encryption fails (updated with code)还将此发布到 Qt 论坛:http://qt-project.
我有两个位于同一子网上的 debian 服务器。它们通过开关连接。我知道 UDP 不可靠。 Question 1: I assume the link layer is ethernet. And M
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
我正在创建一个应用程序 (C#),它将通过网络发送一些消息。传出消息将由私钥签名,传入消息将使用私钥解密。 如果有人窃取私钥,我希望能够撤销它(向所有其他客户端发送撤销消息)。由于我是被盗私钥的所有者
我仍在学习 SIP 及其所有协议(protocol),特别是尝试将 PJSIP 集成到 iPhone 应用程序中以进行 p2p 调用。我对使用 PJSUA 的对等 2 对等连接有疑问。我能够通过使用
我想用 Webrtc 创建应用程序,它可以在网站、移动 ios 应用程序和 android 应用程序上运行。 我找到了这个链接:- http://xsockets.net/api/net-c/cust
我使用 Spark Streaming 从 Twitter 接收推文。我收到很多警告说: replicated to only 0 peer(s) instead of 1 peers 这个警告有什么
我有一个需要大量数据的模拟程序。我将数据加载到 GPU 中进行计算,数据中存在很多依赖性。由于 1 个 GPU 不足以处理数据,所以我将其升级为 2 个 GPU。但限制是,如果我需要其他 GPU 上的
我在调试我的代码时遇到问题,因为我无法理解引发的套接字错误。这是回溯。 Traceback (most recent call last): File "clickpression.py", lin
我试图将Fabric配置为可以在Kubernetes集群中使用,并且在一切正常的同时,我很难将链码(使用composer-cli)部署到网络上。看来,chaincode容器看不到创建它们的对等方。 2
在Java中,您可以使用Socket和ServerSocket来获取两个进程之间通信的双向 channel 。但其中只有一个必须拥有 ServerSocket 并被视为“服务器”,另一个则被视为“客户
我用不同的名称和不同的字段名称创建了自己的用户表,我想让 kamailio 将它用作对等用户的“acc”表,我该怎么做? 最佳答案 kamailio 中的 acc 表用于会计记录(即账单数据)。 我知
我是一名优秀的程序员,十分优秀!