7
public static string SERVER = "irc.rizon.net";
private static int PORT = 6667;
private static string USER = "Test C# Irc bot";
private static string NICK = "Testing";
private static string CHANNEL = "#Test0x40"; 

public static void Main(string[] args)
{
    NetworkStream stream;
    TcpClient irc;
    StreamReader reader;
    StreamWriter writer;

    irc = new TcpClient(SERVER, PORT);
    stream = irc.GetStream();
    reader = new StreamReader(stream);
    writer = new StreamWriter(stream);

    writer.WriteLine("NICK " + NICK);
    writer.Flush();
    writer.WriteLine("JOIN " + CHANNEL);
    writer.Flush(); 

    Console.ReadKey(true);
}

为什么我的 IRC 机器人无法连接?

4

1 回答 1

3

IRC 协议需要 CR/LF 对,而 StreamWriter 的默认行为只是换行。您应该像这样创建您的 StreamWriter:

writer = new StreamWriter(stream) { NewLine = "\r\n", AutoFlush = true };

此外,您可能应该在加入频道之前使用 USER 命令指定用户名,尽管我不确定它是否完全有必要:

writer.WriteLine("USER username +mode * :Real Name");
于 2010-02-27T18:38:29.337 回答