Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
ListenFromClient( TCPServerForClient.AcceptSocket( ) );


in the thread instead?

Code Below:

C#
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:

C#
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
Posted
Updated 19-Dec-18 14:35pm

1 solution

Thanks. I'm going down the route of converting my Dictionary to a List type as thought i might.
 
Share this answer
 
Comments
CHill60 20-Dec-18 4:42am    
If you want to reply to a post then use the "Have a Question or Comment?" link next to it. The poster is notified of your response.
Never post comments or questions as solutions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900