简体   繁体   English

无法弄清楚如何在我的客户端-服务器模型中实现线程

[英]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: 如果必须使用管道,则可以执行以下操作:

  1. The client sends a connection request through the main named pipe to the control thread. 客户端通过主命名管道向控制线程发送连接请求。
  2. The server creates (or fetches from a pool) a worker thread that listens on another, unique pipe. 服务器创建(或从池中获取)工作线程,该工作线程在另一个唯一的管道上侦听。
  3. The control thread answers with the name of this new pipe. 控制线程使用此新管道的名称进行回答。
  4. The client closes the control pipe and sends real request data to the new pipe. 客户端关闭控制管道,并将实际请求数据发送到新管道。
  5. The worker thread reads the request, processes it, and sends back the response. 辅助线程读取请求,进行处理,然后发送回响应。
  6. Meanwhile the control thread is ready to read another connection request from another client. 同时,控制线程已准备好从另一个客户端读取另一个连接请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM