使用 C# 连接到 IRC 服务器

发布于 2024-09-08 00:49:17 字数 966 浏览 9 评论 0原文

我一直在尝试使用简约的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-09-15 00:49:17

首先,我建议您查看相应的 RFC:

http://www.faqs。 org/rfcs/rfc2812.html

查看连接注册。您需要按照以下步骤获取连接:

  1. 传递消息
  2. 尼克消息
  3. 用户消息

您缺少 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:

  1. Pass message
  2. Nick message
  3. User message

You're missing the USER command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文