简体   繁体   English

存储连接的客户端?

[英]Storing connected clients?

I am currently learning and working on a Client/Server, and I am having some difficult on how I should store the connections. 我目前正在学习和使用客户端/服务器,并且在如何存储连接方面遇到一些困难。

Here is some sample code: 这是一些示例代码:

while (_isRunning)
{
    if (!tcpListener.Pending())
    {
        Thread.Sleep(200);
        continue;
    }

    TcpClient client = tcpListener.AcceptTcpClient();
    // TODO: handle connection
}

I know I will have a handler that will receive/send data to that given client however, just having the handler would not allow me to do things like knowing how many clients are connected, sending a broadcast message or a shutdown message to all. 我知道我将有一个处理程序来接收/发送数据到给定的客户端,但是,只有具有该处理程序,我才能做一些事情,例如知道已连接多少个客户端,向所有人发送广播消息或关闭消息。

Would storing client on a list be fine or should I store anything else ? 将客户端存储在列表中是否可以,还是应该存储其他内容?

What would be the correct way to store the clients connected in this case ? 在这种情况下,存储连接的客户端的正确方法是什么?

For most common usage patterns keeping a List of connected clients will be enough. 对于大多数常见的使用模式,仅保留一个已连接客户端List The main point of interest will be deciding: a list of what ? 主要的兴趣点是确定: 什么清单?

For many applications pure TCP functionality is not convenient to use, so you would need a layer on top of it that transforms the stream into something more like a message queue from which whole self-contained "messages" can be pulled; 对于许多应用程序而言,纯TCP功能不方便使用,因此您需要在其之上的一层将流转换为更像消息队列的东西,从中可以提取整个自包含的“消息”。 the format of those messages is dependent on your application. 这些消息的格式取决于您的应用程序。

I dug up an older project of mine that is a client-server configuration where multiple clients connect to the server and offer remote control functionality. 我挖出了我的一个较旧的项目,它是一个客户端-服务器配置,其中多个客户端连接到服务器并提供远程控制功能。 In that project the server keeps a List<ConnectedClient> where: 在该项目中,服务器保留一个List<ConnectedClient> ,其中:

// This is the whole actual class
private class ConnectedClient
{
    public TcpMessageConnection Connection { get; private set; }

    public bool ConnectedEventRaised { get; set; }

    public ConnectedClient(TcpMessageConnection connection)
    {
        this.Connection = connection;
    }
}

And TcpMessageConnection looks like: TcpMessageConnection看起来像:

// Only class outline shown
public class TcpMessageConnection
{
    private readonly MessageStream messageStream; // another custom class

    private readonly TcpClient tcpClient;

    public EndPoint RemoteEndPoint { get; private set; } // who connected here?

    public MessageReader MessageReader { get; } // messageStream.Reader

    public MessageWriter MessageWriter { get; } // messageStream.Writer

    public bool IsCurrentlyDisconnecting { get; private set; }

    public void Close();
}

Together with MessageReader and MessageWriter (which do the real heavy lifting) this is enough to implement an async-based server that services multiple clients on a single thread. 连同MessageReaderMessageWriter (它们真正完成繁重的工作)一起,足以实现一个基于异步的服务器,该服务器在单个线程上为多个客户端提供服务。

With a server you are typically not so much interested in the socket as you are in what the socket is for. 使用服务器时,您对套接字的兴趣通常不如套接字的用途大。 The approach that I would take would be to have a class that manages the server end of the conversation between the client and the server. 我将采用的方法是拥有一个类来管理客户端与服务器之间的对话的服务器端。 This class would be based on a thread so the idea is that each time a connection is accepted, you spin off a thread to handle the conversation and in that thread is a socket for the server end of the connection. 该类将基于线程,因此其思想是,每次接受连接时,您都会剥离一个线程来处理对话,并且该线程中是连接服务器端的套接字。

At this point you would be managing the various threads rather than individual sockets. 此时,您将管理各种线程而不是单个套接字。 This is a higher level of abstraction and more useful. 这是更高的抽象级别,更有用。

How you manage the threads would depend on what you want to do. 您如何管理线程取决于您要做什么。 It may be that you really do not want to worry about it. 可能您确实不想为此担心。 As each thread does its own thing, when it is done, it just closes its end of the TCP connection and exits. 由于每个线程都在做自己的事情,完成后,它只会关闭TCP连接的末端并退出。

However until it does exit, the thread class handles the server side of the conversation. 但是,直到退出之前,线程类都会处理会话的服务器端。

I suggest you check this stackoverflow, Best way to accept multiple tcp clients . 我建议您检查此stackoverflow, 接受多个tcp客户端的最佳方法

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

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