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


C++ TcpSocket::setMaxReceive方法代码示例

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


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

示例1: readLinesFromClientTest

bool readLinesFromClientTest(const int linesize=TcpSocket::DEFAULT_MAX_MSG){
    cout << "Starting readLinesFromClientTest(" << linesize << ") ...\n";
    string message("Passaro verde abandona ninho. \nEscuto!\n");

    pid_t child;
    // client code
    if ((child=fork())==0){
        try {
            TcpSocket toServer;
            cout << "  Forked child waiting a bit\n";
            sleep(1);
            cout << "  Forked child connecting to server\n";
            toServer.connect("127.0.0.1",LOCALPORT);
            cout << "  Forked child connected: " << toServer << endl;
            cout << "  Forked child sending to server\n";
            toServer.writeline(message);
            cout << "  Forked child closing connection to server\n";
            toServer.close();
            cout << "  Forked child exiting\n";
            exit(0);
        } catch (std::exception& e) {
            cout << "  Forked child exception: " << e.what() << endl;
            exit(-1);
        }
        return false; // for clarity
    }

    // server code
    TcpSocket* connectedSocket;
    try {
        TcpSocket serverSocket;
        connectedSocket = bindListenAccept(LOCALPORT, serverSocket);
        std::string clientmessage;
        cout << "  Reading one (at most " << linesize << " chars long) line from client\n";

        // Read a line
        connectedSocket->setMaxReceive(linesize);
        while ((*connectedSocket) >> clientmessage);
        if (clientmessage.empty())
            throw std::runtime_error("clinet message is empty");

        cout << "  Read: \"" << clientmessage << "\"\n";
        if (!(clientmessage.compare(message) == 0)){
            
            throw std::runtime_error("Messages dont match");
        }
        checkChild(child);
        serverSocket.close();
        connectedSocket->close();
        cout << "Done!\n";
        delete connectedSocket;
        return true;
    } catch (std::exception& e) {
        cout << "  Server exception: " << e.what() << endl;
        cout << "Failed!\n";
        checkChild(child, true);
        delete connectedSocket;
        return false;
    }
}
开发者ID:joaotavora,项目名称:minns,代码行数:60,代码来源:TcpSocketUnit.cpp


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