C# IRC机器人GUI崩溃?
好的,机器人连接很好,但是当我创建机器人时,表单崩溃了。机器人保持连接,但也保持连接!关于命令不起作用。如果我在控制台应用程序中使用相同的代码,一切都会正常工作。问题只是UI表单C# IRC机器人GUI崩溃?,c#,c#-3.0,C#,C# 3.0,好的,机器人连接很好,但是当我创建机器人时,表单崩溃了。机器人保持连接,但也保持连接!关于命令不起作用。如果我在控制台应用程序中使用相同的代码,一切都会正常工作。问题只是UI表单 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usin
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:
* :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();
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();
}
}
}
这是安装窗口我怀疑导致问题的原因是您正在执行
This.Close()
,然后它被处理掉了。调用Close()
后,您进入for()
并尝试对可能已处理的资源进行操作。您应该给出更多代码来分析它。需要表单的代码。好的,我添加了另一个表单,不确定您是否也要查看设计器代码,@GrzegorzSławeckiw崩溃的原因是什么?Stacktrace?@PeteGarafano不知道,如果是这样,整个应用程序就会崩溃。试图解决它,我注意到它的发生是因为for循环。也尝试了一段时间的循环,但结果是一样的。我尝试删除它并尝试了它,但没有得到相同的结果。我仍然收到套接字异常。。。您应该在代码中捕捉到这一点。你有任何有效的连接详细信息可以尝试吗?是的,当我输入错误的端口/一个不存在的端口时,我就知道了,否则如果正确,bot连接程序就会崩溃,bot显然不会断开连接,直到我终止.exe