Why does my IRC bot not connect?
Asked Answered
M

1

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

Why does my IRC bot not connect?

Morpheme answered 27/2, 2010 at 18:34 Comment(4)
What makes you think it's not connecting? Is there an error?Overmodest
check if port 6667 is blocked. In my case, i am behind a proxy server which has blocked 6667. so i cannot connect to IRC on 6667.Encephalomyelitis
@Mr. Sharp: Does your bot actually not connect? ie: If you /who Testing (I think that's the right command) is he not there? If you read from the StreamReader are you getting any error messages from the server?Obed
I recommend using an existing IRC client library instead of doing it all yourself. eg thresher.sourceforge.netDene
G
3

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");
Guessrope answered 27/2, 2010 at 18:38 Comment(2)
The latter can be checked in the RFC or if you are connecting to a particular IRC daemon, its documentation can be referenced also.Masticate
You'll also need to answer any PINGs that come before and during registration. Many IRC servers send these as a primitive anti-bot check, simple bots do not respond and receive a ping timeout or registration timeout message. The PING will contain unique text which must be answered in your PONG message.Decamp

© 2022 - 2024 — McMap. All rights reserved.