[OT?] Tying a socket to stdin/stdout w/dup2() ?
Hi all, Sorry if this question is a little off topic since it's not strongly FreeBSD-specific. I've got a C program that opens a TCP/IP socket and makes a client connection. What I'd like to do is to 'tie' the socket to this program's standard I/O, so that anything that is fed into this program's stdin, is immediately sent to the socket, and anything that appears on the socket, is immediately sent out this program's stdout. (The end effect being a sort of pathologically simple version of what telnet, (or inetd or ucspi-tcp) does.) Trying to figure this out using Google, the best lead I got was to try something like this: fclose(stdin); fclose(stdout); dup2(sd, 0); dup2(sd, 1); But it doesn't seem to do much good. (At best, I think this means the program can communicate with the socket as if it were stdio - but from the program user's point of view, stdio disappears completely.) I'd rather not resort to getting my hands dirty with select(2), but I guess if there's really no other way, I'll have to. TIA, -Chris ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: [OT?] Tying a socket to stdin/stdout w/dup2() ?
On Nov 7, 2003, at 12:19 PM, Chris Pressey wrote: I've got a C program that opens a TCP/IP socket and makes a client connection. What I'd like to do is to 'tie' the socket to this program's standard I/O, so that anything that is fed into this program's stdin, is immediately sent to the socket, and anything that appears on the socket, is immediately sent out this program's stdout. (The end effect being a sort of pathologically simple version of what telnet, (or inetd or ucspi-tcp) does.) Take a look at netcat, from /usr/ports/net/netcat. -- -Chuck ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: [OT?] Tying a socket to stdin/stdout w/dup2() ?
On Fri, 7 Nov 2003 12:35:55 -0500 Charles Swiger [EMAIL PROTECTED] wrote: On Nov 7, 2003, at 12:19 PM, Chris Pressey wrote: I've got a C program that opens a TCP/IP socket and makes a client connection. What I'd like to do is to 'tie' the socket to this program's standard I/O, so that anything that is fed into this program's stdin, is immediately sent to the socket, and anything that appears on the socket, is immediately sent out this program's stdout. (The end effect being a sort of pathologically simple version of what telnet,(or inetd or ucspi-tcp) does.) Take a look at netcat, from /usr/ports/net/netcat. Ahh great, now I'm blind! :) Seriously - it seems to confirm that I was confused. Looks like you can either: a) dup the socket to stdin/out, then exec a program (a la inetd); or b) keep stdin/out and the socket, and multiplex between them (telnet). Since I want my program to act as a pipe source/sink, rather than exec-ing something else, I'll have to go with b), which means I'll have to face the select(2) music. Anyway, thanks for the pointer... -Chris ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]