Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TCP客户端在一段时间后从Twitch IRC断开连接_C#_Sockets_Irc_Twitch - Fatal编程技术网

C# TCP客户端在一段时间后从Twitch IRC断开连接

C# TCP客户端在一段时间后从Twitch IRC断开连接,c#,sockets,irc,twitch,C#,Sockets,Irc,Twitch,我的问题是,在我连接到IRC并从IRC接收原始文本并记录它之后,它只是决定在不活动后或在我启动未确认的2-3秒后断开与服务器的连接 任何帮助都将不胜感激。以下是我的代码在此处发布时遇到的问题: 我需要它来停止断开连接,但我真的找不到一个方法。我知道流行的mIRC客户端在x时间段后会与Twitch断开连接,但会重新连接,只要它知道及时重新连接2-5秒就可以了 这部分是对乒乓球的回应: if (buf.StartsWith("PING ")) output.Write(buf.Replace("P

我的问题是,在我连接到IRC并从IRC接收原始文本并记录它之后,它只是决定在不活动后或在我启动未确认的2-3秒后断开与服务器的连接

任何帮助都将不胜感激。以下是我的代码在此处发布时遇到的问题:

我需要它来停止断开连接,但我真的找不到一个方法。我知道流行的mIRC客户端在x时间段后会与Twitch断开连接,但会重新连接,只要它知道及时重新连接2-5秒就可以了

这部分是对乒乓球的回应:

if (buf.StartsWith("PING ")) output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush();

非常感谢您的帮助。

下面的代码通过运行单独的线程ping到IRC服务器,帮助我的Twitch bot避免断开连接

PingSender类:

用法:


我希望您认识到,无论if语句如何,output.Flush都将运行。if影响的唯一语句是output.Write,output.Flush不是if语句块的一部分。所以我需要使用括号来包含它?我会尽快尝试,如果它能解决问题,那就谢谢了。99%确定它不会解决问题,但希望你明白,仅仅因为它们在同一条线上,并不意味着它们是if的一部分。添加一个括号并不能解决问题。你曾试过使用Wireshark之类的网络嗅探工具来查看它为什么会断开连接吗?2-5秒对于IRC的超时来说似乎很短。你有没有说明它有那么短的地方?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TwitchBot
{
    /*
    * Class that sends PING to irc server every 5 minutes
    */
    class PingSender
    {
        static string PING = "PING ";
        private Thread pingSender;

        // Empty constructor makes instance of Thread
        public PingSender() 
        {
            pingSender = new Thread (new ThreadStart (this.Run) ); 
        } 
        // Starts the thread
        public void Start() 
        { 
            pingSender.Start(); 
        } 
        // Send PING to irc server every 5 minutes
        public void Run()
        {
            while (true)
            {
                Program.irc.sendIrcMessage(PING + "irc.twitch.tv");
                Thread.Sleep(300000); // 5 minutes
            }
        }
    }
}
/* Ping to twitch server to prevent auto-disconnect */
PingSender ping = new PingSender();
ping.Start();