Reputation: 17213
Okay, so i have this code i wrote:
class Connection
{
public static StreamWriter writer;
public static string SERVER;
private static int PORT;
private static string USER;
private static string NICK;
private static string CHANNELS;
private Thread connection;
private Thread ping;
public Connection()
{
connection = new Thread(new ThreadStart(this.Run));
ping = new Thread(new ThreadStart(this.Ping));
}
public void Start(string server, int port, string ident, string realname, string nick, string channels)
{
SERVER = server;
PORT = port;
USER = "USER " + ident + " 8 * :" + realname;
NICK = nick;
CHANNELS = channels;
connection.Start();
ping.Start();
}
public void Ping()
{
while (true)
{
try
{
writer.WriteLine("PING :" + SERVER);
writer.Flush();
Console.WriteLine("Ping: " + SERVER);
Thread.Sleep(15000);
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
}
}
public void Run()
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
try
{
irc = new TcpClient(SERVER, PORT);
stream = irc.GetStream();
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
writer.WriteLine(USER);
writer.Flush();
writer.WriteLine("NICK " + NICK);
writer.Flush();
Thread.Sleep(5000);
writer.WriteLine("JOIN " + CHANNELS);
writer.Flush();
while (true)
{
while ((inputLine = reader.ReadLine()) != null)
{
Console.WriteLine(inputLine);
}
writer.Close();
reader.Close();
irc.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Thread.Sleep(5000);
Run();
}
}
}
now, that workes perfectly, when i introduce the first server:
Connection one = new Connection();
one.Start("irc.serveraddress.com", 6667, "ident", "realname", "nick", "#channel");
it pings the server on time, everything.. but as soon as i introduce a second connection:
Connection two = new Connection();
two.Start("irc.differentserveraddress.com", 6667, "ident", "realname", "nick", "#channel");
it stops pinging the first server. and just pings the second server, how can i make that in a way that it will continue to ping both servers?
Upvotes: 1
Views: 709
Reputation: 92520
Remove all of your "static"s. You should be closing your connections and whatnot over the class object, not creating a single instance.
Upvotes: 1
Reputation: 29956
public static string SERVER;
Since this field is static, all instances of your class are pointing to the same server. When you create the second instance, it stores the name of the second server in this field, which means the first instance uses it too.
Upvotes: 2