【问题标题】:I'm trying to write a simple IRC bot but I'm getting null exception why is that?我正在尝试编写一个简单的 IRC 机器人,但我得到空异常,这是为什么?
【发布时间】:2013-08-23 16:21:59
【问题描述】:

这是我的Form1 代码:

namespace Irc_Bot
{
    public partial class Form1 : Form
    {
        int port;
        string buf, nick, owner, server, chan;
        System.Net.Sockets.TcpClient sock = new System.Net.Sockets.TcpClient();
        System.IO.TextReader input;
        System.IO.TextWriter output;

        public Form1()
        {
            InitializeComponent();

            //Get nick, owner, server, port, and channel from user
            label1.Text = "Enter bot nick: ";
            nick = textBox1.Text;
            label2.Text = "Enter bot owner name: ";
            owner = textBox2.Text;
            label3.Text = "Enter server name: ";
            label4.Text = "Enter port number: ";
            if (textBox4.Text != "")
            port = Convert.ToInt32(textBox4.Text);
            label5.Text = "Channel: ";
            chan = textBox5.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void connectToIrc()
        {
            //Connect to irc server and get input and output text streams from TcpClient.
            server = textBox3.Text;
            port = Convert.ToInt32(textBox4.Text);
            sock.Connect("chat.eu.freenode.net", port);//server, port);

            if (!sock.Connected)
            {
                Console.WriteLine("Failed to connect!");
                return;
            }

            input = new System.IO.StreamReader(sock.GetStream());
            output = new System.IO.StreamWriter(sock.GetStream());

             //Starting USER and NICK login commands 
         output.Write(
            "USER " + nick + " 0 * :" + owner + "\r\n" +
            "NICK " + nick + "\r\n"
         );

         output.Flush();

         //Process each line received from irc server
         for (buf = input.ReadLine(); ; buf = input.ReadLine())
         {
             //Display received irc message
             Console.WriteLine(buf);

             //Send pong reply to any ping messages
             if (buf.StartsWith("PING ")) { output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush(); }
             if (buf[0] != ':') continue;

             /* IRC commands come in one of these formats:
              * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
              * :SERVER COMAND ARGS ... :DATA\r\n
              */

             //After server sends 001 command, we can set mode to bot and join a channel
             if (buf.Split(' ')[1] == "001")
             {
                 output.Write(
                    "MODE " + nick + " +B\r\n" +
                    "JOIN " + chan + "\r\n"
                 );
                 output.Flush();
             }
         }
       }

        private void button1_Click(object sender, EventArgs e)
        {
            connectToIrc();
        }
    }
}

例如,我输入 chat.eu.freenode.net 作为服务器名称,输入 6667 作为端口。

如果我通过 MIRC 程序进入此服务器,它就可以工作。

但在我的程序中,在FOR 循环中经过 3-4 次迭代后,我在这一行得到了异常 null:

if (buf.StartsWith("PING "))

变量bufnull

首先它会进行几次迭代,然后在执行命令 3-5 次后:continue;,然后等待近 15 秒,然后跳转到该行:

if (buf.StartsWith("PING ")) 

并抛出异常空消息。

对象引用未设置为对象的实例

System.NullReferenceException 未处理
HResult=-2147467261
Message=对象引用未设置为对象的实例。
来源=Irc Bot

如何解决?

【问题讨论】:

  • 除了第一次迭代外,每次迭代都在读取流twice。这是为什么呢?
  • 你说得对,它不是我的代码,所以我没有检查它。那是问题吗?对不起。

标签: c# winforms


【解决方案1】:

这部分代码可能是导致错误的原因。

for (buf = input.ReadLine(); ; buf = input.ReadLine())

如果只有一行被发送,那么第一个 ReadLine 会占用该行,第二个会为空,如果只发送一行,则导致 buf 为空。

删除buf = input.ReadLine() 之一,并在开始处理之前添加buf != null 的检查。这可能看起来像这样。请注意,在 while 循环之前和结尾都有一个 buf = input.ReadLine();。我还在中间添加了一个错误检查,因为我在使用一些无效参数进行测试时遇到了if (buf[0] != ':') continue; 的无限循环。

//Process each line received from irc server
buf = input.ReadLine();
while (buf != null)
{

  //Display received irc message
  Console.WriteLine(buf);

  if (buf.StartsWith("ERROR")) break;

  //Send pong reply to any ping messages
  if (buf.StartsWith("PING ")) { output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush(); }
  if (buf[0] != ':') continue;

  //After server sends 001 command, we can set mode to bot and join a channel
  if (buf.Split(' ')[1] == "001")
  {
    output.Write(
      "MODE " + nick + " +B\r\n" +
      "JOIN " + chan + "\r\n"
    );
    output.Flush();
  }
  buf = input.ReadLine();
}

【讨论】:

  • Ohlin 我编辑了我的问题,请看一下。即使在删除了其中一个 ReadLine() 之后,我仍然在 buf 变量上获得 null 以及在哪里检查它是否为 null ?
  • 我已经重写了循环并自己进行了测试。试试我的新例子。
猜你喜欢
  • 2011-02-27
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多