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;
}
List< ClientInfo > clients = new List< ClientInfo >( );
List< Socket > acceptedSockets = new List< Socket >( );
Dictionary< string, ClientInfo > clientMap1 = new Dictionary< string, ClientInfo >( );
Dictionary< Socket, ClientInfo > clientMap2 = new Dictionary< Socket, ClientInfo >( );
Than further down after server is running i am doing this:
Socket newAcceptedClientSocket = TCPServerForClient.AcceptSocket( );
acceptedSockets.Add( newAcceptedClientSocket );
new Thread( ( ) =>
{
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 );
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