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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:42:24+00:00 2026-05-25T14:42:24+00:00

My question is more general to network programming, but because I’m trying to write

  • 0

My question is more general to network programming, but because I’m trying to write a Modbus TCP server (slave), I will use it to illustrate my question.

In a Modbus TCP frame, the 5th and 6th byte of the frame give the size of the frame:
bytes 1 & 2: Transaction Id
bytes 3 & 4: Protocol Id
bytes 5 & 6: How many bytes remain in the frame
bytes 7 – n: The rest of the frame.

When using NetworkStream.Read()/BeginRead(), how can I delimit the received frames?

I’ve seen some open source implementations that just read the first 6 bytes, parse bytes 5 and 6 to get the size of the rest of the frame, and then read the rest of the frame. But if you don’t have start and end delimiters in your frame, how do you know where the frame begins and ends?

For example, maybe the client sent a few garbage characters, then sent a good frame. In this case if I just read the first 6 bytes, I’ll miss the good frame, and possibly future frames because I’m out of sync with the rhythm of the client.

This question isn’t really specific to Modbus, but to any protocol that uses a “size” field to specify the size of the frame. How do you delimit the frames?

I feel like I’m missing something fundamental to network programming, but maybe the protocol is just not well-designed.

  • 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-25T14:42:24+00:00Added an answer on May 25, 2026 at 2:42 pm

    Clients are not supposed to send garbage characters in most Internet protocols: If a client sends garbage, the server will send an error and close the connection. If the client wants to continue communicating with the server, it needs to create a new connection and behave properly this time.

    There a some protocols that use a Self-synchronizing code. This allows a recipient to find the start of the next frame from any position within the stream.

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

Sidebar

Related Questions

I asked a related question about findbugs, but let's ask a more general question.
My question is not about a specific code snippet but more general, so please
I know this SO question, but it deals with the subject in more general
I've found a similar question here , but I'm looking for more general solutions.
This is more a general question but my particular case involves a ruby/rails app
I know it is more general question but i am curious about that.Android is
I'm not posting any code here, because the question is more general. So I
I'm using IsapiRewriter on IIS6, but I think this question is more general than
This is a more general question, but which has wider implications for a data
I have a more general question but any advice in implementing this in C#

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.