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 1070965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:38:46+00:00 2026-05-16T20:38:46+00:00

I’m working on an application that reads and writes binary data from and to

  • 0

I’m working on an application that reads and writes binary data from and to a socket. So first I create a socket with TcpClient. But I’m stuck on reading the data from the socket. Are there any code samples on how to read binary data from the socket?

Concrete, there are 2 things I don’t understand. First, how do I know if a message is “received”? Do I need to create some kind of loop for this? And second: what is the best way to read this data, since the data I receive is binary and not just an ordinary string.

Thanks

  • 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-16T20:38:47+00:00Added an answer on May 16, 2026 at 8:38 pm

    The TCPClient documentation from Microsoft is actually pretty useful.

    How do read the data from the TCPClient

    The key part in the Microsoft example for reading the data from the socket is this:

    // Get the stream
    NetworkStream stream = client.GetStream();
    Byte[] data = new Byte[256];
    
    // String to store the response ASCII representation.
    String responseData = String.Empty;
    
    // Read the first batch of the TcpServer response bytes.
    int bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    

    They use the GetStream method of the TCPClient class and read all the available data (up to 256 bytes) from the stream.

    This is slightly cheating though, as they are just receiving one lot of data and assuming it is a complete and correct message. It will work fine so long as your client is 100% predicable in that it always sends the same thing then disconnects.

    Better way of handling this below…

    How do you know if a message is received?

    You don’t, you just keep receiving data until there is no more then check what you have received (or check as you go).

    To do this, you have to design the message to include some way of telling the length (integer at the start indicating length), or have some end of message marker (e.g. carriage return) or have a fixed length message.

    For example your message specification might be like this:

    DATA
    hello friend
    END

    By looking for END followed by a carriage return you will know you have found the end of the message. Likewise the DATA marker at the beginning helps you to tell where a message starts and split out the message data.

    What is the best way to read this data?

    All data sent/received will be in binary, you need to Encode and Decode the data. In the example, they are converting binary data to string (and the other way round) using the built in ASCII Encoding class GetString and GetBytes methods.

    Consider implementing a known protocol

    I would recommend to (where possible) use an established protocol rather than just sending plain text over TCP. There are standards for almost anything and anyone connecting to your application will find it easier to follow a known and defined standard.

    There are also many pre-built libraries for commonly used protocols saving you and your clients the hastle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

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.