0

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

Avada Kedavra
  • 8,523
  • 5
  • 32
  • 48
user1692494
  • 65
  • 1
  • 7

1 Answers1

0

You'll need to send some data periodically to keep firewalls and NAT devices from timing out their entries for the connection.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • does it matter if i send Message from server to client or reversed as long as i keep messaging between them – user1692494 Sep 26 '12 at 07:12
  • No, it won't matter. Persisting the connection state just requires a packet to be send in each direction, and data sent will be acknowledged by the TCP stack, so sending data in one direction results in a packet in each direction. – David Schwartz Sep 26 '12 at 07:30