2014-02-06 153 views
0

好吧,机器人连接就好,但是当我创建机器人时,表单崩溃。机器人保持连接,但也!!关于命令不起作用。如果我使用与控制台应用程序相同的确切代码,一切正常。问题仅仅是UI形式IRC Bot GUI崩溃?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using System.Net.Sockets; 

namespace cIRCBot 
{ 
    public partial class bSetup : Form 
    { 
     public bSetup() 
     { 
      InitializeComponent(); 
     } 

     private void createbotbtn_Click(object sender, EventArgs e) 
     { 
      string buf, nick, owner, server, chan; 
      int port; 
      TcpClient sock = new TcpClient(); 
      TextReader input; 
      TextWriter output; 

      //Get nick, owner, server, port, and channel from user 
      nick = botnick.Text; 
      owner = botname.Text; 
      server = servername.Text; 
      bool isNumber = int.TryParse(portnum.Text, out port); 
      chan = channelname.Text; 

      if (isNumber == false) 
      { 
       MessageBox.Show("Failed to connect. Make sure the server address and port number are correct.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 

      //Connect to irc server and get input and output text streams from TcpClient. 
      sock.Connect(server, port); 
      if (!sock.Connected) 
      { 
       //Console.WriteLine("Failed to connect!"); 
       return; 
      } 
      else 
      { 
       this.Close(); 
       input = new StreamReader(sock.GetStream()); 
       output = new 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: 
        * :[email protected] 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(); 
         if (buf.Contains("!about")) 
         { 

          output.WriteLine("PRIVMSG {0} :" + "I'm a shitty little bot coded by " + botname, channelname); 
          output.Flush(); 
         } 
        } 
       } 
      } 
     } 

    } 
} 

这是最主要的窗口,我有你使用系统中的其它形式

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace cIRCBot 
{ 
    public partial class mWin : Form 
    { 
     public mWin() 
     { 
      InitializeComponent(); 
     } 

     private void newBotToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      bSetup bSetup = new bSetup(); 
      bSetup.Show(); 

     } 

     private void quitToolStripMenuItem1_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 
    } 
} 

这是设置窗口

+0

你应该给一些更多的代码来分析。需要表单的代码。 –

+0

好的我添加了其他表单,不确定你是否也想看到设计器代码,@GrzegorzSławecki – OGF

+0

什么是崩溃?堆栈跟踪? –

回答

0

我怀疑什么导致问题的原因是您正在执行this.Close()并将其丢弃。在致电Close()后,您会进入for()并尝试操作可能处置的资源。

+0

我试图删除它,并尝试没有,相同的结果。 – OGF

+0

我仍然收到套接字异常...你应该在你的代码中捕获它。你有没有有效的连接细节可以尝试? –

+0

是的,当我输入错误的端口/不存在的一个,否则,如果它是正确的机器人连接程序崩溃和机器人显然不断开,直到我终止.exe – OGF