C ++ Winsock - 服务器只与单个客户端通信,而它应该与每个客户端通信
c++ winsock - server communicates only with single client while it should communicate with every client
我正在编写一个带有GUI的聊天程序。我想编写一个可以接受许多客户端的服务器。每个客户端都可以成功连接。但是发送和接收数据存在一个奇怪的问题。我使用 select(( 和一个线程同时处理许多套接字。如果客户端向服务器发送一些数据,它将接收它并将其发送回该客户端(特别是客户端在没有"预测"的情况下编写(。但是服务器不会将其进一步发送到另一个客户端(就像每个客户端都与服务器进行自己的私人对话一样(。这是我的代码:
// this is rewritten from the Beej's tutorial with a little and insignificant changes
/* in the thread */
fd_set mainfd;
fd_set readfd;
// sin-size, newfd, maxfd - int
while(TRUE)
{
readfd = mainfd;
if(select(maxfd+1, &readfd, NULL, NULL, NULL) == -1)
{
MessageBoxA(NULL, "Error while trying to accept incoming connections (select)", "Error", 16);
itoa(GetLastError(), buf, 10);
MessageBoxA(NULL, buf, buf, 0);
break;
}
for(int i = 0; i <= maxfd; i++)
{
char* psr;
char srMsg[256];
if(FD_ISSET(i, &readfd))
{
if(i == mainSocket)
{
sin_size = sizeof(their_addr);
newfd = accept(mainSocket, (struct sockaddr*)&their_addr, &sin_size);
if(newfd == SOCKET_ERROR)
{
AddTextToEdit(hStaticChat, "* Error: couldn't accept incoming connection.", TRUE);
}
else
{
FD_SET(newfd, &mainfd);
if(newfd > maxfd)
{
maxfd = newfd;
}
}
}
else
{
len = recv(i, srMsg, 256, 0);
if(len == 0 || len == SOCKET_ERROR)
{
AddTextToEdit(hStaticChat, "* Client has disconnected", TRUE);
close(i);
FD_CLR(i, &mainfd);
}
else
{
AddTextToEdit(hStaticChat, srMsg, TRUE);
for(int j = 0; j <= maxfd; j++)
{
if(FD_ISSET(j, &readfd))
{
send(j, srMsg, len, 0);
}
}
}
}
}
}
}
您只将数据发送到 fd 处于readfd
中的客户端,也就是说,仅发送到刚刚与您通信的客户端。尝试改为测试FD_ISSET(j, mainfd)
。
此代码在 WinSock 下无效。 Windows 不像其他平台那样使用整数文件描述符处理套接字。 套接字改为使用实际的内核对象表示,因此不能将循环计数器用作套接字句柄等。 还有 API 差异(closesocket()
而不是 close()
,maxfd
被 select()
忽略,FD_XXX()
期望SOCKET
句柄而不是int
,等等(。
在Windows上,你需要使用更像这样的东西:
fd_set mainfd;
SOCKET newfd;
int sin_size;
...
while(TRUE)
{
fd_set readfd = mainfd;
if (select(0, &readfd, NULL, NULL, NULL) == SOCKET_ERROR)
{
itoa(WSAGetLastError(), buf, 10);
MessageBoxA(NULL, "Error while trying to accept incoming connections (select)", "Error", 16);
MessageBoxA(NULL, buf, buf, 0);
break;
}
for(int i = 0; i < readfd.fd_count; i++)
{
if (readfd.fd_array[i] == mainSocket)
{
sin_size = sizeof(their_addr);
newfd = accept(mainSocket, (struct sockaddr*)&their_addr, &sin_size);
if (newfd == INVALID_SOCKET)
{
AddTextToEdit(hStaticChat, "* Error: couldn't accept incoming connection.", TRUE);
}
else
{
// Note that fd_set can only hold FD_SETSIZE (64) sockets as a time!
FD_SET(newfd, &mainfd);
}
}
else
{
char srMsg[257];
len = recv(readfd.fd_array[i], srMsg, 256, 0);
if (len < 1)
{
if (len == 0)
AddTextToEdit(hStaticChat, "* Client has disconnected", TRUE);
else
AddTextToEdit(hStaticChat, "* Error: couldn't read from a client connection.", TRUE);
closesocket(readfd.fd_array[i]);
FD_CLR(readfd.fd_array[i], &mainfd);
}
else
{
srMsg[len] = 0;
AddTextToEdit(hStaticChat, srMsg, TRUE);
for (int j = 0; j < mainfd.fd_count; j++)
{
if (mainfd.fd_array[i] != mainSocket)
send(mainfd.fd_array[j], srMsg, len, 0);
}
}
}
}
}
相关文章:
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 提升::Asio 异步聊天客户端停止与服务器通信
- UDP客户端/服务器程序在不同的IP和端口上通信超过2个插座
- 如何让客户端通过modbus-tcp协议与多个服务器通信?
- 如何将数据序列化为C++ zmq 客户端和 Python zmq 服务器之间的通信
- 使用UNIX套接字C++客户端与Node.js服务器通信的简单示例
- 嗅探 QWebsocket 服务器 - 客户端通信
- 证书验证在客户端服务器通信中使用boost :: asio和openssl失败
- 客户端JavaScript和C 套接字服务器之间的通信
- 如何在服务器和客户端之间进行异步通信
- C++:一个客户端与多个服务器通信
- C++:服务器和客户端之间的经典通信练习
- C++ 客户端无法与 Java 服务器通信
- 使用 C/C++、SSL 协议的客户端和服务器通信不起作用
- 如果传递长类型,32位客户端如何与64位服务器通信
- c++tcp多线程客户端/服务器-如何与线程套接字处理程序进行通信
- 如何中断客户端和服务器的通信
- Raspberry Pi 和 Android 之间的客户端-服务器通信
- C/C++服务器,通过stdin/stdout与客户端通信(阻塞stdin,直到读取了大量字节)
- 具有进程间通信功能的服务器-客户端日志记录体系结构.你知道解决方案吗?