排序套接字作为客户端对象 [英] Sorting Sockets as Client Objects

查看:146
本文介绍了排序套接字作为客户端对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用C#聊天程序,和我有一个关于客户端连接的问题。

I'm currently making chat program in C#, and I have a question regarding connection of clients.

我怎样才能让每一个客户端(插座)连接到服务器有一个名字和更多的细节?

How can I make each client (socket) that connects to the server have a name and more details?

例如:

乔连接到服务器。一个新的对象,C1,将创建包含他的名字和IP(可观看/静音/私人信息的目的)。

Joe connects to server. A new object, c1, is created containing his name and IP (for kicking/muting/private messaging purposes).

后来,詹姆斯连接到服务器。一个新的对象,C2是创建一个包含名称和IP詹姆斯。

Later, James connects to server. A new object, c2 is created containing the name and IP of James.

等等...

我需要的客户为一些主要目的对象。

I need to make the clients as objects for few main purposes.

举个例子,如果我想踢某人聊天,我可以写在服务器/脚,它会踢他。

As an example, if I want to kick someone from the chat, I can write in the server "/kick " and it'll kick him.

我希望服务器检查与该名客户。如果它的连接,踢它。

I want the server to check for a client with that name. If its connected, kick it.

要做到这一点的唯一方法是使对象为每个客户端为这些确切的目的。

The only way to do it is to make an object for each client for these exact purposes.

那么,如何可以使一个对象的每个插座?

So how can I make an object for each socket?

推荐答案

只是包装类中的所有相关信息:

Just wrap all related information in a class:

class ClientInfo {
 public string Name { get; set; }
 public Socket Socket { get; set; }
}

您只需要那些一个列表:

You just need a single list of those:

List<ClientInfo> clients = new List<ClientInfo>();

您可以轻松地添加客户端当一个人连:

You can easily add a client when one has connected:

clients.Add(new ClientInfo() { Name = "x", Socket = someSocket });

您也可以按名称和插座寻找客户:

You can also find clients by Name and by Socket:

var client = clients.SingleOrDefault(x => x.Name == "x");
client.Socket.Send(...);

这篇关于排序套接字作为客户端对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆