TcpClient;NetworkStream.Read(); method

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace WindowsFormsApp17
{
    public partial class Form1 : Form
    {
        TcpListener tcpListener = null;

        TcpClient tcpClient = null;

        int x = 0;

        public Form1()
        {
            InitializeComponent();

            tcpListener = TcpListener.Create(8000);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (tcpListener.Pending())
            {
                label1.Text = "Pending request detected!";

              

            }
            else
            {
                label1.Text = "Pending request not detected.Trying to detect...";
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();

            tcpListener.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();

            tcpListener.Stop();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {

                tcpClient = new TcpClient("192.168.1.5", 25);

                label1.Text = "Connected";

                x = Encoding.ASCII.GetByteCount("Hello google!");

                tcpClient.GetStream().Write(Encoding.ASCII.GetBytes("Hello google!"), 0, Encoding.ASCII.GetByteCount("Hello google!"));

                label1.Text = x.ToString();

            }
            catch (Exception exc)
            {
                label1.Text = exc.Message;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            byte[] data = { };

            tcpClient.GetStream().Read(data, 1, x);

            label1.Text = Encoding.ASCII.GetString(data);
           
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            

            

          
           
        }
    }
}

Open in new window


Well , hello I here's my problem;

I press button 3 to create my tcp client,connect to the remote ipaddress and when I press button 4 to receive the data it occurs an exception out of range

What am I missing?
LVL 1
Max DestinyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical SpecialistCommented:
What is text value for label1.Text after you tried?
MishaProgrammerCommented:
May be you  not correct use method  NetworkStream.Read...

The third paramente must be the number of bytes to read from the NetworkStream.
And size of data buffer must be equal number of read buffer.

MSDN
Max DestinyAuthor Commented:
it says that x is 13 so the the text value equals to "13";
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

MishaProgrammerCommented:
And what about method "read"?
Max DestinyAuthor Commented:
buffer size is x so it is 13;
MishaProgrammerCommented:
The length of your buffer "data" = 0; May be you should create data buffer with lingth = x ?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.