4

When I try and connect to freenode using a simple IRC bot in C#, I get the following messages:

:asimov.freenode.net NOTICE * :* Looking up your hostname...

:asimov.freenode.net NOTICE * :* Checking Ident

:asimov.freenode.net NOTICE * :* Couldn't look up your hostname

:asimov.freenode.net NOTICE * :* No Ident response

And then after about 30 seconds, the program terminates.

I have tried sending the USER message before the NICK message, and also have tried manually appending the carriage return "\r\n" to the end of the line instead of specifying it in the StreamWriter options. Any ideas as to other things that might cause this behavior?

Here is my current code:

        socket = new TcpClient(host, port);
        socket.ReceiveBufferSize = 1024;
        Console.WriteLine("Connected");
        NetworkStream stream = socket.GetStream();
        reader = new StreamReader(stream);
        writer = new StreamWriter(stream) { NewLine = "\r\n", AutoFlush = true };
        writer.Write("NICK " + user);
        writer.Write("USER " + user + " 8 * :" + user);
        writer.Write("JOIN " + chan);
        //System.Threading.Thread.Sleep(5000);

        while(running)
        {
            line = reader.ReadLine();
            Console.WriteLine(line);
        }

**Fixed - using .WriteLine fixed it. Thanks!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
WilHall
  • 11,644
  • 6
  • 31
  • 53
  • If you packetsniff this with Wireshark or similar, do you see your data actually being sent? I suspect it is sitting in a buffer somewhere. – Brad Apr 03 '11 at 21:42
  • 1
    I think you should use .WriteLine not .Write if you want streamwriter to append \r\n to each line and flush it. – Marcin Deptuła Apr 03 '11 at 21:45
  • 1
    Try using `writer.WriteLine` to make sure the newline is appended at the end. Edit: Ninja'd by @Ravadre :) – Håvard Apr 03 '11 at 21:47
  • @Harpyon: Yey, I became 1min ninja :-) – Marcin Deptuła Apr 03 '11 at 21:50
  • 1
    And you need to respond to PINGs. Else you'll get kicked very quickly. I don't remember if a PING can happen before registering. – CodesInChaos Apr 03 '11 at 21:52
  • @CodeInChaos, you are correct. After registering, there is a PING, and if you don't respond to it, you are disconnected. – Håvard Apr 03 '11 at 22:11
  • @CodeInChaos, Harpyon: Actually, that PING after registering depends on the server - for example, Freenode doesn't use it to my knowledge, I was able to connect and was never pinged right away. However, foonetic, I believe, does implement it. – WilHall Apr 04 '11 at 13:05
  • Your domain/servers/system must be setup to allow for reverse DNS. – Joshua Drake Apr 04 '12 at 19:30

1 Answers1

3

You're not flushing the buffer. use writer.Flush(); after each Write.

Example:

writer.Write("NICK {0}\r\n", user);
writer.Flush();

Or

writer.WriteLine("NICK {0}", user);
writer.Flush(); // Maybe here you will not need to flush because of the autoflush
Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36