none
Is that efficient way to shotdown every client in socket? RRS feed

  • General discussion

  • Hello,

    Let's suppose that you have server socket.

    Socket svrSocket = ...

    svrSocket.BeginAccept(new AsyncCallback(AcceptCallback...

    private void AcceptCallback(IAsyncResult ar)
            {
                Socket svrSocket = ar.AsyncState as Socket;
                Socket client = svrSocket.EndAccept(ar);

                // You may want to communicate with client socket.

               //..BeginAccept..

            }

    Let's say you close the SvrSocket. Once you closed the server socket, obviously, client won't be able to connect to this server.

    However,  clients that have been connected will still alive

    For example, let's say you have 10 client connection and close the server socket. At this moment, no more client connection will be allowed,

    but 10 client will still perform on it. My question is how I am supposed to close these client connection gracefully.

    Well, obviously, we could keep track of each client whenever the client was connected.

    Maybe something like...List<Socket> clientPool = new List<Socket>(); clientPool.Add(client);

    When server socket was closed, we could also close these client sockets from this clientPool collection.

    My question is that this is efficient way? Let's say you have tremendous client connection...

    R u going to keep these client connections and....when the server socket was closed, r u going to loop through to close for the each client connection?

    What would be idea way to handle this scenario?

    Thanks,

    Tuesday, July 31, 2012 4:19 PM

All replies

  • Publish an event on server which will fire when the socket is closed. Let the clients subscribe to this event. When the even fires, let the clients close themselves. This is just an idea. I have never implemented this :)


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, July 31, 2012 4:36 PM
  • T J : I agree with your solution as being efficient.  I like to use

    List<Socket> clientPool = new List<Socket>(); clientPool.Add(client);

    You can add a dispose method to clientPool which will call the close method.  Then all you have to do is to dispose the List in a for loop to close all the connection.


    jdweng

    Tuesday, July 31, 2012 4:49 PM
  • Joel,

    Yes, like I said, that could be one of solutions.

    However, think this way if there were tons of client connection has been made in that client pool collection?

    If you loop through, it would take forever to close them all.

    So I just was curious whether there is any better way doing this.

    Thanks,


    • Edited by T J Tuesday, July 31, 2012 5:37 PM
    Tuesday, July 31, 2012 4:52 PM
  • Adavesh,

    How are you supposed to fire event? Since this is socket communication, only thing I could guess is that when the server socket is closed, you would need to send some kind of packet to client..so they know the server is closed, from this point, they can take some closing action gracefully. However, still, isn't it necessary to keep track of each client connection in memory to go through to send the packet? I am not sure but i doubt it would make any difference.

    Thanks


    • Edited by T J Tuesday, July 31, 2012 5:38 PM
    Tuesday, July 31, 2012 4:53 PM
  • There is a finish datagram that is automatically sent over the connection for a TCP.  So you don't have to do anything from the server.

    jdweng

    Tuesday, July 31, 2012 5:17 PM
  • Joel,

    I guess you read the post that I answered for Adavesh.

    What I was telling you is that it seems not efficient way to keep track of each client connection to dispose in a loop.

    Tuesday, July 31, 2012 5:32 PM
  • In this case he doesn't want to "keep track" he just want to close all connections gracefully.


    jdweng

    Tuesday, July 31, 2012 6:12 PM
  • In this case he doesn't want to "keep track" he just want to close all connections gracefully.


    jdweng

    Joel,

    I am not sure what you were talking.

    So I guess you meant he is Adavesh?

    Ok..so how each connected client knows when the server listening socket is closed?

    How your server is going to notify the each client when the server listening socket is closed so they close their connection gracefully from client side?

    Without keeping track of each client connection in server side, how would you do that?

    That's why what I was saying that his approach wouldn't make any difference I think.

    Tuesday, July 31, 2012 6:37 PM
  • The connection is TCP.  When the server closes a TCP connection a finish message is sent across the connection at the transport level.  The Client can capture the finish event in a callback function.  You do not need to (optional) send any messages at the application level.


    jdweng

    Tuesday, July 31, 2012 7:49 PM
  • Joel,

    In server side, when you closed the listening socket, this won't close every client socket connection already has been made.

    For example, you have the listening socket, and already have 10 client connection socket made.

    At this point, when you dispose/close the listening socket, those 10 client connections won't be notified at all.

    Yes, since the listening socket was closed, no more connection could be made. However, those 10 connection has been made still in connection state.

    If you want to close those 10 client connection, you would have to close them explicitly from server side.

    One of way for doing this is to keep the client connection in collection, and dispose them in a loop, which I think it is inefficient if there were tons of client connection in that collection.

    One that Adavesh suggested, as far as I understood his opinion, without keeping track of each connection, his approach cannot be achieved either.

    His idea, if I was correct, was that client would subscribe a kind of event so that it knows when disposing from client side.

    It means server needs to notify that event when the listening socket was closed, so it means server needs to have a collection that keep track of each client connection...

    I am not sure what would be better way, but I guess a collection that keeps track of each client connection would be only way to go.

     

    Tuesday, July 31, 2012 8:01 PM
  • A connection can have only one client and one server.  Each listener has it own connection.  You can have a TCPClient class (at the server) connected to more than one host.  Normally at the server I create a List of Listeners

    List<Socket> clientPool = new List<Socket>(); clientPool.Add(client);

    By adding a dispose method to the clientPool Calls with a disconnect message.  When you close each of the clientPool classes you will close only one connection. 

    "One socket cannot have 10 client connections."


    jdweng

    Tuesday, July 31, 2012 8:48 PM
  • A connection can have only one client and one server.  Each listener has it own connection.  You can have a TCPClient class (at the server) connected to more than one host.  Normally at the server I create a List of Listeners

    List<Socket> clientPool = new List<Socket>(); clientPool.Add(client);

    By adding a dispose method to the clientPool Calls with a disconnect message.  When you close each of the clientPool classes you will close only one connection. 

    "One socket cannot have 10 client connections."


    jdweng

    Joel,

    You still don't get it what I was trying to say.

    "One socket cannot have 10 client connections." -> I am not sure where you get this. I DIDNOT say this.

    What i was saying suppose that you have 10 client connection that already has been made. I didn't mean one socket can have 10 client connections, I meant suppose you server had 10 client connection already has been made.

    I am not sure how I could explain this clearly more.

    List<Socket> clientPool = new List<Socket>(); clientPool.Add(client); -> Yes like I said, this is one way, but I thought it is inefficient. The reason why I asked question in this forum to find out better way than this approach. But it looks like no way.

    If you have 10 socket connections in your server, clientPool will have 10 sockets objects. If you have 10000 socket connections, clientPool will have 10000 socket connections. Think about looping through for 10000 socket objects to close each connection. This would be inefficient in terms of space & time. (Well, realistically, this may not be true though)

    So I was thinking this may not be ideal solution, so I just was looking for some other better way. However, as far as I can think of, there is no other way I guess.




    • Edited by T J Tuesday, July 31, 2012 9:22 PM
    Tuesday, July 31, 2012 9:18 PM
  • How about this http://msdn.microsoft.com/en-us/magazine/cc300760.aspx

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, August 1, 2012 10:53 AM
  • CrazyGhost_Von: Isn't the webpage you posted just the code we have been refering in this posting?  the webpage does exactly what I have been saying in words.

    jdweng

    Wednesday, August 1, 2012 11:02 AM
  • Hi Jdweng,

    Sorry for post duplicate views here. But I havenot found you have mentioned that link.

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Thursday, August 2, 2012 3:18 AM