c# - How to add delays in thread-safe manner (TcpClient) -


i'm creating project read in list of emails , using external libraries, mx records domain, , use telnet determine whether or not email exists.

i have managed make work far - due how fast code runs, believe i'm attempting read server's response quicker being sent. (i assume this, code return empty strings when run without debugging, breakpoints returns fine.)

to counter-act added thread.sleep() in areas around code wait response, works locks thread , stops responding several minutes.

how able perform task without locking ui?
here code class issue -

class telnetconnection {     tcpclient tcpclient;     list<char> reply;     public telnetconnection(string hostname, int port)     {         tcpclient = new tcpclient();          tcpclient.beginconnect(hostname, port, null, null);         thread.sleep(2000);     }      public list<char> getreply()     {         reply = new list<char>();         stringbuilder sb = new stringbuilder();         while (tcpclient.available > 0)         {             parsetelnet(sb);         }         (int = 0; < sb.length; i++)         {             reply.add(sb[i]);         }         return reply;     }     public list<char> greet(string greeting)     {         writeline(greeting);         thread.sleep(2000);         return getreply();     }     public list<char> mailfrom(string mailfrom)     {         writeline(string.concat("mail from: <", mailfrom, ">"));         thread.sleep(2000);         return getreply();     }     public list<char> rcptto(string rcptto)     {         writeline(string.concat("rcpt to: <", rcptto, ">"));         thread.sleep(2000);         return getreply();     }      public void writeline(string cmd)     {         write(cmd + "\n");     }      public void write(string cmd)     {         if (!tcpclient.connected) return;         byte[] buf = system.text.asciiencoding.ascii.getbytes(cmd.replace("\0xff", "\0xff\0xff"));         tcpclient.getstream().write(buf, 0, buf.length);     }      public bool isconnected     {         { return tcpclient.connected; }     }      // method external library     void parsetelnet(stringbuilder sb)     {             try             {                 int input = tcpclient.getstream().readbyte();                 switch (input)                 {                     case -1:                         break;                     case (int)verbs.iac:                         // interpret command                         int inputverb = tcpclient.getstream().readbyte();                         if (inputverb == -1) break;                         switch (inputverb)                         {                             case (int)verbs.iac:                                 //literal iac = 255 escaped, append char 255 string                                 sb.append(inputverb);                                 break;                             case (int)verbs.do:                             case (int)verbs.dont:                             case (int)verbs.will:                             case (int)verbs.wont:                                 // reply commands "wont", unless sga (suppres go ahead)                                 int inputoption = tcpclient.getstream().readbyte();                                 if (inputoption == -1) break;                                 tcpclient.getstream().writebyte((byte)verbs.iac);                                 if (inputoption == (int)options.sga)                                     tcpclient.getstream().writebyte(inputverb == (int)verbs.do ? (byte)verbs.will : (byte)verbs.do);                                 else                                     tcpclient.getstream().writebyte(inputverb == (int)verbs.do ? (byte)verbs.wont : (byte)verbs.dont);                                 tcpclient.getstream().writebyte((byte)inputoption);                                 break;                             default:                                 break;                         }                         break;                     default:                         sb.append((char)input);                         break;                 }             }             catch (exception ex)             {                 throw new exception(ex.message, ex);             }     } } 

remove sleeps. can't know how long take server respond.

instead, agree upon marker in data sent server client , marks end of response.

in getreplay read stream until encounter marker , return.

edit looks parsetelnet method tries this. make sure server sending expecting.

an alternative have server send length of reply first client has read amount of bytes.

an agreement called protocol.

to prevent blocking ui perform sending , reading asynchronously using *async methods of networkstream.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -