TCPIP connection in c# and data sending/receiving in another threads

1.7k Views Asked by At

I have a problem with my TCPIP connection Form programm.

I have a code, where I'm trying to send and receive some data from server. The main problem of my app is how to reconcile some threads:

  • myListenThread - to listening data from server
  • myReadStreamThread - to read data from server
  • System.Threading.Thread - main thread eg. to write data to server
  • captureThread - to do another things like capturing images from camera

Part of my code:

        private void buttonConnect_Click(object sender, EventArgs e)
    {
        try
        {
            Connect();

            Connected = true;
            this.myListenThread = new Thread(new ThreadStart(Listen));
            this.myListenThread.Start();
        }
        catch
        {
            MessageBox.Show("Invalid host! Try again.");
        }
    }

        private void Listen()
    {
        this.myReadStreamThread = new Thread(new ThreadStart(ReadStream));
        this.myReadStreamThread.Start();

        while (Connected)
        {
            if (!myReadClient.Connected)
            {
                Connect();
            }
        }
    }

        private void Connect()
    {
        IPAddress IP = IPAddress.Parse(textboxIP.Text);
        int PORT = Convert.ToInt32(textboxPORT.Text);
        this.myReadClient = new TcpClient();
        this.myReadClient.Connect(IP, PORT);//SOMETIMES HERE'S AN ERROR 
        this.myStream = this.myReadClient.GetStream();

        Properties.Settings.Default.IP = Convert.ToString(IP);
        Properties.Settings.Default.PORT = Convert.ToString(PORT);
        Properties.Settings.Default.Save();
    }

    private void ReadStream()
    {
        while (true)
        {
            try
            {
                this.myReadBuffer = new byte[this.myReadClient.ReceiveBufferSize];
                this.myBufferSize = myStream.Read(myReadBuffer, 0, this.myReadClient.ReceiveBufferSize);

                if (myBufferSize != 0)
                {
                    this.myString = Encoding.ASCII.GetString(myReadBuffer);
                    //myDelegate myDel;
                    //myDel = new myDelegate(Print);
                    //richtextboxRead.Invoke(myDel);
                }
            }
            catch
            {
                break;
            }
        }
    }

All is working correct when I'm connecting to server, but when I want to send some string the problem appears because of threads.

I decided to send string, by clicking Button3 and waiting until I receive string "1" from server using while loop:

        private void button3_Click(object sender, EventArgs e)
    {
        this.captureThread = new Thread(new ThreadStart(() => this.newGame()));
        this.captureThread.Start();
    }

        private bool newGame()
    {

        string command = "12345abc";

        if (Connected)
        {
            WriteStream(command);
        }

        while (myBufferSize == 0 && myString !="1") { }
        Thread.Sleep(2000);

         ...//doing other things
    }

        private void WriteStream(string command)
    {
        Connect();
        this.myWriteBuffer = Encoding.ASCII.GetBytes(command);
        this.myStream.Write(this.myWriteBuffer, 0, command.Length);
    }

And the problem with connection and data send/receive problem appears, when it should write my string "command" - it doesn't react. MyBufferSize is always 0 and myString is always null. Sometimes an Error about connection appears when I click Button3 (assigned in code). I think it is because in captureThread I can't see any data from another threads. How to solve it?

0

There are 0 best solutions below