0

这是代码:

private void connectToIrc()
        {
            //Connect to irc server and get input and output text streams from TcpClient.
            nick = textBox1.Text;
            owner = textBox2.Text;
            server = textBox3.Text;
            port = Convert.ToInt32(textBox4.Text);
            chan = "#" + textBox5.Text;
            sock.Connect(server, port);//server, port);

            richTextBox1.Invoke((MethodInvoker)delegate
            {
                ColorText.AppendText(richTextBox1, "Server: ", Color.Green);
                ColorText.AppendText(richTextBox1, server+"          ", Color.Red);
                ColorText.AppendText(richTextBox1, "Port: ", Color.Green);
                ColorText.AppendText(richTextBox1, port.ToString() + Environment.NewLine + Environment.NewLine, Color.Red);
            });
            if (!sock.Connected)
            {
                Console.WriteLine("Failed to connect!");
                return;
            }
            input = new System.IO.StreamReader(sock.GetStream());
            output = new System.IO.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
         //buf = input.ReadLine();
         while ((buf = input.ReadLine()) != null)
         {
             buf = buf + Environment.NewLine;
             //Display received irc message
             //Console.WriteLine(buf);
             richTextBox1.Invoke((MethodInvoker)delegate
             {
                 ColorText.AppendText(richTextBox1, buf,Color.Black);
             });
             if (buf.StartsWith("ERROR")) break;

             //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;

             //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" + "PRIVMSG " + chan + " :hello"
                 );
                 output.Flush();
             }
             buf = input.ReadLine();
         }

       }

我将这部分添加到代码中: + "PRIVMSG" + chan + " :hello" 但它什么也没做,我在频道中看不到任何“hello”。

我在这个网站上使用了这个例子:

http://jakash3.wordpress.com/2012/02/13/simple-vb-net-and-csharp-irc-client/

C# 示例。

加入频道后如何向频道发送消息?我想发送一条消息,如果它的下一步工作是将消息添加到队列中,它们将被一个一个地自动发送。

我该怎么做 ?

4

2 回答 2

1

仅仅因为您发送到您加入的服务器,并不意味着您立即进入频道。您需要等到您在实际频道中,(等待从服务器返回的 JOIN 命令)然后发送 privmsg。

于 2013-09-13T22:11:46.297 回答
0

您忘记了 PRIVMSG 命令末尾的 \r\n。

于 2013-08-25T09:20:55.290 回答