Skip to content

CTcpFwd – cross-platform stdin/out to socket forwarding class


A few weeks ago, I had the pleasure to take part in a  local 24-hour long, programming marathon (greets to my team: Pawel and Wojtek!). Due to the nature of the competition, I was obliged to create a simple class, making it possible to redirect sockets to standard i/o (stdin / stdout), which would greatly facilitate the communication process with the contest server. Because of the fact that we were going to work on different system platforms – both Microsoft Windows and GNU/Linux, the class had to be as cross-platform compatible as it was only possible. And so the CTcpFwd class, presented today, came into existence.

After the event was over, I decided to standardize the code and apply some minor fixes – currently, it is supposed to be a fully functional module. What should be kept in mind, though, is the fact that the code is released as a Proof of Concept project – its main purpose is to present the exact way of how the stdin / stdout file descriptors can be tampered with, on Windows – as it turns out, it is not as simple as one might assume.

This tiny class lets the programmer create and close multiple named, remote connections (Connect and Disconnect methods), as well as switch the current stream associated with stdin/stdout (SwitchStdin and SwitchStdout routines), using the names declared previously. By default, the class turns off both the Nagle’s buffering algorithm for the sockets (TCP_NODELAY flag) and the standard i/o buffering. Some exemplary usage of CTcpFwd follows:

CTcpFwd Sock("Google","",80);

puts("Sending a request...");

printf("GET HTTP/1.1\n"


The above code connects to the host on the HTTP port, sends a simple request and prints the server response on the screen – everything is happening through standard i/o.

A complete package, containing the class source code (written in C++) can be downloaded from here (6kB).

As I’ll have more time, I’ll try to post some wider explanation of the msvcrt-specific mechanisms utilized in the code, soon. If any problems with either the compilation or class performance are encountered, please contact me directly ;>

Comments obviously always welcome ;>

{ 3 } Comments

  1. Scout | 22-Apr-10 at 02:26:10 | Permalink

    Good work j00ru!

  2. Gynvael Coldwind | 23-Apr-10 at 02:52:20 | Permalink

    Using plain old printf/scanf for network communication… thats just awesome ;)
    Great work ;)

  3. przemoc | 28-Apr-10 at 12:03:29 | Permalink

    Simple idea + implementation = success. Well done, j00ru!

    It may sound like a cliche, but ideas w/o PoC are not so worthful, even if they are simple. This simplicity is important (but it doesn’t mean that code also will be simple), because overcomplicated ideas often lead to horrible half-baked code scraps or even abandoning implementation stage. Saving the world always can (and should) be postponed. For developer’s own good. :)

    +1 for portability, -1 for coding convention.
    Methods and its’ arguments names starting with upper case? Space indents? Awful. ;)

Post a Comment

Your email is never published nor shared. Required fields are marked *