Monday, June 07, 2004

AJP13 CPing

AJP13 CPing



package edu.nitin.apache.ajp;

import java.net.Socket;
import java.net.SocketException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
*
* @author Nitin Verma
*/
public class TCPClient {

/** Creates a new instance of TCPClient */
public TCPClient(String host, int port) {

Socket socket = null;
try {
if(host == null) {
host = "127.0.0.1";
}
if(port == 0) {
port = 8080;
}
System.out.println("host:port = " + host + ":" + port);
socket = new Socket(host, port);
final InputStream in = socket.getInputStream();
final DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Thread t1 = new Thread(getRunnableReader(in));
t1.start();
byte [] magic = {0x12, 0x34};
byte [] data = {0x0A};
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(data);
int dataLenght = byteArrayOutputStream.size();
int MAX_LENGTH = 8 * 1024 - 2 /*data length*/ - magic.length;
if( dataLenght >= MAX_LENGTH ) {
System.err.println("> max length [" + MAX_LENGTH + "]");
return;
}

// Magic bytes
out.write(magic);
out.flush();
// Data lenght

out.writeShort(dataLenght);
out.flush();
// Data
byteArrayOutputStream.writeTo(out);
out.flush();
t1.join();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (Throwable th) {
th.printStackTrace();
}
finally {
System.out.println();
try {
socket.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

private Runnable getRunnableReader(final InputStream in) {
Runnable runnable = new Runnable() {
public void run() {
try{
int rByte = 0;
while((rByte = in.read()) != -1){
System.out.print(Integer.toHexString(rByte));
System.out.print(" ");
}
}
catch(SocketException se){
se.printStackTrace();
return;
}
catch(Throwable th){
th.printStackTrace();
return;
}
}
};
return runnable;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = null;
int port = 0;
if(args.length >= 2) {
host = args[0];
port = Integer.parseInt(args[1]);
}
new TCPClient(host, port);
}


}

Thursday, May 13, 2004

Software Physics

Study of nature of software systems

As we know physics is study of nature. On the same lines I can say Software Physics is study of nature of software systems.

Welome to Nitin's Net

Yet to think what to start with! What about core dump ? ;-)