Thursday, July 12, 2007

Getting the program name that is listening on a specified port

Sometimes we need to find out which program is listening on a specified port.

In Linux, we can use:
netstat -natlp

-n, --numeric don't resolve names
-a, --all, --listening display all sockets (default: connected)
< Socket >={-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
-l, --listening display listening server sockets
-p, --programs display PID/Program name for sockets

In windows, we can use fport which can be downloaded in the internet.

Its official website: http://www.foundstone.com/us/resources-free-tools.asp

Friday, July 6, 2007

Java: a simple HTTP server sample

In JDK 6, it is very easy to create a HTTP server. Here is a sample in SDK 6.

===================================================
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.spi.HttpServerProvider;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;


public class SimpleHttpServer {
public static void main(String[] args) throws Exception{
HttpServerProvider httpServerProvider = HttpServerProvider.provider();
InetSocketAddress addr = new InetSocketAddress(8888);
HttpServer httpServer = (HttpServer)httpServerProvider.createHttpServer(addr, 1);
httpServer.createContext("/mydemo/", new MyHttpHandler());
httpServer.setExecutor(null);
httpServer.start();
System.out.println("started");
}

static class MyHttpHandler implements HttpHandler{
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Hello world!";
httpExchange.sendResponseHeaders(200, response.length());
OutputStream out = httpExchange.getResponseBody();
out.write(response.getBytes());
out.close();
}
}
}
===================================================

Website visited statistics

For counting the visited amount, first of all, add one configuration in config/environment.rb
RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", "daily")

1. get the request statistics
cat production.log.20070702|grep "200 OK"|wc -l

2. get the URL visited statistics
cat production.log.20070702 | grep "200 OK" | awk '{print $17}'|sort|uniq -c | sort -r -n > stat.log

Wednesday, July 4, 2007

Java: Send a Http Request

Here is the sample code for sending a http request using Java Library

===================================================
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;

public class HttpTest extends Thread{
public static void main(String[] args) {
try {
URL u = new URL("http://192.168.1.10:80/http/incoming");
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("Host" , "1.1.1.1");
uc.setRequestProperty("Accept", "*/*");
uc.setRequestProperty("Content-Type", "application/octet-stream");
uc.setDoOutput(true);
uc.connect();
OutputStream os = uc.getOutputStream();
os.write("123456".getBytes());
uc.disconnect();
System.out.println("Response code: " + uc.getResponseCode());
String key = null;
for (int i=1; ((key = uc.getHeaderFieldKey(i))!=null); i++) {
System.out.println(key + ": " + uc.getHeaderField(key));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

===================================================

In my opinion, if it was a very simple application such as only sending a simple message via HTTP protocol, it was better to implement by yourself because it is more flexible and more efficient. I did it in my current project. However, there is a risk for implement by yourself. It's that Java Sock library is not thread safe. Be careful.

Here is a HTTP message sample:
==============================================
POST /http/incoming HTTP/1.1
Accept: */*
Content-Length: 6
Host: 192.168.1.10:80
Content-Type: application/octet-stream
Date: 2007-07-04 12:12:12

123456
==============================================