C#存储信息并正确清除 [英] C# storing info and clearing correctly

查看:66
本文介绍了C#存储信息并正确清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在c#中有一个Socket服务器和客户端。当有人连接时,我存储该套接字,我还在字典中存储了一堆关于它的信息。假设其中一个断开连接,那么清除特定信息的最佳方法是什么?另外,我是否需要存储acceptedSockets?我没有使用它所以我应该致电



 ListenFromClient(TCPServerForClient.AcceptSocket()); 





在线程中呢?



代码如下:



  class  ClientInfo 
{
public string strTimeConnected = string .Empty;
public Socket clientSocket,technicianSocket;
}

// 用于计算连接客户端的数量
列表< ClientInfo > clients = new 列表< ClientInfo > ();

// 用于存储连接数
List< ;套接字> acceptedSockets = new 列表<套接字> ();

// 保存ID并收集客户信息
字典< string ,ClientInfo > clientMap1 = new 字典< string ,ClientInfo > ();
// 保留收集技术人员和技术信息的插槽
字典<套接字,ClientInfo > clientMap2 = 字典<套接字,ClientInfo > ();





比服务器后面更远正在运行我这样做:



套接字newAcceptedClientSocket = TCPServerForClient.AcceptSocket(); 

// 从客户端建立连接后,添加到接受的套接字
acceptedSockets.Add(newAcceptedClientSocket);

// 启动新线程并等待接收客户端命令
new 主题(()= >
{
// 在后台生成线程
Thread.CurrentThread.IsBackground = true ;

// 收听什么客户端说
ListenFromClient(newAcceptedClientSocket);
})。Start();

clientNew.strTimeConnected = DateTime.Now.ToString( HH:mm:ss tt );
clientNew.clientSocket = clientSocket;
clientNew.technicianSocket = null ;
clients.Add(clientNew);

if (!clientMap2.TryGetValue(clientSocket, out tmp))
clientMap1.Add(clientNew.strId,clientNew);





提前致谢。我需要信息,但如果有人断开连接,也需要它。也许我存储错误并有更好的方法?



我尝试过的方法:



迭代数组,但字典中没有顺序,也没有索引。



考虑使用数组而不是

解决方案

任何与信号器相同的套接字服务器都会为您提供来自每个客户端的唯一信息。例如他们的ip地址。

如果你在signalr中使用持久连接然后onclose连接你可以从你的字典中删除那个ip,但正如你所说arraylist是保存套接字数据的更好选择。

感谢。我正按照我的意愿将我的字典转换为List类型。


Hi All,

I have a Socket server and Client in c#. When someone connects, i store that socket and i also store a bunch of info about it in a dictionary. Say one of them disconnects, whats the best way to clear that particular info? Also, do i need to even store acceptedSockets? I'm not using it so should i just call

ListenFromClient( TCPServerForClient.AcceptSocket( ) );



in the thread instead?

Code Below:

class ClientInfo
        {
            public string strTimeConnected  =   string.Empty;
            public Socket clientSocket, technicianSocket;
        }

        //Used to count the number of connected clients
        List< ClientInfo >  clients                 =   new List< ClientInfo >( );

        //Used to store the number of connections
        List< Socket >      acceptedSockets         =   new List< Socket >( );
        
        //Holds an ID and collected info of client
        Dictionary< string, ClientInfo > clientMap1 =   new Dictionary< string, ClientInfo >( );
        //Holds Socket of technician and tech info collected
        Dictionary< Socket, ClientInfo > clientMap2 =   new Dictionary< Socket, ClientInfo >( );



Than further down after server is running i am doing this:

Socket newAcceptedClientSocket = TCPServerForClient.AcceptSocket( );

//When Connection From Client Is Made, Add To The Accepted Sockets
acceptedSockets.Add( newAcceptedClientSocket );

                //Launch A New Thread And Wait For A Client Command To Be Received
new Thread( ( ) =>
{
    //Spawn The Thread In The Background
    Thread.CurrentThread.IsBackground = true;

    //Listen To What The Client Is Saying
    ListenFromClient( newAcceptedClientSocket );
} ).Start( );

clientNew.strTimeConnected      =   DateTime.Now.ToString( "HH:mm:ss tt" );
clientNew.clientSocket          =   clientSocket;
clientNew.technicianSocket      =   null;
clients.Add( clientNew );

if( !clientMap2.TryGetValue( clientSocket, out tmp ) )
     clientMap1.Add( clientNew.strId, clientNew );



Thanks in advance. I need the info, but also need it gone if someone disconnects. Maybe i'm storing it incorrectly and there's a better way?

What I have tried:

Iterating array, but there is no order in a dictionary, nor an index.

Thought about using array instead

解决方案

Any socket server same as signalr give you a unique info from each client coming . for example their ip addresses .
if you are using persistent connection in signalr then onclose connection you can delete that ip from your dictionary but as you said arraylist is a better choice for holding socket data.


Thanks. I'm going down the route of converting my Dictionary to a List type as thought i might.


这篇关于C#存储信息并正确清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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