Simple method to capture HTTP input

Bryan Sant bryan.sant at gmail.com
Wed Jan 18 13:36:17 MST 2006


On 1/18/06, Ross Werner <ross at agilestudios.com> wrote:
> (p.s. this is super-trivial to do in just about any modern programming
> language. It's about four lines of perl, php, and about a dozen in Java.
> The phrase "socket programming" shouldn't scare you away.)

Actually, it's 16 lines of code in Java (ignoring whitespace):

iimport java.io.*;
import java.net.*;

public class NetEcho {

  public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(8081);

    while (true) {
      byte[] data = new byte[1024];
      int actualRead = 0;
      Socket clientSocket = serverSocket.accept();
      InputStream is = clientSocket.getInputStream();

      while ((actualRead = is.read(data)) > -1)
        System.out.print(new String(data, 0, actualRead));

      clientSocket.close();
    }
  }

}

-Bryan



More information about the PLUG mailing list