连接到基于ruby的服务器

我正在使用C#Net framework 2.0开发一个应用程序。 它基本上是一个带有tcp客户端类的应用程序。 应用程序连接到我的服务器,这是一个基本的ruby tcp服务器,它会停留4个小时,然后断开连接

class TelnetConnection { TcpClient tcpSocket; int TimeOutMs = 400; static string host = null; static int prt = 0; static int timetorec = 10; //sec public TelnetConnection(string Hostname, int Port) { host = Hostname; prt = Port; try { tcpSocket = new TcpClient(Hostname, Port); } catch (Exception x) { Console.WriteLine(x.Message) } } public void reconnect(int sec) { try { System.Threading.Thread.Sleep(sec * 1000); //10sec then retry tcpSocket = new TcpClient(host, prt); } catch (Exception x) { Console.WriteLine(x.Message) } } 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 to connect : no login prompt"); WriteLine(Username); s += Read(); if (!s.TrimEnd().EndsWith(":")) throw new Exception("Failed to connect : no password prompt"); WriteLine(Password); s += Read(); TimeOutMs = oldTimeOutMs; return s; } public string Read() { try { if (!tcpSocket.Connected) return null; StringBuilder sb = new StringBuilder(); do { Parse(sb); System.Threading.Thread.Sleep(TimeOutMs); } while (tcpSocket.Available > 0); return sb.ToString(); } catch (Exception) { return null; } } protected override void OnStart(string[] args) { System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseTime = TimeSpan.MaxValue; while (tc.IsConnected) { Thread.Sleep(120); what(tc.Read()); } } static void what(string w) { if (w == "Example") { //DO } } } 

请帮助我尝试了tcp客户端instaid但同样的应用程序应始终保持连接RUBYCODE

 require 'socket' # Sockets are in standard library hostname = 'localhost' port = 2000 s = TCPSocket.open(host, port) while line = s.gets # Read lines from the socket puts line.chop # And print with platform line terminator end 

并重复ruby它只是socket使用的一个例子

您需要定期发送一些数据,以防止防火墙和NAT设备超时输入连接。