我正在使用c ++在windows平台上开发客户端服务器程序,如下所示。
1.我有远程数据中心(服务器) 2.我有一个网关(客户端)将与数据中心(tcp)连接 我有一个gui应用程序远程管理网关。
所以GUI Gateway就是服务器。
我做了以下事情
我在网关中有两个侦听器线程,在每个线程中调用select()系统调用。
当我启动网关时,两个侦听器自动启动并侦听来自数据中心和GUI的连接和数据。
网关与数据中心连接并接收数据。当我启动gui应用程序并尝试连接连接返回0但是它不会在网关中的GUI监听器线程中调用accept时为BUT。
如果是这样的话,那是什么原因。?
void* CConnectionMgr::ListnerThreadDataCenter(void* args)
{
CGatewayInstanceManager* pGatewayInstance = (CGatewayInstanceManager*)args;
fd_set fdRead;
fd_set fdExcept;
int nSelectRetVal = 0;
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
while(true)
{
FD_ZERO(&fdRead);
FD_ZERO(&fdExcept);
GatewayConnection::iterator itrGatewayCon;
GatewayConnection mapGatewayConn = pGatewayInstance->GetConnectionMgr()->GetGatewayConnection();
itrGatewayCon = mapGatewayConn.begin();
while (itrGatewayCon != mapGatewayConn.end())
{
FD_SET (itrGatewayCon->second,&fdRead);
FD_SET (itrGatewayCon->second,&fdExcept);
itrGatewayCon++;
}
nSelectRetVal = select(NULL,&fdRead,NULL,&fdExcept,&tv);
// demultiplexing the socket for in coming message
if (nSelectRetVal>0)
{
for ( itrGatewayCon = mapGatewayConn.begin();itrGatewayCon!= mapGatewayConn.end();itrGatewayCon++)
{
//Reading packet from the socket
unsigned char* pPacket = pGatewayInstance->GetConnectionMgr()->ReadPacketDataFromSocket(itrGatewayCon->second);
CProtocolMgr objProtocol;
CMessage* pMessage = objProtocol.ParseMsg(pPacket);
pGatewayInstance->HandleRequest(pMessage);
}
}
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(ghListenerThreadDataCenterShutdownEvent,WAIT_TIME);
if (WAIT_OBJECT_0 == dwWaitResult)
break;
}
return 0;
}
void* CConnectionMgr::ListnerThreadUI(void* args)
{
CGatewayInstanceManager* pGatewayInstance = (CGatewayInstanceManager*)args;
fd_set fdRead;
fd_set fdExcept;
int nSelectRetVal = 0;
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
while(true)
{
FD_ZERO(&fdRead);
FD_ZERO(&fdExcept);
if (pGatewayInstance->GetConnectionMgr()->GetServerSocket()!=-1 )
{
FD_SET (pGatewayInstance->GetConnectionMgr()->GetServerSocket(),&fdRead);
FD_SET (pGatewayInstance->GetConnectionMgr()->GetServerSocket(),&fdExcept);
}
UIConnection::iterator itrUIConnection;
UIConnection listUIConnection = pGatewayInstance->GetConnectionMgr()->GetUIConnection();
for(itrUIConnection = listUIConnection.begin();itrUIConnection != listUIConnection.end();itrUIConnection++)
{
if ( *itrUIConnection != -1)
{
FD_SET (*itrUIConnection,&fdRead);
FD_SET (*itrUIConnection,&fdExcept);
}
}
// demultiplexing the socket for in coming message
nSelectRetVal = select(NULL,&fdRead,NULL,&fdExcept,&tv);
if(nSelectRetVal>0)
{
if (FD_ISSET(pGatewayInstance->GetConnectionMgr()->GetServerSocket(),&fdRead))
{
// accepting and new client and updating the UIConnection List
int nAcceptRetVal = 0;
nAcceptRetVal = pGatewayInstance->GetConnectionMgr()->ManageConnection(pGatewayInstance->GetConnectionMgr());
continue;
}
if (FD_ISSET(pGatewayInstance->GetConnectionMgr()->GetServerSocket(),&fdExcept))
{
}
for( itrUIConnection = listUIConnection.begin();itrUIConnection != listUIConnection.end();itrUIConnection++)
{
if (FD_ISSET(*itrUIConnection,&fdRead))
{
//Reading packet from the socket
//string strPacket = pGatewayInstance->GetConnectionMgr()->ReadPacketDataFromSocket(*itrUIConnection);
//pGatewayInstance->HandleRequest();
}
}
}
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(ghListenerThreadUIShutDownEvent,WAIT_TIME);
if (WAIT_OBJECT_0 == dwWaitResult)
break;
}
return 0;
}
int CConnectionMgr::ManageConnection(CConnectionMgr* pConMgr)
{
sockaddr_in addrConnectedPeer;
int nAddrLen =sizeof(addrConnectedPeer) ;
int nAcceptRetVal = accept(pConMgr->GetServerSocket(),(sockaddr*)&addrConnectedPeer,&nAddrLen);
if (nAcceptRetVal>0)
{
pConMgr->AddUIConnection(nAcceptRetVal);
}
return 0;
}
答案 0 :(得分:2)
您是否检查了所选实施的手册?因为select通常会将第一个param视为最高fd +1。 见这里:http://www.manpagez.com/man/2/select/
基本上,select可以在app中多次使用,但是fd-masks(param 2,3,4)应该是不同的 - 否则输入的处理将被复制。
修改强>
返回值0表示超时,任何指定的fds都没有活动。
马里奥