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


C++ TCPSocket::accept方法代码示例

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


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

示例1: run

void ServerSocketThread::run (void)
{
    while (true) {
        Socket *pSocket = _pServerSocket->accept();
        if (pSocket) {
            printf ("ServerSocket: got a connection\n");
            ConnHandler *pHandler = new ConnHandler (pSocket);
            pHandler->start();
        }
    }
}
开发者ID:agilecomputing,项目名称:nomads,代码行数:11,代码来源:StreamIntDataTest.cpp

示例2: main


//.........这里部分代码省略.........
            if (0 != (rc = pSocketToNextNode->connect (strNextNodeHost, iNextNodePort))) {
                printf ("failed - will try again in 2 seconds\n");
                sleepForMilliseconds (2000);
            }
            else {
                printf ("Connected\n");
                break;
            }
        }

        SocketWriter sw (pSocketToNextNode);
        BufferedWriter bw (&sw, 2048);
        
        // Freeze
        int result;
        uint64 ui64StartFreeze = getTimeInMilliseconds();
        result = pMocket->getState (&bw);
        printf ("TIME FOR GETSTATE **  %llu  **\n", (getTimeInMilliseconds() - ui64StartFreeze));
        printf ("getState ended with status %d\n", result);
        if (0 != result) {
            delete pMocket;
            return -3;
        }
    }

    else if (0 == stricmp (argv[1], "receiver")) {
        // Create the Server Socket
        int rc;
        TCPSocket *pServerSocket;
        pServerSocket = new TCPSocket();
        int iLocalPortNum = 4444;
        if (0 != (rc = pServerSocket->setupToReceive (iLocalPortNum))) {
            return -3;
        }
        printf ("listening on port %d\n", iLocalPortNum);
        
        Socket *pSocketFromPreviousNode;
        // Wait for connection from previous node
        printf ("Waiting for connection from previous node...\n");
        fflush (stdout);
        pSocketFromPreviousNode = pServerSocket->accept();
        if (pSocketFromPreviousNode == NULL) {
            printf ("Failed\n");
            return -4;
        }
        printf ("Connected\n");
        pSocketFromPreviousNode->bufferingMode(0);
        
        SocketReader sr (pSocketFromPreviousNode);
        BufferedReader br (&sr);
        
        // Resume
        int result;
        uint64 ui64StartResumeAndRestoreState = getTimeInMilliseconds();
        result = pMocket->resumeAndRestoreState (&br);
        printf ("TIME FOR RESUMEANDRESTORESTATE **  %llu  **\n", (getTimeInMilliseconds() - ui64StartResumeAndRestoreState));
        printf ("resumeFromSuspension ended with status %d\n", result);
        
        if (0 != result) {
            printf ("ERROR\n");
            delete pMocket;
            return -5;
        }

        /*
         * The connection has been restored successfully.
         * We do not set to skip odd packets with the method debugStateCapture().
         * Odd packets that were in the queues and have migrated to the new node will be sent now.
         * Sleep for a little bit before closing the connection so that the queues can be emptied.
         */
        sleepForMilliseconds (5000);

        delete (pServerSocket);
        pServerSocket = NULL;
        delete (pSocketFromPreviousNode);
        pSocketFromPreviousNode = NULL;
        pMocket->close();
    }
    else {
        printf ("ERROR\n");
        return 0;
    }
    
    printf ("Mocket statistics:\n");
    printf ("    Packets sent: %d\n", pMocket->getStatistics()->getSentPacketCount());
    printf ("    Bytes sent: %d\n", pMocket->getStatistics()->getSentByteCount());
    printf ("    Packets received: %d\n", pMocket->getStatistics()->getReceivedPacketCount());
    printf ("    Bytes received: %d\n", pMocket->getStatistics()->getReceivedByteCount());
    printf ("    Retransmits: %d\n", pMocket->getStatistics()->getRetransmitCount());

    delete pMocket;

    delete pLogger;
    pLogger = NULL;

    #if defined (WIN32) && defined (_DEBUG)
        _CrtDumpMemoryLeaks();
    #endif
    return 0;
}
开发者ID:agilecomputing,项目名称:nomads,代码行数:101,代码来源:MigrationFileSend.cpp

示例3: addPermanentTCPSocket

Packet*
PacketReceiver::receiveAndCreatePacket(int32 micros,
                                       TCPSocket** tcpReceiverSock) 
{
   // Variables that must be set before creating the packet
   byte* buffer = NULL;
   /*ssize_t*/uint32 bufferLength = 0;
   int packetLength = 0;
   
   // Used to get the correct socket to read from
   TCPSocket* tcpSock;
   DatagramReceiver* datagramSock;

   // Reset tcpReceiverSock
   if ( tcpReceiverSock != NULL ) {
      *tcpReceiverSock = NULL;
   }
   
   if ( m_receiver.select( micros, tcpSock, datagramSock ) ) {
      if ( tcpSock != NULL ) {
         // Set the socket to read from depending on the state
         TCPSocket* readSocket = tcpSock;
         bool deleteReadSocket = false;
         bool keepSocket = false; // Override on deleteReadSocket

         if (tcpSock->getState() == TCPSocket::LISTEN) {
            uint32 acceptStartTime = TimeUtility::getCurrentTime();
            readSocket = tcpSock->accept();
            uint32 acceptTime = TimeUtility::getCurrentTime() - acceptStartTime;
            if ( acceptTime > 1000 ) {
               mc2dbg << "[PR]: Took " << acceptTime
                      << " to accept which is unacceptable" << endl;
            }
            // Check if the socket should be permanently added.
            if ( isPermanent( tcpSock ) ) {
               mc2dbg8 << "[PR]: Socket is permanent" << endl;
               // Yes - add the child socket too.
               addPermanentTCPSocket( readSocket );
            } else {
               PacketReceiveSocket* prs =  
                  dynamic_cast< PacketReceiveSocket* > ( readSocket );
               if ( prs == NULL ) {
                  mc2dbg8 << "[PR]: New socket is not PacketReceiveSocket"
                          << endl;
                  // No - disconnect after reading a packet.
                  deleteReadSocket = true;
               } else {
                  // Continue reading next time.
                  mc2dbg8 << "[PR]: I will read during the next round"
                          << endl;
                  // Add the socket, but don't restart the selector.
                  m_receiver.addTCPSocket( readSocket, true );
                  readSocket = NULL;
                  keepSocket = true;
               }
            }            
         }
            
         if (readSocket != NULL) {
            if ( dynamic_cast< PacketReceiveSocket* > ( readSocket ) ) {
               PacketReceiveSocket* prs =  
                  static_cast< PacketReceiveSocket* > ( readSocket );
               prs->readBytes();
               if ( prs->getReadState() == PacketReceiveSocket::done ) {
                  buffer = prs->getBuffer( true );
                  bufferLength = prs->getSize();
                  packetLength = prs->getSize();
                  if ( !isPermanent( readSocket ) ) {
                     deleteReadSocket = true;
                     removeTCPSocket ( readSocket );
                  }
               } else if ( prs->getReadState() == 
                           PacketReceiveSocket::reading_size ||
                           prs->getReadState() == 
                           PacketReceiveSocket::reading_packet )
               {
                  // This shouldn't happen anymore since it has been
                  // done above.
                  if ( readSocket != tcpSock && !isPermanent( tcpSock ) ) {
                     // Add new socket
                     MC2_ASSERT( false );
                     m_receiver.addTCPSocket( readSocket );
                  }
                  keepSocket = true;
               } else {
                  // Else error
                  if ( !isPermanent( readSocket ) ) {
                     deleteReadSocket = true;
                  }
               }
            } else if ( ! isPermanent( readSocket ) ) {
               // Should not hang forever if someone connects without
               // sending
               buffer = readPacket( readSocket, bufferLength, packetLength,
                                    10*1000*1000);
            } else {
               // Maybe timeout here too?
               buffer = readPacket( readSocket, bufferLength, packetLength);
            }
            if ( buffer == NULL && !keepSocket ) {
//.........这里部分代码省略.........
开发者ID:FlavioFalcao,项目名称:Wayfinder-Server,代码行数:101,代码来源:PacketReceiver.cpp


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