Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my IRC bot not connect?

Tags:

c#

.net

bots

irc

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);
}

Why does my IRC bot not connect?

like image 890
Mr. Sharp Avatar asked Feb 27 '10 18:02

Mr. Sharp


1 Answers

The IRC protocol wants CR/LF pairs, whereas the default behavior for StreamWriter is just line feeds. You should create your StreamWriter like this:

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

Additionally, you should probably specify a username with the USER command before joining a channel, although I'm not sure if it's completely necessary:

writer.WriteLine("USER username +mode * :Real Name");
like image 181
MikeP Avatar answered Sep 23 '22 22:09

MikeP