Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple Proxy Server, Bypassing. Help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Proxy Server, Bypassing. Help

    I'm creating a simple proxy server that is multi-threaded and will bypass all the host in the host array.
    Please help me to my code.

    I only want to use socket and streams for this:

    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class ProxyServer extends Thread{
     
        private ServerSocket ss;
        private String[] host = {"www.google.com", "www.facebook.com"};
     
        private int remotePort = 80;
        public static long timeout = 30000;
     
        public ProxyServer(ServerSocket server) {
            this.ss = server;
        }
     
        public static void main(String[] args){
            try{
     
                System.out.println("Starting the Proxy Server..");
                ServerSocket ss = new ServerSocket(50005);
                System.out.println("Proxy server started at port " + ss.getLocalPort() + ".\r\n");
                for(int i = 0; i < 5; i++){
                    ProxyServer proxy = new ProxyServer(ss);
                    proxy.start();
                }
            }catch(Exception e){
                System.out.println(e);
            }
        }
     
        public void run(){
     
            try{
                Socket clientSocket = ss.accept();
                InputStream clientIn = clientSocket.getInputStream();
                OutputStream clientOut = new BufferedOutputStream(clientSocket.getOutputStream());
                for(int i = 0; i < host.length; i++){
                    try{
                        Socket serverSocket = new Socket(host[i], remotePort);
                        System.out.println("Open connection to: " + serverSocket + "(timeout= " + timeout + " ms)\r\n");
                        InputStream serverIn = serverSocket.getInputStream();
                        OutputStream serverOut = new BufferedOutputStream(serverSocket.getOutputStream());
                        OpenConnection oc = new OpenConnection(clientIn, serverIn, clientOut, serverOut, clientSocket, serverSocket);
                        oc.run();
                    }catch(Exception e){
                        System.out.println(e);
                    }       
                }   
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }

    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class OpenConnection extends Thread{
     
        InputStream clientIn;
        InputStream serverIn;
        OutputStream clientOut;
        OutputStream serverOut;
        Socket clientSocket;
        Socket serverSocket;
        int timeout = 30000;
     
        public OpenConnection(InputStream clientIn, InputStream serverIn, OutputStream clientOut, OutputStream serverOut, Socket c, Socket s) {
            this.clientIn = clientIn;
            this.serverIn = serverIn;
            this.clientOut = clientOut;
            this.serverOut = serverOut;
            this.clientSocket = c;
            this.serverSocket = s;
        }
     
        public void run(){
            int r0 = -1, r1 = -1, ch = -1, i = -1;
            long time0 = new Date().getTime();
            long time1 = new Date().getTime();
            try{
                while(r0 != 0 || r1 != 0 || (time1 - time0) <= timeout) {
                    while((r0 = clientIn.available()) > 0) {
                        ch = clientIn.read();
                            if(ch != -1) {
                                serverOut.write(ch);
                                System.out.print((char)ch);
                            }
                        time0 = new Date().getTime();
                        serverOut.flush();
                    }
                    while((r1 = serverIn.available()) > 0) {
                        try{
                            ch = serverIn.read();
                            if(ch != -1) {
                                clientOut.write(ch);
                            }
                            time0 = new Date().getTime();
                            clientOut.flush();
                        }catch(Exception e){
                        }   
                    }
                }
            }catch(Exception e){
                System.out.println(e);
            }finally{
                try{
                clientIn.close();
                clientOut.close();
                serverIn.close();
                serverOut.close();
                clientSocket.close();
                serverSocket.close();
                }catch(Exception e){
                    System.out.println(e);
                }
            }
        } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple Proxy Server, Bypassing. Help

    Cross posted at: Proxy Server Bypass Help

  3. The Following User Says Thank You to Norm For This Useful Post:

    Tjstretch (December 4th, 2011)

Similar Threads

  1. Simple client-server chat program
    By Saloni Patil in forum Java Networking
    Replies: 3
    Last Post: October 22nd, 2011, 09:29 AM
  2. Questions on simple client-server app
    By worwhite in forum Threads
    Replies: 24
    Last Post: July 30th, 2011, 08:19 PM
  3. [SOLVED] Simple server client echo issues
    By Kakashi in forum Java Networking
    Replies: 4
    Last Post: March 3rd, 2011, 10:54 AM
  4. simple ftp server and ftp client
    By simontkk2005 in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2011, 10:29 AM
  5. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM