Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
netcat
$ java -jar netcat.jar google.com -p 80
Connecting to google.com port 80
GET
HTTP/1.0 302 Found
Location: http://www.google.pl/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=d3ab73f155cad978:FF=0:TM=1337454744:LM=1337454744:S=q7r3IZqRmvlnVqwn; expires=Mon, 19-May-2014 19:12:24 GMT; path=/; domain=.google.com
Set-Cookie: NID=59=j3omC2VT9csLVOvUOZdmom-YTRVeXXWUEc6FoMnUrMh_4HBFS1MStyT5tN4rzC1846-wmuXros8ZFYUPyBIP5l9kwF6z4nMtyNmXmGhByd15Zrlhwj-NxNmeuu9mrbJU; expires=Sun
, 18-Nov-2012 19:12:24 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sat, 19 May 2012 19:12:24 GMT
Server: gws
Content-Length: 218
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.pl/">here</A>.
</BODY></HTML>
^Z
public static void main(String[] args) throws Exception {
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("l", "listen", false, "listen mode");
options.addOption("p", "port", true, "port number");
CommandLine line = parser.parse(options, args);
if (line.hasOption('l')) {
if (line.hasOption('p')) {
int port = Integer.parseInt(line.getOptionValue('p'));
listen(port);
}
} else {
if (line.hasOption('p')) {
int port = Integer.parseInt(line.getOptionValue('p'));
connect(line.getArgs()[0], port);
} else {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("netcat [OPTIONS] <HOST>", options);
}
}
}
private static void transferStreams(Socket socket) throws IOException,
InterruptedException {
InputStream input1 = System.in;
OutputStream output1 = socket.getOutputStream();
InputStream input2 = socket.getInputStream();
PrintStream output2 = System.out;
Thread thread1 = new Thread(new StreamTransferer(input1, output1));
Thread thread2 = new Thread(new StreamTransferer(input2, output2));
thread1.start();
thread2.start();
thread1.join();
socket.shutdownOutput();
thread2.join();
}
public class StreamTransferer implements Runnable {
private InputStream input;
private OutputStream output;
public StreamTransferer(InputStream input, OutputStream output) {
this.input = input;
this.output = output;
}
@Override
public void run() {
try {
PrintWriter writer = new PrintWriter(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
writer.println(line);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.