0

I'm writing a bot for moderating my twitch.tv channel in C#.

Here's the basic code for the loop, which is done by a background worker to avoid UI freezes. There's a TCPClient (Client), StreamReader (Reader), StreamWriter (Writer), and NetworkStream (Stream).

    private void listener_dowork(object sender, DoWorkEventArgs e)
    {
        string Data = "";
        while ((Data = Reader.ReadLine()) != null)
        {
            //Perform operations on the received data
        }
        Console.WriteLine("Loop ended");//this shouldn't happen
    }
    private void listener_workercompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //basically, display a console message that says "OOPS!" and try to reconnect.
    }

I get the message "Loop ended" and "OOPS!" and at that point, I get the exception (which I cannot for the life of me catch).

The thing is, I can physically unplug the network cable from my computer wait 30 seconds and plug it back in, and it'll continue just fine.

The full exception is:

System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)

Note the lack of a line number, which is present in every other kind of exception I've had, which means I have no idea which part of the program is causing the exception, even though I've put every possible line inside a try/catch.

I guess what I'm looking for is some insight into why this is occurring.

It happens invariably every time I start the bot and leave it running for a few minutes on any channel, though the number of minutes varies.

4

1 に答える 1

1

コメントですでに述べたように、Twitch.tv はチャットの基盤となるシステムとして IRC を使用しています。サーバーとの接続を維持するには、サーバーから頻繁に送信される「PING」リクエストに応答する必要があります (通常は 30 秒ごと、サーバーの実装によって異なる場合があります)。IRC クライアント プロトコルの詳細については、RFC 2812を参照してください。

既に StreamWriter と Reader があるとおっしゃいましたが、行に「PING」が含まれているかどうかを確認し、「PONG」で応答するだけです。

if (Data.Contains("PING"))
{
  _streamWriter.WriteLine(Data.Replace("PING","PONG");
  _streamWriter.Flush();
}
于 2015-01-04T20:53:56.300 に答える