我正在 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 客户端,但同样的应用程序应该始终保持连接 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
并重复红宝石,它只是正在使用的套接字的一个例子