Problem fread/fwriting sockets

Problem fread/fwriting sockets

Post by Jim Tomlinson (jimt » Wed, 24 Feb 1993 03:49:58



I'm doing a lot of client-server stuff here, using both Inet and UNIX
sockets.  For portability reasons, I'm fdopen()ing connected sockets,
and then using fread() and fwrite() to do the i/o.  I'm having a
problem where a client writes 512 bytes to the server, and, rather than
receiving that same data back (which is what should happen), it
receives it with the last byte it sent prepended.  That is, it's like
the last byte doesn't quite make it out the other end (although the
server reads it just fine), and is pushed back to the client in front
of the data being written by the server.  Everything looks like it's
working fine on the server end.  All these socket connections have a
setvbuf(FILE *,(char *) NULL,_IONBF,(size_t) 0) call immediately after
the fdopen(), so the i/o should be completely unbuffered.  The
fread/fwrite's are of the form fread(void *, 1, 512, FILE *).  I've
found a workaround by calling fseek(FILE *, 0, SEEK_CUR) after each
fread and fwrite, but this is obviously a hack masking a problem which
I don't understand.  Any suggestions as to what the problem might be
would be greatly appreciated.  I love a mystery, but this one's gone on
too long, and I'm out of suspects.
--
Jim Tomlinson                         (206)865-6578  \  "falling snow

Boeing Computer Services   ...uunet!bcstec!voodoo!jdt  \  - Anderson/Gabriel

 
 
 

Problem fread/fwriting sockets

Post by Jim Tomlinson (jimt » Wed, 03 Mar 1993 09:58:45



|> I'm doing a lot of client-server stuff here, using both Inet and UNIX
|> sockets.  For portability reasons, I'm fdopen()ing connected sockets,
|> and then using fread() and fwrite() to do the i/o.  I'm having a
|> problem where a client writes 512 bytes to the server, and, rather than
|> receiving that same data back (which is what should happen), it
|> receives it with the last byte it sent prepended....

Yessirreebob, RTFM'ing (or Remembering-what-you-read in TFM) sure would
keep the egg-on-face to a minimum.  Anyway, as the man page for
fopen/freopen/fdopen states:

     When a file is opened for update (when + appears as the second (or third,
     in the case of fopen or freopen) character of type) both input and output
     may be done on the resulting stream.  However, output may not be directly
     followed by input without an intervening fseek, fsetpos, or rewind.
     Similarly, input may not be directly followed by output without an
     intervening call to one of these functions, unless the input operation
     left the file positioned at end-of-file....

That fixed it right up (BTW, those of you who thought fflush() might help will
be receiving the home version of our game as a consolation prize).
--
Jim Tomlinson                         (206)865-6578  \  "falling snow

Boeing Computer Services   ...uunet!bcstec!voodoo!jdt  \  - Anderson/Gabriel

 
 
 

1. Sockets and stdio: fread and fwrite on the same socket.

I've gone through the FAQ's, and haven't found a real answer.  I'm writing a
small HTTP chat program under Linux.  The client (a web browser) sends a short
line of text, and gets a reply.  The connection to the client stays open, and
as more people send text, each still still-connected receives the new text.

I'd like to save myself a file handle and fdopen() the same socket for read and
write.  But when I do this, things don't work right: the fwrite() never sends
anything back to the client.

Then I read:
       ANSI  C requires   that  a  file  positioning  function  intervene
       between  output  and  input

Ok, so I'll put one in.  But everything I try comes back with an error that the
operation is not permitted.  I've tried all the combinations of fseek() and
fsetpos() that I can think of (to the current position, to the beginning, the
end) but (logically enough) none are supported.  

Is there any way to make this work?  Currently I'm doing this on Linux 1.2.13,
but I had the same problems with Solaris 2.5.  I've taken the approach of just
opening one FILE pointer for reading and one for writing, but it seems like
there has to be a better approach.  I could just skip the stdio, but it makes
my life so much easier to let it do the buffering.  

Alternatively, is the any way to fclose() just one of the sockets I open, so
that my program doesn't have to keep the input handle open?  The client isn't
going to send any data to the server after the first piece, yet if I try to
close the input handle both the input and output handles are closed.  Ideas?
Can I do some sort of fdreopen() call to change from read to write?

Thanks!


http://www.tripod.com

2. Setup lpsystem

3. How do you use fread and fwrite in c with unix

4. Shell Scripting Rookie

5. fread/fwrite guarantees

6. System CD

7. man pages for 'fread' & 'fwrite'

8. Linux to Winsock SLIP

9. Help: fread and fwrite not compatible between Linux and SGI

10. unexpected fwrite/fread/fopen behaviour

11. fwrite / fread impossibility

12. fwrite problem

13. Problem with fwrite()