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


C++ TcpServer::Send方法代码示例

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


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

示例1: ServerTCP

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);
    }
}
开发者ID:ShowLove,项目名称:Robotics_Club,代码行数:42,代码来源:example_networking.cpp

示例2: FakeGPS

void FakeGPS()
{
    TcpListenSocket listen;
    TcpServer connection;
    Packet data;
    string source;
    if(!listen.InitializeSocket(PORT)) 
    {
        cout << "Unable to initialize listen socket on port " << PORT << endl;
        return;
    }
    cout << "Initialized TCP Listen Socket\n";
    cout << "Waiting for a connection.... ";
    bool quitFlag = false;
    while( listen.AwaitConnection(connection) && !quitFlag) 
    {
        cout << "Connection Made!\n";
        std::stringstream str;
        Time t;
        while(!quitFlag)
        {
            // Clear string.
            str.clear();
            str.str(std::string());
            // Set position data and message type.
            str << "$GPGLL,4916.45,N,12311.12,W,";
            // Get the current time.
            t.SetCurrentTime();
            str << t.mHour << t.mMinute << "." << t.mSecond;
            str << ",A";
            // Now calculate the checksum.
            unsigned char checkSum = 0;
            // Don't include the '$' character, but go to end.
            for(unsigned int i = 1; i < (unsigned int)str.str().size(); i++)
            {
                if(i == 1) 
                {
                    checkSum = str.str().c_str()[i];
                }
                else
                {
                    checkSum ^= str.str().c_str()[i];
                }
            }
            // Now add the checksum to the string.
            str << "*" << std::hex << std::uppercase << (int) checkSum;
            // Print to screen.
            std::cout << str.str() << std::endl;
            // Now send the data.
            if(connection.Send(str.str().c_str(), (unsigned int)str.str().length()) <= 0)
            {
                // Failed to send, connection must be closed.
                break;
            }
            // Check for exit by user.
            if(GetChar() == 27)
            {
                quitFlag = true;
            }
            SleepMs(250);
        }

        cout << "Connection Closed.\n";
        connection.Shutdown();
    }
}
开发者ID:ShowLove,项目名称:Robotics_Club,代码行数:66,代码来源:example_networking.cpp


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