How to connect to an other computer at a given port, t.ex. to read a HTML-page

Homepage
Sourcecodes
Java call of c function
Connect to an other computer
Java compatible GZIP in C++
little Networksniffer in Java

Protocol, what is going on between two TCP programs, t.ex. you browser and a webserver:


Sample call:
java -jar sniff.jar inPort=8080 targetPort=80 targetServer=www.google.de logIn logOut
to listen, which data run between your browser and Google. After starting the program run your browser with the address 'http://localhost:8080/'

package de.felfri;

/**
 * Programm to protocol data that is send between two programs by TCP connection
 * t.ex. to see, what is going on between a Browser and a WebServer
 * 
 * Start the program t.ex. with
 * 
 * Sniff inPort=80 targetPort=80 targetServer=www.google.de logIn logOut
 * 
 * and run your Browser with   http://localhost/
 * 
 * to see, which cookies google is sending to your Browser
 * 
 * This program is free to use, manipulate or what ever you
 * what to do with it.
 * 
 * @author olaf stueben
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/** Class to sniff between two TCP programs */
public class Sniff
{
   /** Port to listen on, on the local machine */
   private int inputPort = 81;
   /** Port to forward the data to */
   private int targetPort = 80;
   /** Server to forward the data to */
   private String targetServer = "localhost";
   /** If set, protocol the data send to target on stdout */
   private boolean protocolRead = false;
   /** If set, protocol the data received from target on stdout */
   private boolean protocolWrite = false;
   
   /**
    * @param args, recognized (caseinsensitive):
    * inPort, targetPort, targetServer, logIn, logOut
    */
   public static void main(String[] args)
   {
      new Sniff().run(args);
   }

   /**
    * @param args, recognized (caseinsensitive):
    * inPort, targetPort, targetServer, logIn, logOut
    */
   private void run(String[] args) 
   {
      for (int i = 0; i < args.length; ++i)
      {
         args[i] = args[i].toLowerCase();
         
         if (args[i].startsWith("inport="))
            inputPort = Integer.parseInt(args[i].substring(7));
         else if (args[i].startsWith("targetport="))
            targetPort = Integer.parseInt(args[i].substring(11));
         else if (args[i].startsWith("targetserver="))
            targetServer = args[i].substring(13);
         else if (args[i].equals("login"))
            protocolRead = true;
         else if (args[i].equals("logout"))
            protocolWrite = true;
         else
         {
            System.out.println("Usage: Sniff [ inPort=1234 ] [ targetPort=3456] [ targetServer=SERVERNAME ] [logIn] [logOut]");
            System.out.println("       Log the Data that is send between two partners, t.ex. Browser to WebServer");
            System.out.println("       with inPort = Port to listen, default = 81");
            System.out.println("       with targetPort = Port of the server");
            System.out.println("       with targetServer = Name of the server");
            System.out.println("       logIn: If set, log the data send from In to Target");
            System.out.println("       logOut: If set, log the data send from Target to In");
            return;
         }
      }

      System.out.println("Mapping localhost:" + inputPort + 
                         " to " + targetServer + ":" + targetPort);
      try
      {
         // The port to read on
         ServerSocket socket = new ServerSocket(inputPort);
         while (true)
         {
            // Wait for a connection
            Socket sourceSocket = socket.accept();
            // Open the target
            Socket targetSocket = null;
            // Open a target on the server
            try
            {
               targetSocket = new Socket(targetServer, targetPort);
            }
            catch (UnknownHostException uhe)
            {
               System.err.println("Unknown Host " + targetServer);
               System.exit(-1);            
            }
            // Create two threads that transport the data from one socket
            // to the other an protocol it (if wished)
            new Worker(sourceSocket, targetSocket, protocolRead).start();
            new Worker(targetSocket, sourceSocket, protocolWrite).start();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
   
   /** Thread to transport data from one socket to the other and 
    * write the data to stdout if wished 
    */
   private class Worker extends Thread
   {
      /** Socket to read from */
      Socket socketIn;
      /** Socket to write to */
      Socket socketOut;
      /** Log the data on stdout if true */
      boolean log;
      
      /** Create a new thread
       * 
       * @param socketIn Socket to read from by getInputStream
       * @param socketOut Socket to write to by getOutputStream
       * @param log if true, log the send data on stdout
       */
      protected Worker(Socket socketIn, Socket socketOut, boolean log)
      {
         this.socketIn = socketIn;
         this.socketOut = socketOut;
         this.log = log;
      }
      
      /** run the thread, executed asynchron by Thread.start() */
      public void run()
      {
         try
         {
            InputStream in = socketIn.getInputStream();
            OutputStream out = socketOut.getOutputStream();
            
            // While input not closed
            while (true)
            {
               // Read data
               byte[] data = new byte[1000];
               int got = in.read(data);
               
               if (got == -1)
               {
                  // EOF
                  in.close();
                  out.close();
                  socketIn.close();
                  socketOut.close();
                  break;
               }
               // Write the data to Output
               out.write(data, 0, got);
               if (log)
                  System.out.print(new String(data, 0, got));
            }
         }
         catch (IOException ioe)
         {
         }
      }
   }
}
For you, here the jar-file, also including the source code for download: sniff.jar.
If you find errors in this code or have comments on it, please tell me at source @ stueben.com