我想写一个服务器,接受来自多个客户端的邮件,直到它收到N个邮件,处理这批数据并将回复“完成”发送给所有客户端。套接字:发送邮件给几个客户端
如果我从客户端收到数据后立即向客户端发送消息“完成”,此代码正常工作。在批处理完成后,如何“保存”所有客户端并在稍后发送消息给每个客户端?
while (true) {
listen(sock, 1000);
newsock = accept(sock, (struct sockaddr *) &cli_addr, &clilen);
n = read(newsock, buffer, read_len);
if (n < 0) {
cout << "ERROR reading from the socket" << endl;
continue;
}
memcpy(data + (msg_count * tuple_size), buffer, tuple_size);
n = write(newsock, "done\n", 5); //sending the message to the current client
msg_count++;
if (msg_count >= batch_size) {
msg_count = 0;
doSomethingWithTheData(data);
//I want to send the message to all the clients here
}
bzero(buffer, read_len);
}
您是否尝试过设置Cookie? – Arash
@ArashMohammadi不,我还没有尝试过,现在我要读一下它。如果服务器和所有客户端都在同一台PC上,Cookie会起作用吗? – vgeclair
如果客户端可以发送多条消息,则可能需要查看“select”或“epoll”。 – user4581301