473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

fclose

If this in not an ANSI-C question, then just flame me and I'll be on my
way....

I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?

TIA,
Jeff

Nov 14 '05 #1
6 5115
Jeff <je**@whitehouse.gov> scribbled the following:
If this in not an ANSI-C question, then just flame me and I'll be on my
way.... I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?


This is not meant to be a flame, but socket descriptors and fdopen() are
not part of ISO standard C. So therefore your question is off-topic on
comp.lang.c. Please ask in comp.unix.programmer.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Nov 14 '05 #2
Jeff wrote:

If this in not an ANSI-C question, then just flame me and I'll be on my
way....

I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?


You've been burned by the anticipated flame ;-)

The problem is with the interaction between the C-defined
fclose() and the non-C components like fdopen() and sockets.
And the upshot is that the unaided C language can't help you
with problems outside its scope. You'll need to seek advice
from a newsgroup or other source that deals with the platform(s)
on which you have the problem.

<off-topic>

It's sometimes possible but always risky to mix different
"access paths" to a single object. On POSIXoid systems it usually
turns out that the FILE* mechanisms are built atop the "lower-
level" file descriptor mechanisms, and as such they are likely to
be confused if you manipulate the lower-level stuff without their
knowledge. By closing the socket while its FILE* object still
thinks it's open you're, you're, uh -- well, imagine getting some
memory from malloc() and then releasing it with munmap(); can
you think of some of the kinds of trouble that might result?

</off-topic>

--
Er*********@sun.com
Nov 14 '05 #3
Jeff <je**@whitehouse.gov> wrote in message news:<Hl****************@nwrdny01.gnilink.net>...
If this in not an ANSI-C question, then just flame me and I'll be on my
way....

I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?


I guess if the socket is closed, the fdopen get a NULL pointer return,
you pass NULL pointer to fclose, it has memory fault.

Why don't you want to check the fdopen reutrn value is NULL or ERROR
condition?
Regards
Nov 14 '05 #4
In <Hl****************@nwrdny01.gnilink.net> Jeff <je**@whitehouse.gov> writes:
I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?


Once you have built a stream around your socket descriptor, you're no
longer allowed to access that socket descriptor directly, ALL the
accesses must be performed using the stream interface. In this case,
your problem goes away, because the only way of closing the socket is
by closing the associated stream.

This is less off topic than it might look, because the advice is valid
for any low level I/O mechanism used by the stdio stream implementation,
not only for file descriptors.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Dan Pop wrote:
In <Hl****************@nwrdny01.gnilink.net> Jeff <je**@whitehouse.gov> writes:

I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I
pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?

Once you have built a stream around your socket descriptor, you're no
longer allowed to access that socket descriptor directly, ALL the
accesses must be performed using the stream interface. In this case,
your problem goes away, because the only way of closing the socket is
by closing the associated stream.

This is less off topic than it might look, because the advice is valid
for any low level I/O mechanism used by the stdio stream implementation,
not only for file descriptors.


If this is true, then why would I get the memory fault when I fclose
the stream?

Jeff

Nov 14 '05 #6
Jeff wrote:

Dan Pop wrote:
In <Hl****************@nwrdny01.gnilink.net> Jeff <je**@whitehouse.gov> writes:

I am using fdopen to associate a FILE with a socket descriptor. (Because
I've mentioned a socket descriptor I anticipate the flame. But I believe
this is a C question.) My problem is this: if the socket is closed and I ^^^^^^^^^^^^^^^^^^^^^^^pass the FILE returned from fdopen to fclose, my program dies with a
memory fault. My question: why do I get a memory fault with fclose when
the socket descriptor is closed and how can I prevent that?

Once you have built a stream around your socket descriptor, you're no
longer allowed to access that socket descriptor directly, ALL the ^^^^^^^ accesses must be performed using the stream interface. In this case, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
your problem goes away, because the only way of closing the socket is
by closing the associated stream.

This is less off topic than it might look, because the advice is valid
for any low level I/O mechanism used by the stdio stream implementation,
not only for file descriptors.


If this is true, then why would I get the memory fault when I fclose
the stream?


Because the socket has already been closed. Hence, some
mechanism other than fclose() closed it. Hence, the program
has failed to observe the "ALL the accesses..." requirement
by allowing this other mechanism to disturb the socket.

--
Er*********@sun.com
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: VB | last post by:
Hi, here File.cpp and File.h: File.cpp: ---------------------- #pragma warning (disable: 4786)
10
by: collinm | last post by:
hi is it better to do: FILE *fp; if((fp = fopen(local_led, "r"))!=NULL) { fclose(fp); } else
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
17
by: kathy | last post by:
if fopen failed, does it necessary to call fclose? I see an example like this: .... stream = fopen(...); if(stream == NULL) { .... } else
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
53
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
16
by: Bill Cunningham | last post by:
Is it really necessary to check the return type (int) or fclose ? fopen I can understand but what about using fflsuh(fp); /* fopen's pointer */ fclose(fp); Would that take care of any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.