连接到基于 ruby​​ 的服务器

编程入门 行业动态 更新时间:2024-10-28 21:28:56
本文介绍了连接到基于 ruby​​ 的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 08:03:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1406750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   服务器   ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!