Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to build a chat, basically i used the invoke function what a thread. i am able to read what the server sends me, but i am able to write only once. i am trying to finish this but not sure how to write to server each time the server:
(take into account that i wrote this before in console application form and the server works fine..ie. the problem isnt with the server).

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        StreamReader sr;
        StreamWriter sw;
        TcpClient connection;
 
        public Form1()
        {
            InitializeComponent();
            Button btn1 = new Button();
            btn1.Click += button1_Click;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            connection = new TcpClient("127.0.0.1", 5000);
            sr = new StreamReader(connection.GetStream());
            sw = new StreamWriter(connection.GetStream());
        
        }
      
        private void button2_Click(object sender, EventArgs e)
        {
    
            Thread t2 = new Thread(Reader);
            t2.Start(connection);
        }
 
        string msg;
 
        public void Reader(object o)
        {
            TcpClient con = o as TcpClient;
            if (con == null)
            {
                return;
            }
            while (true)
            {
                msg = sr.ReadLine();
                Invoke(new Action(Output));
            }
        }
     
        public void Output()
        {
            ChatScreen.Text = msg;
        }
 
        string textinput;
 
        private void button1_Click(object sender, EventArgs e)
        {
            textinput = InputLine.Text;
            sw.WriteLine(textinput);
            sw.Flush();
        }
    }
}


what i thought to do is to connect the button so it will be able to do multiple clicks ..e.g btn.Click()..or run a thread with invoke on the WriteLine but my intuition says that making the button click several times would make the program work
Posted
Updated 6-Feb-11 23:50pm
v2

1 solution

Perhaps if you added the button to the form, it would help things...
In your constructor add:
Controls.Add(btn1);
after the click handler

If the user can't see the button, he can't click it!
 
Share this answer
 
Comments
Dmitry Makovetskiy 7-Feb-11 6:07am    
Griff, i gave up, and reset the whole code to what it used to be a month ago, now i am going to start the work from the begining. i want to do private messaging, any idea on how i can specify the server to chat with one user / connection and not the other?,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900