[英]Can't figure out how to implement threads in my client-server model
I have a small server-client application that doesn't do very much (a client connects to the server, sends a number trough a pipe and receives another number). 我有一个小型的服务器-客户端应用程序,该应用程序做得并不多(一个客户端连接到服务器,通过管道发送一个数字并接收另一个数字)。
But it only works with one connection at a time (While a client is connected, no other client has access to the server) 但是一次只能使用一个连接(虽然连接了一个客户端,但是没有其他客户端可以访问该服务器)
I want to make it possible for multiple clients to connect to the server at one time, and I plan to do this with worker threads. 我想让多个客户端一次连接到服务器成为可能,并且我计划使用辅助线程来做到这一点。
Obs: Obs:
#define CONNECT_NAMEDPIPE "\\\\.\\pipe\\ClientToServer"
Server: 服务器:
HANDLE namedPipe = CreateNamedPipe (CONNECT_NAMEDPIPE,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
2,
sizeof(MinMax),
sizeof(NumberList),
0, // timeout
NULL);
if (namedPipe == INVALID_HANDLE_VALUE) {
printf("Unable to create named pipe\r\nServer closing\r\n");
printf("CreateNamedPipe failed, GLE=%d.\r\n", GetLastError());
} // Error unable create pipe
else {
printf("Server created\r\n");
printf("Awaiting connection\r\n");
ConnectNamedPipe(namedPipe, NULL);
etc ...
}
So the server waits on ConnectNamedPipe until a client connects, then is unavailable for any other connections. 因此,服务器等待ConnectNamedPipe直到客户端连接为止,然后对于任何其他连接均不可用。
If I'd like to enable multiple connections, how should I create the worker threads ? 如果要启用多个连接,应如何创建辅助线程?
Should every connection attempt create a new pipe (with a new pipe name / path - CONNECT_NAMEDPIPE can't be used for all) 如果每个连接尝试都创建一个新管道(具有新的管道名称/路径-CONNECT_NAMEDPIPE不能全部使用)
How do I know when someone else is trying to connect ? 我怎么知道其他人何时尝试连接? Where should my threads be ?
我的线程应该在哪里? I'm stuck.
我被卡住了。
I think Berkeley sockets are better suited for this. 我认为伯克利插座更适合于此。 If you must go with pipes, something like this could work:
如果必须使用管道,则可以执行以下操作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.