00001 using System; 00002 using System.Drawing; 00003 using System.Collections; 00004 using System.ComponentModel; 00005 using System.Windows.Forms; 00006 using System.Data; 00007 using System.Net; 00008 using System.Net.Sockets; 00009 using System.Text; 00010 using System.IO; 00011 using System.Threading ; 00012 00013 namespace NDB_CPC.telnetclient 00014 { 00018 public class telnetClient 00019 { 00020 Char IAC = Convert.ToChar(255); 00021 Char DO = Convert.ToChar(253); 00022 Char DONT = Convert.ToChar(254); 00023 Char WILL = Convert.ToChar(251); 00024 Char WONT = Convert.ToChar(252); 00025 Char SB = Convert.ToChar(250); 00026 Char SE = Convert.ToChar(240); 00027 const Char IS = '0'; 00028 const Char SEND = '1'; 00029 const Char INFO = '2'; 00030 const Char VAR = '0'; 00031 const Char VALUE = '1'; 00032 const Char ESC = '2'; 00033 const Char USERVAR = '3'; 00034 string m_strResp; 00035 00036 private ArrayList m_ListOptions = new ArrayList(); 00037 private IPEndPoint iep ; 00038 private AsyncCallback callbackProc ; 00039 private string address ; 00040 private int port ; 00041 private Socket s ; 00042 private TextBox textBox1; 00043 Byte[] m_byBuff = new Byte[32767]; 00044 00045 00046 public telnetClient(string ip, int p, TextBox tb) 00047 { 00048 00049 address = ip; 00050 port = p; 00051 textBox1=tb; 00052 IPHostEntry IPHost = Dns.Resolve(address); 00053 string []aliases = IPHost.Aliases; 00054 IPAddress[] addr = IPHost.AddressList; 00055 00056 try 00057 { 00058 // Create New Socket 00059 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 00060 // Create New EndPoint 00061 iep = new IPEndPoint(addr[0],port); 00062 // This is a non blocking IO 00063 s.Blocking = false ; 00064 // Assign Callback function to read from Asyncronous Socket 00065 callbackProc = new AsyncCallback(ConnectCallback); 00066 // Begin Asyncronous Connection 00067 s.BeginConnect(iep , callbackProc, s ) ; 00068 00069 } 00070 catch(Exception eeeee ) 00071 { 00072 MessageBox.Show(eeeee.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop ); 00073 Application.Exit(); 00074 } 00075 } 00076 00077 public void ConnectCallback( IAsyncResult ar ) 00078 { 00079 try 00080 { 00081 // Get The connection socket from the callback 00082 Socket sock1 = (Socket)ar.AsyncState; 00083 if ( sock1.Connected ) 00084 { 00085 // Define a new Callback to read the data 00086 AsyncCallback recieveData = new AsyncCallback( OnRecievedData ); 00087 // Begin reading data asyncronously 00088 sock1.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData , sock1 ); 00089 } 00090 } 00091 catch( Exception ex ) 00092 { 00093 MessageBox.Show(ex.Message, "Setup Recieve callbackProc failed!" ); 00094 } 00095 } 00096 00097 00098 public void OnRecievedData( IAsyncResult ar ) 00099 { 00100 // Get The connection socket from the callback 00101 Socket sock = (Socket)ar.AsyncState; 00102 // Get The data , if any 00103 int nBytesRec = sock.EndReceive( ar ); 00104 if( nBytesRec > 0 ) 00105 { 00106 string sRecieved = Encoding.ASCII.GetString( m_byBuff, 0, nBytesRec ); 00107 string m_strLine=""; 00108 for ( int i=0; i < nBytesRec;i++) 00109 { 00110 Char ch = Convert.ToChar(m_byBuff[i]); 00111 switch( ch ) 00112 { 00113 case '\r': 00114 m_strLine += Convert.ToString("\r\n"); 00115 break; 00116 case '\n': 00117 break; 00118 default: 00119 m_strLine += Convert.ToString(ch); 00120 break; 00121 } 00122 } 00123 try 00124 { 00125 int strLinelen = m_strLine.Length ; 00126 if ( strLinelen == 0 ) 00127 { 00128 m_strLine = Convert.ToString("\r\n"); 00129 } 00130 00131 Byte[] mToProcess = new Byte[strLinelen]; 00132 for ( int i=0; i < strLinelen ; i++) 00133 mToProcess[i] = Convert.ToByte(m_strLine[i]); 00134 // Process the incoming data 00135 string mOutText = ProcessOptions(mToProcess); 00136 if ( mOutText != "" ) 00137 textBox1.AppendText(mOutText); 00138 00139 // Respond to any incoming commands 00140 RespondToOptions(); 00141 } 00142 catch( Exception ex ) 00143 { 00144 Object x = this ; 00145 MessageBox.Show(ex.Message , "Information!" ); 00146 } 00147 } 00148 else 00149 { 00150 // If no data was recieved then the connection is probably dead 00151 Console.WriteLine( "Disconnected", sock.RemoteEndPoint ); 00152 sock.Shutdown( SocketShutdown.Both ); 00153 sock.Close(); 00154 Application.Exit(); 00155 } 00156 } 00157 00158 private string ProcessOptions(byte[] m_strLineToProcess) 00159 { 00160 string m_DISPLAYTEXT =""; 00161 string m_strTemp ="" ; 00162 string m_strOption =""; 00163 string m_strNormalText =""; 00164 bool bScanDone =false; 00165 int ndx =0; 00166 int ldx =0; 00167 char ch ; 00168 try 00169 { 00170 for ( int i=0; i < m_strLineToProcess.Length ; i++) 00171 { 00172 Char ss = Convert.ToChar(m_strLineToProcess[i]); 00173 m_strTemp = m_strTemp + Convert.ToString(ss); 00174 } 00175 00176 while(bScanDone != true ) 00177 { 00178 int lensmk = m_strTemp.Length; 00179 ndx = m_strTemp.IndexOf(Convert.ToString(IAC)); 00180 if ( ndx > lensmk ) 00181 ndx = m_strTemp.Length; 00182 00183 if(ndx != -1) 00184 { 00185 m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx); 00186 ch = m_strTemp[ndx + 1]; 00187 if ( ch == DO || ch == DONT || ch == WILL || ch == WONT ) 00188 { 00189 m_strOption = m_strTemp.Substring(ndx, 3); 00190 string txt = m_strTemp.Substring(ndx + 3); 00191 m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx); 00192 m_ListOptions.Add(m_strOption); 00193 m_strTemp = txt ; 00194 } 00195 else 00196 if ( ch == IAC) 00197 { 00198 m_DISPLAYTEXT= m_strTemp.Substring(0,ndx); 00199 m_strTemp = m_strTemp.Substring(ndx + 1); 00200 } 00201 else 00202 if ( ch == SB ) 00203 { 00204 m_DISPLAYTEXT= m_strTemp.Substring(0,ndx); 00205 ldx = m_strTemp.IndexOf(Convert.ToString(SE)); 00206 m_strOption = m_strTemp.Substring(ndx, ldx); 00207 m_ListOptions.Add(m_strOption); 00208 m_strTemp = m_strTemp.Substring(ldx); 00209 } 00210 } 00211 else 00212 { 00213 m_DISPLAYTEXT = m_DISPLAYTEXT + m_strTemp; 00214 bScanDone = true ; 00215 } 00216 } 00217 m_strNormalText = m_DISPLAYTEXT; 00218 } 00219 catch(Exception eP) 00220 { 00221 MessageBox.Show(eP.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop ); 00222 Application.Exit(); 00223 } 00224 return m_strNormalText ; 00225 } 00226 00227 void DispatchMessage(string strText) 00228 { 00229 try 00230 { 00231 Byte[] smk = new Byte[strText.Length]; 00232 for ( int i=0; i < strText.Length ; i++) 00233 { 00234 Byte ss = Convert.ToByte(strText[i]); 00235 smk[i] = ss ; 00236 } 00237 00238 IAsyncResult ar2 = s.BeginSend(smk , 0 , smk.Length , SocketFlags.None , callbackProc , s ); 00239 s.EndSend(ar2); 00240 } 00241 catch(Exception ers) 00242 { 00243 MessageBox.Show("ERROR IN RESPOND OPTIONS"); 00244 } 00245 } 00246 00247 void RespondToOptions() 00248 { 00249 try 00250 { 00251 string strOption; 00252 for ( int i=0; i < m_ListOptions.Count; i++) 00253 { 00254 strOption = (string)m_ListOptions[i]; 00255 ArrangeReply(strOption); 00256 } 00257 DispatchMessage(m_strResp); 00258 m_strResp =""; 00259 m_ListOptions.Clear(); 00260 } 00261 catch(Exception ers) 00262 { 00263 MessageBox.Show("ERROR IN RESPOND OPTIONS"); 00264 } 00265 } 00266 void ArrangeReply(string strOption) 00267 { 00268 try 00269 { 00270 00271 Char Verb; 00272 Char Option; 00273 Char Modifier; 00274 Char ch; 00275 bool bDefined = false; 00276 00277 if(strOption.Length < 3) return; 00278 00279 Verb = strOption[1]; 00280 Option = strOption[2]; 00281 00282 if ( Option == 1 || Option == 3 ) 00283 { 00284 // case 1: // Echo 00285 // case 3: // Suppress Go-Ahead 00286 bDefined = true; 00287 // break; 00288 } 00289 00290 m_strResp += IAC; 00291 00292 if(bDefined == true ) 00293 { 00294 if ( Verb == DO ) 00295 { 00296 // case DO: 00297 ch = WILL; 00298 m_strResp += ch; 00299 m_strResp += Option; 00300 // break; 00301 } 00302 if ( Verb == DONT ) 00303 { 00304 ch = WONT; 00305 m_strResp += ch; 00306 m_strResp += Option; 00307 // break; 00308 } 00309 if ( Verb == WILL ) 00310 { 00311 ch = DO; 00312 m_strResp += ch; 00313 m_strResp += Option; 00314 //break; 00315 } 00316 if ( Verb == WONT) 00317 { 00318 ch = DONT; 00319 m_strResp += ch; 00320 m_strResp += Option; 00321 // break; 00322 } 00323 if ( Verb == SB) 00324 { 00325 Modifier = strOption[3]; 00326 if(Modifier == SEND) 00327 { 00328 ch = SB; 00329 m_strResp += ch; 00330 m_strResp += Option; 00331 m_strResp += IS; 00332 m_strResp += IAC; 00333 m_strResp += SE; 00334 } 00335 // break; 00336 } 00337 } 00338 else 00339 { 00340 // switch(Verb) 00341 // { 00342 if ( Verb == DO ) 00343 { 00344 ch = WONT; 00345 m_strResp += ch; 00346 m_strResp += Option; 00347 // break; 00348 } 00349 if ( Verb == DONT) 00350 { 00351 ch = WONT; 00352 m_strResp += ch; 00353 m_strResp += Option; 00354 // break; 00355 } 00356 if ( Verb == WILL) 00357 { 00358 ch = DONT; 00359 m_strResp += ch; 00360 m_strResp += Option; 00361 // break; 00362 } 00363 if ( Verb == WONT) 00364 { 00365 ch = DONT; 00366 m_strResp += ch; 00367 m_strResp += Option; 00368 // break; 00369 } 00370 } 00371 } 00372 catch(Exception eeeee ) 00373 { 00374 MessageBox.Show(eeeee.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop ); 00375 Application.Exit(); 00376 } 00377 00378 } 00379 00380 private void textBox1_KeyPress_1(object sender, System.Windows.Forms.KeyPressEventArgs e) 00381 { 00382 if ( e.KeyChar == 13 ) 00383 { 00384 DispatchMessage("\r\n"); 00385 } 00386 else 00387 if ( e.KeyChar == 8 ) 00388 { 00389 try 00390 { 00391 // string mtmp = textBox1.Text.Substring(0,textBox1.Text.Length-1); 00392 // textBox1.Text = "" ; 00393 } 00394 catch(Exception ebs) 00395 { 00396 MessageBox.Show("ERROR IN BACKSPACE"); 00397 } 00398 } 00399 else 00400 { 00401 string str = e.KeyChar.ToString(); 00402 DispatchMessage(str); 00403 } 00404 } 00405 00406 00407 } 00408 }
1.4.7

