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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:09:36+00:00 2026-05-28T04:09:36+00:00

My C program has read (using read(2) or recv(2) ) a few bytes from

  • 0

My C program has read (using read(2) or recv(2)) a few bytes from a TCP socket on Linux. Is it possible to push these bytes back so that subsequent read(2) and recv(2) calls (issued deeply inside a library which I don’t control) will read them again?

I know about the MSG_PEEK flag of recv(2), and I’m going to use it as a workaround if pushing back turns out to be impossible.

  • 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-28T04:09:36+00:00Added an answer on May 28, 2026 at 4:09 am

    What I’m asking for seems to be impossible. I ended up calling recv() with the flag MSG_PEEK. This will make the subsequent recv() or recvmsg() call in the library read the same data.

    Without any other calls I can use it to conveniently look ahead only a single byte. Let’s suppose I needed to look ahead 2 bytes. I’d call recv(fd, buf, 2, MSG_PEEK). If 1 out of the 2 bytes have already arrived, then recv would return immediately, no matter how many times I call it. I can use epoll_ctl with EPOLLIN | EPOLLET to wait for the 2nd byte. If I want to know if there was an EOF afterwards, I need EOPLLIN | EPOLLET | EPOLLRDHUP. (Please note that EPOLLHUP won’t be returned on EOF.) So by using epoll_ctl I can avoid calling recv in a busy, polling loop to read the 2nd byte.

    I’ve just verified on my Linux system that I can peek about 900 kB to the socket this way by default. (SO_RECVBUF is 1 MB for me by default, decreasing it with setsockopt seems to decrease how much can be received, but not by a consistent amount. Maybe I decrease it too late?)

    Even the combination of MSG_PEEK and EPOLLET is a workaround, because they still don’t let me unread arbitrary bytes to the socket. All they let me do is peeking at the bytes already arrived without consuming them.

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

Sidebar

Related Questions

In a C++ program, I am trying to read data from MSSQL database using
My program has to read files that use various encodings. They may be ANSI,
My program has one dialog and two sockets. Both sockets are derived from CAsyncSocket,
The target language is C/C++ and the program has only to work on Linux,
I have a program using android and I would like a few tips on
I've been using SqlDataReader to read results from my querys to an SQL server
My program has two threads: Main execution thread that handles user input and queues
My program has the following class definition: public sealed class Subscriber { private subscription;
If a program has literally just deserialized an object (doesn't really matter how, but
The data model in my program has a number of discrete states, but I

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.