Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7633591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:55:40+00:00 2026-05-31T06:55:40+00:00

Hello I think my server reads wrong value from TcpClien.Write it should be 52(length

  • 0

Hello I think my server reads wrong value from TcpClien.Write it should be 52(length byte array at the moment I sending it), but at server side when I look at size in TcpClient.Read it’s allways 8192. What I’m doing wrong?

My Client:

using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.IO;

using System.Text.RegularExpressions;
namespace client
{
    public partial class Form1 : Form
    {
        TcpClient clientSocket = new TcpClient();
        NetworkStream serverStream;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Multiline = true;
            msg("Client Started");
            label1.Text = "Client Socket Program - ";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Send();
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clientSocket.Connect("192.168.1.109", 8888);

            label1.Text = "Client Socket Program - Server Connected ...";
        }
        private void Send()
        {
            NetworkStream serverStream = clientSocket.GetStream();
            XElement outXML =XElement.Load("test.xml") ;

            byte[] outStream = ConvertXmlToByteArray(outXML, Encoding.UTF8);

            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }
        byte[] ConvertXmlToByteArray(XElement xml, Encoding encoding)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = encoding;
                settings.OmitXmlDeclaration = true; // No prolog
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    xml.Save(writer);
                }
                return stream.ToArray();
            }
        }

    }
}

My Server

using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient clientSocket = new TcpClient();
            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (true)
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
    }

    //Class to handle each client request separatly
    public class handleClinet
    {
        TcpClient clientSocket;
        string clNo;
        public void startClient(TcpClient inClientSocket, string clineNo)
        {
            this.clientSocket = inClientSocket;
            this.clNo = clineNo;
            Thread ctThread = new Thread(doChat);
            ctThread.Start();
        }
        private void doChat()
        {
            int requestCount = 0;
            requestCount = 0;

            while ((true))
            {
                byte[] bytesFrom = new byte[(int)clientSocket.ReceiveBufferSize];
                requestCount = requestCount + 1;
                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                Console.Write(clientSocket.ReceiveBufferSize.ToString());

                XElement inXML = ConvertByteArrayToXml(bytesFrom);
                inXML.Save("jest2.xml");

                //XMLSave(dataFromClient);

            }
        }
        //private void XMLSave(string inner)
        //{

        //    XmlDocument XmlDoc = new XmlDocument();
        //    XmlDoc.Load("jest.xml");
        //    XmlDeclaration xmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
        //    XmlNode nextNode = XmlDoc.CreateElement("root");
        //    nextNode.InnerText = inner;

        //    XmlDoc.DocumentElement.AppendChild(nextNode);

        //    XmlDoc.Save("jest.xml");
        //}
        XElement ConvertByteArrayToXml(byte[] data)
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            using (MemoryStream stream = new MemoryStream(data))
            using (XmlReader reader = XmlReader.Create(stream, settings))
            {

                return XElement.Load(reader);
            }
        }
        private byte[] RemoveNulls(byte[] DataStream)
        {
            for (int i = 0; i < DataStream.Length - 1; i++)
            {

                if (DataStream[i] == 0x00) DataStream[i] = 0x01;
            }
            return DataStream;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:55:41+00:00Added an answer on May 31, 2026 at 6:55 am

    NetworkStream.Read returns an integer which tells the number of read bytes.

    Hello I think my server reads wrong value from TcpClien.Write it should be 52(length byte array at the moment I sending it

    Incorrect. TCP is a stream based protocol. That means that TCP can split up your message however it likes. It can send a couple of bytes at a time, merge two messages into one etc.

    You’ll therefore have to implement some kind of message detection in your code. There are two ways that are usually used:

    1. Introduce a header that contains the number of bytes that the message is
    2. Use a delimiter at the end of the message
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys. I think it isn't possible just using PHP, but just to be
What I want is, I think, relatively simple: > Bin = <<Hello.world.howdy?>>. > split(Bin,
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello, everybody. We are developing an iPhone app which supported by a server-side that
Hello and thanks in advance. I am retrieving data from the db. The data
Hello i am having some problems sending mails thoug my site. I keep getting
Hello guys and girls im trying to a sql update but think i forgot
I'm trying to send XML doc to server from client. But when server get
Suppose file1 looks like this: bye bye hello thank you And file2 looks like
I'm stuck with this pretty silly thing; I got a textfile like this; Hello::140.0::Bye

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.