c# - ~1 second TcpListener Pending()/AcceptTcpClient() lag -


probably watch video: http://screencast.com/t/owe1owvko see, delay between connection being initiated (via telnet or firefox) , program first getting word of it.

here's code waits connection

    public idlserver(system.net.ipaddress addr,int port)     {         listener = new tcplistener(addr, port);          listener.server.nodelay = true;//i added testing, has no impact          listener.start();          connectionthread = new thread(connectionlistener);         connectionthread.start();       }      private void connectionlistener()     {         while (running)         {             while (listener.pending() == false) { system.threading.thread.sleep(1); }//this part lag             console.writeline("client available");//from point on runs fast              tcpclient cl = listener.accepttcpclient();               thread proct = new thread(new parameterizedthreadstart(instancehandler));             proct.start(cl);           }      } 

(i having trouble getting code code block)

i've tried couple different things, i'm using tcpclient/listener instead of raw socket object? it's not mandatory tcp overhead know, , i've tried running in same thread, etc.

you should consider accepting clients asynchronously, remove lag seeing.

i've modified code slightly

public idlserver(system.net.ipaddress addr,int port) {     listener = new tcplistener(addr, port);      listener.start();              // use beginxxxx pattern accept clients asynchronously     listener.beginaccepttcpclient(this.onacceptconnection,  listener); }  private void onacceptconnection(iasyncresult asyn)  {     // listener handles client request.     tcplistener listener = (tcplistener) asyn.asyncstate;      // newly connected tcpclient     tcpclient client = listener.endaccepttcpclient(asyn);      // start client work     thread proct = new thread(new parameterizedthreadstart(instancehandler));     proct.start(client);      // issue connect, if want handle multiple clients     listener.beginaccepttcpclient(this.onacceptconnection,  listener);     } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -