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

  • Home
  • SEARCH
  • 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 4079712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:50:12+00:00 2026-05-20T17:50:12+00:00

I have some experience with socket programming using the Berkeley socket API in C.

  • 0

I have some experience with socket programming using the Berkeley socket API in C. Generally, any socket programming requires a strategy that enables the receiving socket to know how much data it should receive. This can be accomplished with either header length fields, or delimiter characters. Generally, I prefer a header field which contains the length.

Of course, we also need to know the size of the length header field itself, which is simply a fixed size value that must be agreed upon by both sender and receiver. In C, this is easy to implement because native integer types are fixed size and in binary format, so you can just say something like:

uint16_t bytes_to_receive;
recv(sock, &bytes_to_receive, sizeof(bytes_to_receive), 0);
bytes_to_receive = ntohs(bytes_to_receive);
// Now receive 'bytes_to_receive' bytes...

But how is this sort of idiom accomplished using Python sockets? In Python, integers are objects and pickled integers are variable length byte arrays. So we can’t use a pickled integer as a length header field, because we can’t be sure of its size in bytes.

Of course, I could always send a byte array of known size containing a binary integer, like b'\x05\x00' to create a 16-bit binary integer with a value of 5 in little endian format, but this really doesn’t seem like the right approach.

So, how is this usually accomplished in Python?

  • 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-20T17:50:13+00:00Added an answer on May 20, 2026 at 5:50 pm

    You can use the struct module convert Python integers to and from strings/byte arrays. Just read the number of bytes that correspond to the size of the type header and convert it with the struct module and you should be good to go. (note: be sure to use the right endian-flags when encoding/decoding)

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

Sidebar

Related Questions

I have some experience making multiplayer turn-based games using sockets, but I've never attempted
I am a web-developer working in PHP. I have some limited experience with using
I'm new to NAnt but have some experience with Ant and CruiseControl. What I
I am working on a rails application (I have some experience with rails). But,
I have some UI in VB 2005 that looks great in XP Style, but
We have some input data that sometimes appears with &nbsp characters on the end.
I am new to C++ network programming but have experience with Java sockets etc.
I have some experience with JavaScript - but mainly with some small stuff, I
I've experienced rather strange behavior of JSTL forEach tag. I have some bean called
I have some ASP.NET web services which all share a common helper class they

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.