Saturday, May 19, 2012

NetCat in Java

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:
http://github.com/rafalrusin/netcat/downloads

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:


10 comments:

  1. Hi ,

    i want to sent command via netcat by connect method. But i dont know make this.

    example :

    connect("192.168.2.104", 6000 ,"ifconfig" ) like this.

    pls help me

    ReplyDelete
  2. Thank you for sharing such a nice and interesting blog with us. i have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle. please keep on updates. hope it might be much useful for us. keep on updating...
    Software Testing Training

    ReplyDelete
  3. I read a weblog, I hope that it doesn't sadden me as much as this one. I’m talking about, I know it was my selection to read, but I actually thought you'd have something interesting to say. Great work admin..
    SAT coaching chennai

    ReplyDelete
  4. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    python training in rajajinagar
    Python training in bangalore
    Python training in usa

    ReplyDelete
  5. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    excel advanced excel training in bangalore
    Devops Training in Chennai

    ReplyDelete
  6. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.


    AWS Training in Marathahalli | Best AWS Training in Bangalore
    Amazon Web Services Training in Chennai | No.1 AWS Training for Solution Architect in Chennai

    ReplyDelete
  7. You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.

    angularjs-Training in velachery

    angularjs Training in chennai

    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    ReplyDelete