2019独角兽企业重金招聘Python工程师标准>>>
同样的,我们将UDP版的doEcho()也设计成返回bool:true表示循环继续;false表示关闭客户端。
class UDPEchoClient: public UDPClientSock{
public:
explicit UDPEchoClient(
int pre_buffer_size = 32);
~UDPEchoClient();
bool doEcho(const std::string& echo_message);
};
我们依然使用C++字符串。
UDPEchoClient::UDPEchoClient(
int pre_buffer_size):
UDPClientSock(pre_buffer_size)
{}
UDPEchoClient::~UDPEchoClient()
{}
bool UDPEchoClient::doEcho(const std::string& echo_message)
{
if ( UDPSendtoDest(echo_message.data(), echo_message.size()) < 0) {
return false;
}
if (echo_message == "/shutdown") {
return false;
}
if (UDPReceive() < 0) {
return false;
}
std::cout.write(preBuffer, preReceivedLength);
std::cout << std::endl;
return true;
}
当echo_message为“空”的时候,即输入直接回车,是一个"",用C风格来说,即时'