トップ回答者
IRCサーバーに接続、またはチャンネルに入ることが出来ない

質問
-
はじめまして。Faubyです。
IRCボットを作りたいのですが、
http://www.c-sharpcorner.com/UploadFile/pasihavia/IrcBot11222005231107PM/IrcBot.aspx
のソースをアレンジして実行してみましたが、他のクライアントで見ていてもボットがチャンネルに入ってきません。
どうすればIRCサーバーに接続、及びチャンネルに入ることが出来ますか。
コード ブロックusing System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;namespace IRCBot
{
class Bot
{
public static string Server = "irc.reicha.net";
private static int Port = 6667;
private static string User = "fabi";
private static string Nick = "Fabi";
private static string Channel = "#Faubynet";public static StreamWriter writer;
public static void Main(string[] args)
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
string nickname;
TcpListener tcpl;try
{
IPAddress localAddr = IPAddress.Parse("127.0.0.1");//192.168.1.4
Console.WriteLine(localAddr);
tcpl = new TcpListener(localAddr, Port);
tcpl.Start();
Console.WriteLine("Start!");
irc = new tcpl.AcceptTcpClient();//TcpClient(Server, Port);
Console.WriteLine("Connected!");
stream = irc.GetStream();
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
// Start PingSender thread
PingSender ping = new PingSender();
ping.Start();
writer.WriteLine(User);
writer.Flush();
writer.WriteLine("NICK " + Nick);
writer.Flush();
writer.WriteLine("JOIN " + Channel);
writer.Flush();
while (true)
{
while ((inputLine = reader.ReadLine()) != null)
{
if (inputLine.EndsWith("JOIN :" + Channel))
{
// Parse nickname of person who joined the channel
nickname = inputLine.Substring(1, inputLine.IndexOf("!") - 1);
// Welcome the nickname to channel by sending a notice
writer.WriteLine("NOTICE " + nickname + " :Hi " + nickname +
" and welcome to " + Channel + " channel!");
writer.Flush();
// Sleep to prevent excess flood
Thread.Sleep(2000);
}
}
// Close all streams
writer.Close();
reader.Close();
irc.Close();
}
}
catch (Exception e)
{
// Show the exception, sleep for a while and try to establish a new connection to irc server
Console.WriteLine(e.ToString());
Thread.Sleep(5000);
string[] argv = { };
Main(argv);
}}
}
class PingSender
{
static string PING = "PING :";
private Thread pingSender;
// Empty constructor makes instance of Thread
public PingSender()
{
pingSender = new Thread(new ThreadStart(this.Run));
}
// Starts the thread
public void Start()
{
pingSender.Start();
}
// Send PING to irc server every 15 seconds
public void Run()
{
while (true)
{
Bot.writer.WriteLine(PING + Bot.Server);
Bot.writer.Flush();
Thread.Sleep(15000);
}
}
}
}- 編集済み Fauby 2009年2月27日 16:32