连接到基于 ruby​​ 的服务器 [英] Connection to ruby based server

查看:30
本文介绍了连接到基于 ruby​​ 的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    }
}
}

请帮助我尝试过 tcp client instaid 但同样的事情应用程序应始终保持连接 RUBYCODE

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

并重复 ruby​​,它只是使用的套接字示例

and repeat the ruby its just an example of socket am using

推荐答案

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

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

这篇关于连接到基于 ruby​​ 的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆