C#IRC Bot错误:'无法从封闭的文本阅读器中读取'

时间:2015-07-29 11:40:53

标签: c# text irc reader

之前我曾使用过这段代码,但它确实有效。现在,每次尝试连接到IRC服务器时,都会收到错误:'无法从已关闭的文本阅读器中读取'

我已经更改了隐私地址

这是我的代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace IRCBot
{
class Program
{

}
class IrcBot
{
    // Irc server to connect 
    public static string SERVER = "irc.rizon.net";
    // Irc server's port (6667 is default port)
    private static int PORT = 6660;
    // User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server 
    private static string USER = "irc_bot";
    // Bot's nickname
    private static string NICK = "BotNick";
    // Channel to join
    private static string CHANNEL = "#testing";
    // StreamWriter is declared here so that PingSender can access it
    public static StreamWriter writer;
    static void Main(string[] args)
    {
        NetworkStream stream;
        TcpClient irc;
        string inputLine;
        StreamReader reader;
        string nickname;
        try
        {
            irc = new TcpClient(SERVER, PORT);
            stream = irc.GetStream();
            reader = new StreamReader(stream);
            writer = new StreamWriter(stream);
            // Start PingSender thread
            PingSender ping = new PingSender();
            ping.Start();
            writer.WriteLine(USER);
            writer.Flush();
            writer.WriteLine("NICK " + NICK);
            writer.Flush();
            writer.WriteLine("JOIN " + CHANNEL);
            writer.Flush();
            while (true)
            {
                while ((inputLine = reader.ReadLine()) != null)
                {
                    if (inputLine.EndsWith("JOIN :" + CHANNEL))
                    {
                        // Parse nickname of person who joined the channel
                        nickname = inputLine.Substring(1, inputLine.IndexOf("!") - 1);
                        // Welcome the nickname to channel by sending a notice
                        writer.WriteLine("NOTICE " + nickname + " :Hi " + nickname +
                        " and welcome to " + CHANNEL + " channel!");
                        writer.Flush();
                        // Sleep to prevent excess flood
                        Thread.Sleep(2000);
                    }
                }
                // Close all streams
                writer.Close();
                reader.Close();
                irc.Close();
            }
        }
        catch (Exception e)
        {
            // Show the exception, sleep for a while and try to establish a new connection to irc server
            Console.WriteLine(e.ToString());
            Thread.Sleep(5000);
            string[] argv = { };
            Main(argv);
        }
    }
}

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 15 seconds
    public void Run()
    {
        while (true)
        {
            IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
            IrcBot.writer.Flush();
            Thread.Sleep(15000);
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

关闭所有流必须在外面:

 while (true)
            {
                while ((inputLine = reader.ReadLine()) != null)
                {
                   ....
                }            
            }
  // Close all streams
                writer.Close();
                reader.Close();
                irc.Close();

答案 1 :(得分:0)

将所有流关闭行移至“finally”块:

try
{
...
}
catch (Exception e)
{
  Console.WriteLine(e.ToString());
  Thread.Sleep(5000);
  string[] argv = { };
  Main(argv);
}
finally 
{
  //Close all streams
  writer?.Close();
  reader?.Close();
  irc?.Close();
}

注意,我使用过新的C#6零传播算子?这里导致您的流可以为null。如果您使用的是C#ver< 6,请按以下方式编写: if (writer!=null) writer.Close();