/// <summary> /// Creates a new Telnet instance /// </summary> /// <param name="IPAddr">IP Address of the Device</param> /// <param name="port">Telnet port for the listening device</param> /// <returns> Bool value representing the success of connection</returns> public void Connect(IPAddress IPAddr, int port) { try { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IPAddr, port); telnet = new TelnetStream(new NetworkStream(socket)); psychic = new Expector(telnet); psychic.ExpectTimeout = new TimeSpan(0, 0, 8); //5 sec timeout _status = "Online"; } catch (SocketException) { _status = "Connection Error"; } catch { _status = "Error"; } }
/// <summary> /// Creates a new Telnet instance /// </summary> /// <param name="IPAddr">IP Address of the Device</param> /// <param name="port">Telnet port for the listening device</param> /// <returns> Bool value representing the success of connection</returns> public void Connect(IPAddress IPAddr, int port) { try { TcpClient tcl = new TcpClient(Dns.GetHostEntry(IPAddr).HostName, port); System.IO.Stream st = tcl.GetStream(); telnet = new TelnetStream(st); psychic = new Expector(telnet); psychic.ExpectTimeout = new TimeSpan(0, 0, 5); //5 sec timeout _status = "Online"; } catch (SocketException) { _status = "Connection Error"; } catch { _status = "Error"; } }
public LineBuffer(TelnetStream stream) { _stream = stream; }
/// <summary> /// This is the main CoSy client communication thread. /// </summary> /// <param name="client">The handle of the TCP client connection</param> private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; TelnetStream stream = new TelnetStream(tcpClient.GetStream()); LineBuffer buffer = new LineBuffer(stream); try { DateTime loginTime = DateTime.Now; buffer.WriteLine("CIX Conferencing System"); while (true) { buffer.WriteString("login: "******"qix" || loginString == "cix") { break; } buffer.WriteLine("Unknown login"); } Version ver = Assembly.GetEntryAssembly().GetName().Version; string versionString = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build); buffer.WriteLine(string.Format("CIX API Proxy {0}", versionString)); buffer.WriteString("Nickname? (Enter 'new' for new user) "); string userName = buffer.ReadLine(); buffer.WriteString("Password: "******""); Console.WriteLine("Logged in as '{0}' with password '{1}'", userName, password); APIRequest.Username = userName; APIRequest.Password = password; LoadScratchpad(); _forums = new Forums(); buffer.WriteLine(string.Format("You are a member of {0} conference(s).", _forums.Count)); buffer.WriteLine("Max scratchsize during file has been set to 20000000 bytes"); TopLevelCommand(buffer); buffer.Output = null; // Round up login time to 1 minute. TimeSpan onlineTime = (DateTime.Now - loginTime).Add(new TimeSpan(0, 1, 0)); buffer.WriteLine(string.Format("{0}, you have been online {1}:{2} on line 5", userName, onlineTime.Hours, onlineTime.Minutes)); buffer.WriteLine("Goodbye from CIX !!!HANGUP NOW!!!"); } catch (IOException) { Console.WriteLine("Client terminated connection"); } catch (IndexOutOfRangeException) { Console.WriteLine("Client terminated connection"); } catch (AuthenticationException) { Console.WriteLine("Client not authorised"); } tcpClient.Close(); }
private void ReadStreamInBackgroud(object sender, DoWorkEventArgs e) { byte[] bucket = new byte[1024]; int readLen = 0; // check if client is connected, if not then connect the client while (tcpclient == null || !tcpclient.Connected) { this.Dispatcher.Invoke((Action)(() => { ShowReconnectingPanel(); })); tcpclient = new TcpClient(); try { Trace.WriteLine("Connecting.." + Session.SessionName); tcpclient.Connect(Session.ServerIP, Session.ServerPort); Session.IsConnected = tcpclient.Connected; } catch (Exception ex) { Trace.WriteLine(ex.Message); } if (!autoreconnect) break; else Thread.Sleep(autoreconnecttime); } if (tcpclient == null || !tcpclient.Connected) { this.Dispatcher.Invoke((Action)(() => { ShowNoConnectionBanner(); })); return; } this.Dispatcher.Invoke((Action)(() => { ShowTerminalRichTextBox(); })); TelnetStream t_stream = new TelnetStream(tcpclient.GetStream()); readLen= t_stream.Read(bucket, 0, 1024); this.Dispatcher.Invoke((Action)(() => { RichTextBox.Write(Encoding.ASCII.GetString(bucket, 0, readLen)); })); if (LogFileEnable) { if (logfile == null) logfile = new LogFile(Session); logfile.Write(bucket, readLen); } if (tcpclient == null || !tcpclient.Connected) { this.Dispatcher.Invoke((Action)(() => { ShowNoConnectionBanner(); })); return; } // read stram asynchrounously Stream = tcpclient.GetStream(); foreach (string cmd in Session.OnConnectCmdList) { Write(Encoding.ASCII.GetBytes(cmd)); } int num = 0; try { while (true) { if (Stream.DataAvailable) { readLen = Stream.Read(bucket, 0, 1024); if (LogFileEnable) logfile.Write(bucket, readLen); if ((bucket[0] == 8 && bucket[1] == 32 && bucket[2] == 8) || (bucket[0] == 27 && bucket[1] == 91 && bucket[2] == 48 && bucket[3] == 68 && bucket[4] == 27)) this.Dispatcher.Invoke((Action)(() => { Trace.WriteLine("back received: "+ ++num); RichTextBox.BackSpace(); })); else this.Dispatcher.Invoke((Action)(() => { RichTextBox.Write(Encoding.ASCII.GetString(bucket, 0, readLen)); })); } if (_bw_Stop_Flag) // if flag is true then come out of the look, its signal for _bw to stop break; Thread.Sleep(100); if (tcpclient == null || !tcpclient.Connected) { this.Dispatcher.Invoke((Action)(() => { ShowNoConnectionBanner(); })); return; } } }catch(Exception ex) { this.Dispatcher.Invoke((Action)(() => { ShowNoConnectionBanner(); })); Trace.Write(ex); } }