-2
我有通过TCP套接字连接到服务器,下面一个客户端程序:读/打印插座输出中(W/O多线程)
int main ()
{
std::cout << "HunterChat client starting up" << std::endl;
std::string cmd;
std::string reply;
bool cont = true;
ClientSocket client_socket ("localhost", PORT);
try {
while(cont) {
try {
std::cout << ">> ";
// std::getline(std::cin,cmd);
gets(cmd);
if(cmd.compare("logout") == 0) {
cont = false;
break;
}
client_socket << cmd;
client_socket >> reply;
std::cout << reply << std::endl;
}
catch (SocketException& e) {
std::cout << "Exception was caught:" << e.description() << "\n";
}
}
}
catch (SocketException& e) {
std::cout << "Exception was caught:" << e.description() << "\n";
}
return 0;
}
ClientSocket的是一个自定义类,让我建立并使用TCP连接;流运营商有过多的,下面的代码:
int status = ::send (m_sock, s.c_str(), s.size(), MSG_NOSIGNAL);
if (status == -1)
{
return false;
}
else
{
return true;
}
的TCP连接本身工作正常,所以我不会有更多的它弄乱张贴。问题在于其中一个可用命令涉及在客户端仍在等待cin输入时向客户端实例发送输入。这意味着当我在cin中键入内容时,服务器消息只能被读取和写入。我试图避免使用多线程,所以有没有办法让cin在没有它的情况下被中断?
解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你应该[编辑]你的问题,以包含一个[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子来重现你的问题,以及你在调试器中所做的观察。 –