使用 C# 连接到 IRC 服务器
我一直在尝试使用简约的 IRC 机器人,但我永远无法连接到工作。
我通过 TcpClient 对象执行此操作,我已经看到该对象在其他此类项目中使用,并且据报道这些项目有效。
这是代码。
private string server = "irc.freenode.net";
private int port = 6667;
private string nick = "testingsharpbot";
private string channel = "testblablabla";
private TcpClient irc;
public ConfigForm() {
InitializeComponent();
}
private void ConnectButton_Click(object sender, EventArgs e) {
this.irc = new TcpClient(this.server, this.port);
using(NetworkStream stream = irc.GetStream()){
using(StreamReader sr = new StreamReader(stream)) {
using(StreamWriter sw = new StreamWriter(stream) {NewLine = "\r\n", AutoFlush = true}) {
sw.WriteLine("NICK " + this.nick);
sw.WriteLine("JOIN " + this.channel);
}
}
}
}
所以我等了一下,然后对昵称执行 /whois,但我总是得到相同的答复:用户不存在。
据我所知,TcpClient 建立连接,然后我可以使用 NetWorkStream 实例读取和写入该连接。
我还需要做什么?
I've been trying my hand at a minimalistic IRC bot, but I can never get a connection to work.
I'm doing this via a TcpClient object, which I've seen used in other such projects and those reportedly work.
Here's the code.
private string server = "irc.freenode.net";
private int port = 6667;
private string nick = "testingsharpbot";
private string channel = "testblablabla";
private TcpClient irc;
public ConfigForm() {
InitializeComponent();
}
private void ConnectButton_Click(object sender, EventArgs e) {
this.irc = new TcpClient(this.server, this.port);
using(NetworkStream stream = irc.GetStream()){
using(StreamReader sr = new StreamReader(stream)) {
using(StreamWriter sw = new StreamWriter(stream) {NewLine = "\r\n", AutoFlush = true}) {
sw.WriteLine("NICK " + this.nick);
sw.WriteLine("JOIN " + this.channel);
}
}
}
}
So I wait a bit and then do a /whois on the nickname, but I always get the same reply: user does not exist.
As far as I'm aware, the TcpClient makes the connection and I can then use the NetWorkStream instance to read and write to that connection.
What else do I need to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我建议您查看相应的 RFC:
http://www.faqs。 org/rfcs/rfc2812.html
查看连接注册。您需要按照以下步骤获取连接:
您缺少 USER 命令。
At first, I suggest you taking a look in the appropiate RFC:
http://www.faqs.org/rfcs/rfc2812.html
Look at Connection Registration. You need to follow this steps to get a connection:
You're missing the USER command.