IRC Bot not identifying with server - Connection hangs
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 - usi开发者_如何学JAVAng .WriteLine fixed it. Thanks!
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
精彩评论