本文整理汇总了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;
}
}