Recently, I tried to run netcat under Windows and I failed to compile it. So I decided to write a simple equivalent in Java, which works under all platforms.
NetCat is a very simple and useful tool, which allows to see contents of TCP/IP requests, like HTTP etc., both server and client side. With NetCat it is possible to either set up TCP/IP listener on a port and receive data or send TCP/IP request to remote server.
The code and executable for Java version of NetCat is available on GitHub:
I used Jakarta Commons CLI to handle commandline parameters. The code is as simple as that:
In order to handle stream I/O I implemented a simple StreamTransferer class like this:
The only issue I had was that whenever I closed Java I/O stream using OutputStream.close(), it was closing the whole socket. So I couldn’t receive any response back from server. So instead of doing that I had to use Socket shutdownOutput method, like in the code below.
It worked perfectly then.
So it is possible to send HTTP GET requests to google using this tool. In order to do that, you need to connect to google.com and type GET and enter. CTRL+Z and enter is for closing input stream under Windows. CTRL+D is for Linux. The example looks like below: