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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:18:55+00:00 2026-06-18T01:18:55+00:00

With the following pseudo-Python script for sending data to a local socket: s =

  • 0

With the following pseudo-Python script for sending data to a local socket:

s = socket.socket(AF_UNIX, SOCK_STREAM)
s.connect("./sock.sock")
s.send("test\n")
s.send("aaa\0")
s.close()

My C program will randomly end up recving the following buffers:

  • test\n
  • test\n<random chars>
  • test\naaa (as expected)

The socket is being recv()‘d after select() points that the socket is readable. Question is, how to avoid the first two cases?

And side question: Is it possible to send the following two messages from that script:

  • asd\0
  • dsa\0

And have select() to show the socket as readable on each of those sends, or will it only do that if I run the script again (restarting the socket client connection) and sending a message for each connect?

  • 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-06-18T01:18:56+00:00Added an answer on June 18, 2026 at 1:18 am

    At a guess, the len argument to recv specifies a maximum amount of data to read, not the precise amount to be returned. recv is free to return any amount of data up to len bytes instead.

    If you want to read a specific number of bytes, call recv in a loop.

     int bytes = 0;
     while (bytes < len) {
        int remaining = len - bytes;
        int read = recv(sockfd, buf+bytes, remaining, 0);
        if (read < 0) {
            // error
            break;
        }
        bytes += read;
     }
    

    As noted by junix, if you’ll need to send unpredictable amounts of data, consider defining a simple protocol that either starts each message with a note of its length or ends with a particular byte or sequence of bytes.

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

Sidebar

Related Questions

The following is pseudo code of an integration test that is failing: [Test] void
Take the following pseudo C# code: using System; using System.Data; using System.Linq; using System.Collections.Generic;
I search the Python equivalent for the following Bash code: VAR=$(echo $VAR) Pseudo Python
Using Oracle 10gR2, I need to produce something like the following pseudo-example from data
The following (pseudo-) code induces some behaviour which I dont understand. I have 2
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I want to build the following pseudo query Select a From APDU a where
I am currently working with the following pseudo code block to fix a regex
I think I might need to do something like the following pseudo-code in my
I can drop a SqlServer Backup Device using SQL-DMO using the following pseudo-code: SQLDMO.SQLServer2

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.