Search code examples
c#winformstcpclient

Using the same tcp client from 2 different forms


I am creating a tcpClient on my main form and i am reading and writing to an irc server.

tcpClient = new TcpClient(serverName, 6667);
reader = new StreamReader(tcpClient.GetStream());
writer = new StreamWriter(tcpClient.GetStream());
writer.AutoFlush = true;

at some point my app opens a second form with a listbox of some options and i want to double click on one of these options and write something to the initial stream. I have tried creating a new tcpClient on the same port with a new reader and writer but this does not seem to work.

    private void listBox_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        SendMessage("some message");
    }

    private void SendMessage(string message)
    {
        TcpClient tcpClient;
        StreamReader reader;
        StreamWriter writer;            
        string serverName = "chicago.il.us.undernet.org";
        tcpClient = new TcpClient(serverName, 6667);
        reader = new StreamReader(tcpClient.GetStream());
        writer = new StreamWriter(tcpClient.GetStream());
        writer.AutoFlush = true;
        writer.WriteLine("PRIVMSG #chan :" + message + "\r\n");

    }

Could anybody point me to the right direction please?


Solution

  • Just like you I am not an expert in C#, I only program as a hobby and I'm self taught. So maybe there is a better way to do this, but this is how I would approach it myself.

    Add a new class to your project like this:

    namespace Test //If you copy this, remember to change the namespace to match your application
    {
        class Shared
        {
            public static TcpClient client = new TcpClient(“server”, 6667);
    
            //OR do this and use the Connect method to connect later (see below)
            public static TcpClient client = new TcpClient();
        }
    }
    

    If you use the second declaration you could then connect later from ANY form (you would only need to connect once) when the form loads or on a button click etc. using this:

    Shared.client.Connect(serverName, 6667);
    

    Then in both of your forms you can do the following. Or if the SendMessage() function would be the same for both forms you could always make it a static function in the Shared class and call Shared.SendMessage("your message");.

    private void SendMessage(string message)
    {
    
        if (Shared.client.Connected)
        {
            StreamReader reader;
            StreamWriter writer;
    
            reader = new StreamReader(Shared.client.GetStream());
            writer = new StreamWriter(Shared.client.GetStream());
            writer.AutoFlush = true;
            writer.WriteLine("PRIVMSG #chan :" + message + "\r\n");
        }
    
    }
    

    Using static to declare client in the Shared class means that it is not referenced by an instance of the class. So you don't have to do Shared sharedClass = new Shared(); before you can access it, and if you made 2 instances of the Shared class client would only have one instance which is accessible to both instances of the class.