yokzu

yokzu

  • NA
  • 306
  • 0

Problem at second connection attempt

Jan 11 2010 8:25 AM
Hi.
I am making just a simple socket program. Server program is always working. When I open client program and click the button, sending/receiving data normally. Everything's ok. But when I close client and opening again, and then clicking button, client program getting lock. Not getting error. When I debug, its waiting on "readline" line. What is the problem? Why its not working when I reopen client program?

Client

private void Form1_Load(object sender, EventArgs e)
        {
           
            try
            {
                tcp_client = new TcpClient("192.168.1.222", 4444);
            }
            catch
            {
                label2.Text = "not connected";
                return;
            }

            ag_akimi = tcp_client.GetStream();
            akim_okuyucu = new StreamReader(ag_akimi);
            akim_yazici = new StreamWriter(ag_akimi);
          
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                akim_yazici.WriteLine("sample ");
                akim_yazici.Flush();
                textBox1.Text += akim_okuyucu.ReadLine();
            }
            catch
            {
                MessageBox.Show("error!");
            }

        }

        private void form_kapatiliyor(object sender, FormClosingEventArgs e)
        {
            try
            {
                akim_okuyucu.Close();
                akim_yazici.Close();
                ag_akimi.Close();
                istemcisoketi.Close();
            }
            catch
            {
                MessageBox.Show("Not properly closed");
            }
        }
    }
***********************************************
server

   public void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            thread_dinleyici = new Thread(new ThreadStart(dinle));
            thread_dinleyici.Start();
        }


        public void dinle()
        {
            tcp_listener = new TcpListener(IPAddress.Any, 4444);
            tcp_listener.Start();
            istemcisoketi = tcp_listener.AcceptSocket();
            ag_akimi = new NetworkStream(istemcisoketi);

            if (istemcisoketi.Connected)
            {
                while (true)
                {
                    akim_yazici = new StreamWriter(ag_akimi);
                    akim_okuyucu = new StreamReader(ag_akimi);
                    try
                    {
                        richTextBox1.Text += akim_okuyucu.ReadLine();
                        akim_yazici.WriteLine("message");
                        akim_yazici.Flush();
                    }
                    catch
                    {
                        label1.Text = "closing";
                        istemcisoketi.Close();
                        ag_akimi.Close();
                        akim_yazici.Close();
                        akim_okuyucu.Close();
                        istemcisoketi.Close();
                        return;
                    }
                }
            }
            else
            {
            }
        
        }



Answers (2)