Home C on linux: using fclose() on Filepointers made from Socket descriptors
Reply: 1

C on linux: using fclose() on Filepointers made from Socket descriptors

gotjazz84
1#
gotjazz84 Published in 2017-12-07 12:08:43Z

I have to write a simple command-line client/server thingy for school and our teacher told us that using fdopen on Sockets is the easy way to go. A protocol is given that defines simple requests and responses only marked by EOF (so not within the request itself but the FIN flag will have to do).

Now my problem is that in order to get a response in the client I have to close the writing direction. For this I'm using fclose() Leaving out the details what I'm doing (skipping all the error handling and unrelated stuff)is pretty much:

sdr = socket(..); 
sdw=dup(sdr); 
writepointer = fdopen(sdw,"w");
readpointer = fdopen(sdr,"r");
send_message(writepointer); 
fclose (writepointer);

Now I would expect flcose() (which successfully return 0 btw) to send EOF, fflush(), close the pointer and close the underlying socket descriptor resulting in a fin flag being sent from the client to the server.

Now when I single step through the program and simultaniously look at tcpdump I see the request being sent (and ack'ed by the server) when I call fclose() but no FIN flag. This of course results in the Server not seeing that the request is done and my client getting very bored when trying to read a response later on.

I don't find many people using file pointers for tcp transfers here so a search for this didn't yield a whole lot.

Has anyone had this problem before?

David Schwartz
2#
David Schwartz Reply to 2017-12-07 12:11:03Z

You have two ways to cause a shutdown of a TCP socket and you are doing neither of them. One of them is to call shutdown. You are not doing that. The other is to close the last descriptor that references the connection. You are not doing that.

Do one of those two things.

Your expectation that this will trigger a shutdown is unreasonable. You still have a socket open to the connection that can be used to both read from it and write to it. So it would be quite surprising if a TCP protocol shutdown was triggered.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.351821 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO