Telnet/TcpClient C# Skipping Code and not iterating through loop -


this going sound silly reason code seems skip on read. or maybe going fast? i'm trying telnet response server , first line response nothing more. doesn't matter how many readline() put or if try sleep thread. can whole response server printed? think narrowed problem down exiting. in sample program if hard code exit, ends loop , displays nothing. tried sleeping thread seemed stop everything.

output:

220 server-12.tower-558.messagelabs.com esmtp 

expected output:

250-server-11.tower-555.messagelabs.com says ehlo iphere 250-pipelining 250-8bitmime 250-starttls 

my code:

 //telnet start  iphostentry hostinfo = dns.resolve(list[j]);  telnetconnection tc = new telnetconnection(hostinfo.addresslist[0].tostring(), 25);  string prompt = "a";  string consoleout = "";  // while connected  while (tc.isconnected && prompt != "exit")  {  // display server output  prompt = "ehlo a.com";  tc.writeline(prompt);  //i've tried adding 2 or 5 second thread sleep here , still same result.  console.write(tc.read());  prompt = "exit";  }  //telnet end 

telnetconnection class:

// minimalistic telnet implementation // conceived tom janssens on 2007/06/06  codeproject // // http://www.corebvba.be    using system; using system.collections.generic; using system.text; using system.net.sockets;  namespace minimalistictelnet {     enum verbs {         = 251,         wont = 252,         = 253,         dont = 254,         iac = 255     }      enum options     {         sga = 3     }      class telnetconnection     {         tcpclient tcpsocket;          int timeoutms = 100;          public telnetconnection(string hostname, int port)         {             try             {                 tcpsocket = new tcpclient(hostname, port);             }             catch (socketexception e)             {                 console.write(e);             }          }          public string login(string username,string password,int logintimeoutms)         {             int oldtimeoutms = timeoutms;             timeoutms = logintimeoutms;             string s = read();             if (!s.trimend().endswith(":"))                throw new exception("failed connect : no login prompt");             writeline(username);              s += read();             if (!s.trimend().endswith(":"))                 throw new exception("failed connect : no password prompt");             writeline(password);              s += read();             timeoutms = oldtimeoutms;             return s;         }          public void writeline(string cmd)         {             write(cmd + "\n");         }          public void write(string cmd)         {             if (!tcpsocket.connected) return;             byte[] buf = system.text.asciiencoding.ascii.getbytes(cmd.replace("\0xff","\0xff\0xff"));             tcpsocket.getstream().write(buf, 0, buf.length);         }          public string read()         {             if (!tcpsocket.connected) return null;             stringbuilder sb=new stringbuilder();                         {                 parsetelnet(sb);                 system.threading.thread.sleep(timeoutms);             } while (tcpsocket.available > 0);             return sb.tostring();         }          public bool isconnected         {             { return tcpsocket.connected; }         }          void parsetelnet(stringbuilder sb)         {             while (tcpsocket.available > 0)             {                 int input = tcpsocket.getstream().readbyte();                 switch (input)                 {                     case -1 :                         break;                     case (int)verbs.iac:                         // interpret command                         int inputverb = tcpsocket.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 = tcpsocket.getstream().readbyte();                                 if (inputoption == -1) break;                                 tcpsocket.getstream().writebyte((byte)verbs.iac);                                 if (inputoption == (int)options.sga )                                     tcpsocket.getstream().writebyte(inputverb == (int)verbs.do ? (byte)verbs.will:(byte)verbs.do);                                  else                                     tcpsocket.getstream().writebyte(inputverb == (int)verbs.do ? (byte)verbs.wont : (byte)verbs.dont);                                  tcpsocket.getstream().writebyte((byte)inputoption);                                 break;                             default:                                 break;                         }                         break;                     default:                         sb.append( (char)input );                         break;                 }             }         }     } } 

update:

using following loop able work once, once not iterate through count of j when verified j 2. output, expected output , function below.

function:

for (int j = 0; j < list.count; j++) { //telnet start console.writeline("on round #" + j); iphostentry hostinfo = dns.resolve(list[j]); telnetconnection tc = new telnetconnection(hostinfo.addresslist[0].tostring(), 25); string prompt = "a"; string consoleout = ""; // while connected while (tc.isconnected && prompt != "exit") { // display server output console.write(tc.read());  // send client input server prompt = "ehlo a.com"; tc.writeline(prompt);  // display server output consoleout = tc.read(); console.write(consoleout);  //send exit input server prompt = "exit"; tc.writeline(prompt); console.write(tc.read()); } console.writeline("**disconnected**"); //telnet end if (consoleout.indexof("starttls")>-1) { if (j == 0) { if (list[j].contains(domains[i])) datagridview1.rows.add(domains[i], list[j], "y", numberemployees, "y"); else datagridview1.rows.add(domains[i], list[j], "y", numberemployees, "n"); } else { if (list[j].contains(domains[i])) datagridview1.rows.add(null, list[j], "y", null, "y"); else datagridview1.rows.add(null, list[j], "y", null, "n"); } } else { if (j == 0) { if (list[j].contains(domains[i])) datagridview1.rows.add(domains[i], list[j], "n", numberemployees, "y"); else datagridview1.rows.add(domains[i], list[j], "n", numberemployees, "n"); } else { if (list[j].contains(domains[i])) datagridview1.rows.add(null, list[j], "n", null, "y"); else datagridview1.rows.add(null, list[j], "n", null, "n"); } }  } 

output:

on round #0 true220 server-6.tower-95.messagelabs.com esmtp 250-server-6.tower-95.messagelabs.com 250-starttls 250-pipelining 250 8bitmime 502 unimplemented (#5.5.1) **disconnected** on round #1 true220 server-14.tower-558.messagelabs.com esmtp **disconnected** 

expected output:

on round #0 true220 server-6.tower-95.messagelabs.com esmtp 250-server-6.tower-95.messagelabs.com 250-starttls 250-pipelining 250 8bitmime 502 unimplemented (#5.5.1) **disconnected** on round #1 true220 server-14.tower-558.messagelabs.com esmtp 250-starttls 250-pipelining 250 8bitmime **disconnected** 

so whatever reason, won't second loop. starting depend on server. servers respond little information , this, makes nothing happened @ all.

on round #0 true220 smtp proxy server ready **disconnected** on round #1 true220 smtp proxy server ready **disconnected** on round #2 true220 smtp server ready **disconnected** on round #3 true220 smtp server ready **disconnected** on round #4 true220 smtp proxy server ready **disconnected** on round #5 true220 smtp proxy server ready **disconnected** 

kyle,

it looks need open socket:

   public string read()     {         if (!tcpsocket.connected)         {           throw new exception("socket closed.");         }         stringbuilder sb=new stringbuilder();                 {             parsetelnet(sb);             system.threading.thread.sleep(timeoutms);         } while (tcpsocket.available > 0);         return sb.tostring();     } 

it isn't open, returning.

[update]

try looping through iphostentry hostinfo = dns.resolve(list[j]); before //telnet start comment:

private const int port25 = 25; // hate magic numbers  foreach (var item in list) {   var hostinfo = dns.resolve(item);   console.writeline(hostinfo);   foreach (var address in hostinfo.addresslist) {     var tc = new telnetconnection(address, port25);     console.writeline("{0} telnetconnection connected: {1}", address, tc.isconnected);   } } 

[update 2]

this hard debug, not knowing trying connect , else going on.

that said, let's try following:

in class minimalistictelnet, add method:

public void close() {     if (tcpsocket != null) {         tcpsocket.close();     } } 

i don't see anywhere else, , causing issues next time attempt second connection.

now, in test code, add new 1 line of code after while loop:

while (tc.isconnected && prompt != "exit") {     // display server output     console.write(tc.read());      // send client input server     prompt = "ehlo a.com";     tc.writeline(prompt);      // display server output     consoleout = tc.read();     console.write(consoleout);      //send exit input server     prompt = "exit";     tc.writeline(prompt);     console.write(tc.read()); } tc.close(); console.writeline("**disconnected**"); 

with luck, reason 2nd connection failing because still had open connection.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -