This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Wrapping a TCP stream in a stdio FILE


I am wrapping an embedded TCP/IP stack interface in a stdio interface,
so that I may use fprintf et al. with it. The TCP/IP stack that does
not eventually boil down to file descriptor interface, so I cannot use
fdopen for this purpose. I first implemented the reading portion, and
was very happy with the results. I am now going to implement the
writing portion.

After browsing the newlib source, I see that both the reading and
writing stdio functions share the same buffer, _bf. Since the read
stream and write stream are unrelated data wise (although very related
conceptually), can they share the same FILE structure? For example,
I'd like to implement an echo server using code something like this
snippet:

	FILE* f = flisten( "tcp/23");
	for( c = getc( f); c != EOF; c = getc( f))
		putc( c, f);

I'm afraid putc will trash the read buffer though. It seems to me it
would work so long as the reading portion consumes its entire buffer
before it starts the writing portion. This isn't convenient though.
Alternatively, I'll implement it using two FILEs, though I think a
single FILE looks neater and better represents the single underlying
socket.

	FILE* fin = flisten( "tcp/23", "r");
	FILE* fout = foutgoing( fin, "w");
	for( c = getc( fin); c != EOF; c = getc( fin))
		putc( c, fout);

Please cc me in your reply. Cheers,
Shaun


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]