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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:18:18+00:00 2026-05-13T16:18:18+00:00

Howdy. I am in a networking class and we are creating our own networking

  • 0

Howdy. I am in a networking class and we are creating our own “networking API” using the socket functions we have learned in class thus far.

For the assignment the professor provided the already complete chat and server programs and we were to fill in a blank .c file that had an associated header file that described the function calls in our little “networking API” that the chat and server programs used.

I am close to done but have ran into a problem. I was gonna go in and mess with my code, but all the other parts of my “networking API” work so I didn’t want to to screw with it when I am so close to done and have a little change cause other parts not to work. Plus I am not 100% sure that I do in fact know what my bug is.

My problem is with my function called recv_line that is supposed to mimic recv().

My function recv_line gets handed the file descriptor from the client for the connection that was setup with the server. I then go ahead and associate a stream with the file descriptor using fdopen. The bit of code looks like:

  // Associate a stream with the sock_fd
  if (NULL == (fdstream = fdopen(sock_fd, "r"))) {
    return -1;
  }

Now this is where I believe my bug to be but I am not 100% sure as I said. Firstly, the first time I use my recv_line function it works great. It does in fact recv_line the line as expected. But the problem arises the second time around.

I stepped through my function with GDB and the above bit of code does excute without problems. I then go on to peel off a character from my stream (using fgetc) and check if it is equal to the EOF char. If it is I return, else I will process it. The second time that I call recv_line it returns cuse it read the EOF char every time.

I’m assuming this is happening because the data sent to the client is in the original stream (the one created the first time around) and the second time I call fdopen there isn’t any data in the second stream I created? I’m not completely sure how fdopen works when I call it the second time around?

On I side note I my reading of my data this way as we are supposed to continue reading data until we read an EOF or the character sequence \r\n. I use getc and ungetc with the stream to provide a look ahead mechanism that will check for the EOF or the \r\n sequence.

So with all the being said, does anyone know what exactly is going on with my big of code and program? I Googled around for a method to check if a FD has an associated stream to it and wasn’t successfully in finding/seeing anything. I did some research to see if there was some fstat time function that would let me see associated steams and came up with nothing. Can I just dup the FD that is passed in to the function recv_line and then fdopen on the dupped FD and avoid all these problems?

Thanks for all the help. I am sorry if I wasn’t clear enough. I will do my best to clarify anything if you ask for me to do so. And sorry my question was so lengthy. =*(

Thanks again though. I really appreciate your help.

  • 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-13T16:18:19+00:00Added an answer on May 13, 2026 at 4:18 pm

    I’m assuming this is happening because the data sent to the client is in the original stream (the one created the first time around) and the second time I call fdopen there isn’t any data left in the file descriptor for in the second stream to read I created?

    That is pretty much correct (with some minor corrections). The major duty of a stream object is to read the file descriptors data stream from the kernel to userland so you don’t need the overhead of a system call. But once the stream object reads the data stream, the kernel advances it’s ‘read pointer’ so the next time you try to read the data from the file descriptor, it picks up where you left off.

    You can only call fdopen one time for a given descriptor. If you can restructure your code so that there is a place where you can easily call fdopen once, that is the way to go.

    Once you call fdopen, you should no longer do anything with the file descriptor – not even close it (when you call fclose the underlying descriptor will be closed).

    Based on Jonathon Leffler’s comment, I’m guessing the professor’s code is also going to close the descriptor. If that is true, you cannot use fdopen.

    Some ideas:

    1. Just read the fd byte by byte. Not very performant but would work.
    2. Are you allowed to assume that your code is only handling one connection at a time? If so, just have a static buffer in recv_line that buffers the fd data. Fill up the buffer when it’s empty and then read data from it.
    3. If you need to handle multiple connections, you can extend #2 to have one buffer per file descriptor. You will need some sort of map to handle this.

    Update based on comment

    @Chris – let me explain the risk of using fdopen. Once you have used fdopen on a file descriptor, you must call fclose (or you risk leaking resources allocated for the FILE *) and you cannot call close on the descriptor (since fclose the descriptor for you can run into problems closing the descriptor multiple times). So, unless your code has already been delegated the responsibility of closing the descriptor (in which case you can then just call fclose), you can’t use fdopen.

    There is no built in way to detect if fdopen has been called on a descriptor. You need to add logic yourself. You would need to keep a collection of FILE *’s returned by fdopen; if you already had a FILE * for a given descriptor, you would use it and otherwise you would call fdopen then.

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

Sidebar

Related Questions

Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I
Howdy... here on my local LAN, I have a Windows Server 2k8 box with
Howdy, codeboys and codegirls! I have came across a simple problem with seemingly easy
Howdy. Consider the following: SQL> DECLARE 2 b1 BOOLEAN; 3 b2 BOOLEAN; 4 FUNCTION
What I want is, I think, relatively simple: > Bin = <<Hello.world.howdy?>>. > split(Bin,
Howdy and Thanks in Advance! I'm trying to access the USB port from a
Howdy! I am looking for a way to list all of the image files
Howdy, I'm writing a batch script that will run on a Windows XP machine.
Howdy all. I am trying to solve a problem which is apparently not uncommon
Howdy - I've written a very simple app to accept job applications including a

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.