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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:18:41+00:00 2026-05-26T22:18:41+00:00

I am trying to create a p2p applications on Linux, which I want to

  • 0

I am trying to create a p2p applications on Linux, which I want to run as efficiently as possible.

The issue I have is with managing packets. As we know, there may be more than one packet in the recv() buffer at any time, so there is a need to have some kind of message framing system to make sure that multiple packets are not treated as one big packet.

So at the moment my packet structure is:

(u16int Packet Length):(Packet Data)

Which requires two calls to recv(); one to get the packet size, and one to get the packet.

There are two main problems with this:

1. A malicious peer could send a packet with a size header of 
  something large, but not send any more data. The application will 
  hang on the second recv(), waiting for data that will never come.
2. Assuming that calling Recv() has a noticeable performance penalty
  (I actually have no idea, correct me if I am wrong) calling Recv() twice 
  will slow the program down.

What is the best way to structure packets/Recieving system for both the best efficiency and stability? How do other applications do it? What do you recommend?

Thankyou in advance.

  • 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-26T22:18:42+00:00Added an answer on May 26, 2026 at 10:18 pm

    I think your “framing” of messages within a TCP stream is right on.

    You could consider putting a “magic cookie” in front of each frame (e.g. write the 32-bit int “0xdeadbeef” at the top of each frame header in addition to the packet length) such that it becomes obvious that your are reading a frame header on the first of each recv() pairs. It the magic integer isn’t present at the start of the message, you have gotten out of sync and need to tear the connection down.

    Multiple recv() calls will not likely be a performance hit. As a matter of fact, because TCP messages can get segmented, coalesced, and stalled in unpredictable ways, you’ll likely need to call recv() in a loop until you get all the data you expected. This includes your two byte header as well as for the larger read of the payload bytes. It’s entirely possible you call “recv” with a 2 byte buffer to read the “size” of the message, but only get 1 byte back. (Call recv again, and you’ll get the subsequent bytes). What I tell the developers on my team – code your network parsers as if it was possible that recv only delivered 1 byte at a time.

    You can use non-blocking sockets and the “select” call to avoid hanging. If the data doesn’t arrive within a reasonable amount of time (or more data arrives than expected – such that syncing on the next message becomes impossible), you just tear the connection down.

    I’m working on a P2P project of my own. Would love to trade notes. Follow up with me offline if you like.

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

Sidebar

Related Questions

I am trying create a data.frame from which to create a graph. I have
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a new file with a Java Applet, but I don't know
I am trying to create a very basic flash game, using Tom Krcha's P2P
Trying to create a very piece of validation to prevent spammers. I want a
I am trying create a search box which will search datatables. The search box
I am trying create an application which will clone the GIT repository to the
I am trying create system in which I can login from php and then
Hey guys I am trying to learn how to create a P2P application in

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.