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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:07:32+00:00 2026-06-18T10:07:32+00:00

After a socket has been setup, is it better practice to use read(2) and

  • 0

After a socket has been setup, is it better practice to use read(2) and write(2) calls on the socket descriptor, or to associate a stream with the socket descriptor using fdopen(3) and then use stdio(3) functions?

int sfd = socket(PF_INET, SOCK_STREAM, 0);
// setup the socket using sfd
FILE * stream = fdopen(sfd, "r+");
// use fprintf, fscanf, etc

EDIT: I also unbuffer the stream

setbuf(stream, NULL)

To avoid having to flush it as mentioned in the comments.

I’ve been using this approach because it lets me reuse code written for FILE* streams, and I have the advantage of being able to use format strings (I am working with human readable text). GNU seems to be suggesting that this is a good idea.

http://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html

However usually when I see code using sockets, the socket descriptor is used instead of a stream for all operations. Is there an advantage to using the lower level functions?

  • 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-18T10:07:34+00:00Added an answer on June 18, 2026 at 10:07 am

    If you need more precise control and handling of error conditions, use read and write. If you don’t, and prefer the convenience of stdio functions, then use the FILE* wrapper.

    One problem with using a FILE * wrapper is that you don’t have control over how and when data is actually written to the socket. This can result in inefficient network utilization and excessive delays (due to Nagle’s algorithm interacting with delayed ACK) if you’re not careful.

    I would recommend using read and write directly if this is a high-performance Internet application.

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

Sidebar

Related Questions

I can only seem emit messages to users when their socket id has been
I'm implementing an RTMP protocol using boost::asio::socket . After the async_accept, the protocol requires
Simply put, is it possible to reuse a socket after a TimedOut exception has
Been trying some different ways of detecting if a remote Socket has disconnected. I
Suppose, I have a connected socket after writing this code.. if ((sd = accept(socket_d,
I am developing a socket server where a client will be bound after being
I wanna kill the TCP connection listener thread(serverside) after client closes the socket.. The
In a client-server environment, after client connects sock = new Socket(serverName, SERVER_PORT); fromServer =
For last 48 hours, I have been trying to understand Multithreading and Socket Programming
I'm pretty new to Java sockets and I'm having problems using the same socket

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.