2014-02-06 104 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