views:

94

answers:

1

can somebody tell me why my code is not working?

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();
    }
    public void Ping()
    {
        while (true)
        {
            try
            {
                Connection.writer.WriteLine("PING :" + SERVER);
                Connection.writer.Flush();
                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();
        }
    }
}

It connects to the server fine, but the ping Thread and voids seem to not functioning at all! and i dont know why, everything seems correct unless im missing something very obviousC

+4  A: 

You haven't started your ping thread. Call Start method of it.

Another note - don't use Thread.Sleep for threads/process synchronization. From my experience, code using it is usually slow, unreliable and hard to maintain. Use Monitor class or various WaitHandle implementations (e.g. AutoResetEvent) or whatever. For client/server communication try to wait until server response to your request (if such is defined in protocol), not just timeout.

Also, if you are starting new threads either make this threads stop (e.g. in Dispose method of your class) or set IsBackground property, or, preferrably, both. Otherwise these threads will block your process from stopping.

elder_george
+1 beat me to it...
Matt Davis
I don't belive this example is using Sleep for synchronisation. It's actually using Sleep for the purpose of pausing - eg to Ping every 15 seconds or so. I would argue that this is an example of using Sleep legitimately.
Andrew Shepherd
Maybe I misunderstood. But it seems it sends request to IRC server, waits for a while (although server will usually write proper response) and then sends another request. So it is used for processes synchronization, not threads, but nevertheless. But I'll modify my post to be more careful in phrasing.
elder_george