当前位置: 首页>>代码示例>>C++>>正文


C++ UdpServer::OnClientConnect方法代码示例

本文整理汇总了C++中UdpServer::OnClientConnect方法的典型用法代码示例。如果您正苦于以下问题:C++ UdpServer::OnClientConnect方法的具体用法?C++ UdpServer::OnClientConnect怎么用?C++ UdpServer::OnClientConnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UdpServer的用法示例。


在下文中一共展示了UdpServer::OnClientConnect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if

//接收socket连接线程
void * UdpServer::AcceptThread(void * pParam)
{
    if(0 == pParam)
    {
        cout << "param is null." << endl;
        return 0;
    }
    UdpServer * pThis = (UdpServer*)pParam;

    int maxFd = 0;
    struct sockaddr_in cliAddr;
    while(true)
    {
        FD_ZERO(&pThis->mReadFd);
        FD_SET(pThis->mServerSocket, &pThis->mReadFd);
        maxFd = (pThis->mServerSocket > maxFd)? pThis->mServerSocket : maxFd;

        for(list<int>::iterator itor = pThis->mCliList.begin(); itor != pThis->mCliList.end(); itor++)
        {
            FD_SET(*itor, &pThis->mReadFd);
            maxFd = (*itor > maxFd)? *itor : maxFd;
        }

        int res = select(maxFd + 1, &pThis->mReadFd, 0, 0, NULL);
        if(-1 == res)
        {
            cout << "select failed." << endl;
        }


        if(-1 != pThis->mServerSocket && FD_ISSET(pThis->mServerSocket , &pThis->mReadFd))
        {
            int sin_size = sizeof(struct sockaddr_in);
            int clientSoc = accept(pThis->mServerSocket, (struct sockaddr *)(&cliAddr), (socklen_t*)&sin_size);
            if(-1 == clientSoc)
            {
                cout << "accept error." << endl;
                continue;
            }
            if(DEBUG) cout << "Accept new client : " << clientSoc << endl;
            pThis->mCliList.push_front(clientSoc);

            string ip = inet_ntoa(cliAddr.sin_addr);
            char tmp[16] = {0};
            sprintf(tmp, "%d", ntohs(cliAddr.sin_port));
            string port = tmp;
            if(0 != pThis->mAcceptFunc)
            {
                pThis->mAcceptFunc(ip, port, clientSoc);
            }
            pThis->OnClientConnect(ip, port, clientSoc);
        }

        for(list<int>::iterator itor = pThis->mCliList.begin(); itor != pThis->mCliList.end(); itor++)
        {
            if(-1 != *itor && FD_ISSET(*itor , &pThis->mReadFd))
            {
                char buf[MAX_MESSAGE_LEN] = {0};
                int res = recv(*itor, buf, sizeof(buf), 0);
                if(0 < res)
                {
                    if(DEBUG) cout << "Receive data : " << buf << endl;
                    pthread_mutex_lock(&pThis->mMutex);
                    pThis->mDataList.push_back(MsgInfo(*itor, buf));
                    pthread_mutex_unlock(&pThis->mMutex);
                }
                else if(0 == res)
                {
                    cout << "client quit." << endl;
                    pthread_mutex_lock(&pThis->mMutex);
                    pThis->mCliList.remove(*itor);
                    itor--;
                    pThis->mDisconnectFunc(*itor);
                    pThis->OnClientDisconnect(*itor);
                    pthread_mutex_unlock(&pThis->mMutex);
                }
                else
                {
                    cout << "receive failed." << endl;
                }
            }
        }
    }
}
开发者ID:buxinqiufeng,项目名称:UdpSocket,代码行数:85,代码来源:UdpServer.cpp


注:本文中的UdpServer::OnClientConnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。