Пример #1
0
void ServerTCP()
{
    TcpListenSocket listen;
    TcpServer server;
    Packet data;
    string source;
    data.SetByteOrder(CX_PACKET_BIG_ENDIAN);
    // Tests non-blocking TCP connections.  If listen socket is non-blocking
    // then all TCP connections generated will be non-blocking.
    if(!listen.InitializeSocket(PORT, 5U, 0U, 0U, 1, 1)) 
    {
        cout << "Unable to initialize listen socket on port " << PORT << endl;
        return;
    }
    cout << "Initialized TCP Listen Socket\n";
    cout << "Waiting for a connection.... ";
    while( true ) 
    {
        if(listen.AwaitConnection(server))
        {
            cout << "Connection Made!\n";

            while(true)
            {
                if(server.Recv(data, 50, 0) > 0) //  Receive up to 50 bytes to packet.
                { 
                    cout << "Received: ";
                    char val;
                    while(data.Read(val))
                    {
                        cout << val;
                        server.Send(data);
                    }
                    cout << endl;
                }
                CxUtils::SleepMs(1);
            }
            cout << "Connection Closed.\n";
        }
        SleepMs(1);
    }
}