jonnytabpni 0 Light Poster

Hi folks,

I'm currently trying to send data from a client to a server. It is an xml file.

The client has this code:

public void sendXML(XmlDocument xdoc)
        {
            try
            {
                //Using the network stream to send data
                NetworkStream nts = new NetworkStream(Network.m_clientSocket);
                xdoc.Save(nts);
                nts.Flush();
            }
            catch
            {
                Connected = false;
            }
        }

and the server has code like this:

public void Start()
        {
            NetworkStream nStream;
           
            string data;

            tcpListener.Start();

            TcpClient tcpClient = tcpListener.AcceptTcpClient();
            nStream = tcpClient.GetStream();
            

            while (true)
            {
                
                byte[] buffer = new byte[1024];
                //Read from the Network Stream into buffer
                nStream.Read(buffer, 0, buffer.Length);
                while (nStream.DataAvailable)
                {
                    nStream.Read(buffer, 0, buffer.Length);
                }
                //Convert buffer into string
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                data = enc.GetString(buffer);


                

                this.CheckData(data);
            }
        }

The problem lies in the fact that the xml file is being split up into 2 reads of the network stream meaning that the CheckData method isn't getting the whole file at once.

How would I go about fixing this?

Thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.