I am developping an application in C# Net framework 2.0. It is basicly a application with a tcp client class. the application connects to my server which is a basic ruby tcp server it stays for 4 hours then get disconnected
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
}
}
}
Please help i have tried tcp client instaid but the same thing application should stay connected at all time 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
and repeat the ruby its just an example of socket am using