我的项目是一个客户机-服务器应用程序(C++),更具体地说,是一个群聊。
我在VS17中用两个不同的解决方案构建了客户机和服务器。
现在,我的问题是,当我想将消息从一个客户端发送到服务器,并将消息重定向到连接到的其他客户端时,我不想成为一个阻塞函数,所以我used _kbhit()函数,但我不能正常工作。除了kbhit() + getch()或cin之外,在客户端是否有其他的输入方式?
客户端
char buffer[20];
int point = 0;
while (1)
{
if (!_kbhit()) {
char cur = _getch();
if (point > 19)
point = 19;
std::cout << cur;
if (cur != 13)
buffer[point++] = cur;
else {
buffer[point] = '\0';
point = 0;
}
}
BytesReceived = recv(sock, buf, 4096, 0);
if (BytesReceived != 0 && BytesReceived != -1)
{
cout << BytesReceived << endl;
cout << string(buf, 0, BytesReceived) << endl;
ZeroMemory(buf, 4096);
}
//cin >> userInput;
// non blocking input
//if(userInput.size()>0)
//int sendResult = send(sock, userInput.c_str(), userInput.size(), 0);
int sendResult = send(sock, buffer, point+1, 0);发布于 2018-07-09 13:02:01
在ncurse中,您可以尝试使用nodelay()函数将getch()转换为非阻塞调用,如果没有可用的键按,则返回ERR。
发布于 2018-07-09 17:59:13
https://stackoverflow.com/questions/51246143
复制相似问题