[英]Client-Server C++ alternative for _kbhit() function
我的项目是客户端服务器应用程序(C ++),更具体地说是群聊。
我在VS17中的2种不同解决方案中构建了客户端和服务器。
现在,我的问题是,当我想将消息从一个客户端发送到服务器,并且将消息重定向到连接的其他客户端时,我不想成为阻止函数,因此我used _kbhit()
函数,但是我工作不正常。 除了kbhit()
+ getch()
或cin
之外,还有什么其他替代品可以输入Client?
客户
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);
在ncurses中,您可以尝试使用nodelay()
函数将getch()转换为非阻塞调用,如果没有可用的按键,则返回ERR。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.