How to remove disconnected sockets


i using socket server program accepting more 1000 clients concurrently. there few things

1. client device make connection server.
2. sending data every 10 seconds without disconnection.
3. client device not concerned acknowledgment. sending data every 10 seconds , forget.
4. server knows data format client sending.

there few questions...

 1. how receive data client without disconnection (unless client disconnects itself)?

 2. updating no of clients connected in file. seems no of clients increasing (some time decreasing increase rate higher). how keep connected clients count more accurate?

here code...

 public static manualresetevent alldone = new manualresetevent(false);         // private static datatable dtserviceid = general.getallserviceid();          private static int numconnections;          private static int disconnected;            public static void startlistening()          {              ipendpoint ipendpoint = new ipendpoint(dns.resolve(dns.gethostname()).addresslist[0], globalvariable.listenport);              socket listener = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);                            try              {                                    listener.bind((endpoint)ipendpoint);                  listener.listen((int)socketoptionname.maxconnections);                                    while (true)                  {                      alldone.reset();                      try                      {                          listener.beginaccept(new asynccallback(acceptcallback), listener);                      }                      catch (exception ex)                      {                                                }                         alldone.waitone();                      }              }              catch (exception ex)              {                                }          }            public static void acceptcallback(iasyncresult ar)          {              try              {                                   alldone.set();                   socket listener = (socket)ar.asyncstate;                  socket handler = ((socket)ar.asyncstate).endaccept(ar);                    interlocked.increment(ref numconnections);                                // writing no of clients in file                    stateobject stateobject = new stateobject();                  stateobject.worksocket = handler;                    handler.beginreceive(stateobject.buffer, 0,stateobject.buffer.length, socketflags.none, new asynccallback(readcallback), stateobject);                                }              catch (exception ex)              {                               }          }            public static void readcallback(iasyncresult ar)          {              string content="";              stateobject stateobject = (stateobject)ar.asyncstate;              socket handler = stateobject.worksocket;              try              {                  int count = handler.endreceive(ar);                  if (!handler.connected || count<=0)                  {                      closeconnection(handler);                                            return;                  }                      stateobject.sb.append(encoding.ascii.getstring(stateobject.buffer, 0, count));                    content = (stateobject.sb).tostring().trim();                  stateobject.sb.clear();                  if (content != "")                  {                                          // doing data insertion                  }                  handler.beginreceive(stateobject.buffer, 0, stateobject.buffer.length, socketflags.none, new asynccallback(readcallback), stateobject);                }              catch (socketexception se)              {                                   closeconnection(handler);              }              catch (exception ex)              {                                    closeconnection(handler);                                }          }            private static void closeconnection(socket handler)          {              interlocked.decrement(ref numconnections);              interlocked.increment(ref disconnected);             // writing no of clients in file                handler.dispose();          }  

there no inbuilt timeout on of socket objects.

i don't think there's last time of communication either.

hence suggestion  keep collection of ones have open.

write code see when last did whatever , close them based on this.

you have list<t> except have thousand of them , you're updating last time of each many times dictionary more efficient.

 dictionary<socket, datetime> lastcomms = new dictionary<socket, datetime>();

you create entry open connection , update each time receive.

then iterate , close on 30 seconds old, remove entry dictionary.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

Catalog Staging

How to Share webservice object to all user