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 5 of 5

Thread: My proxy Server

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My proxy Server

    what i wanted was
    (1) Client forward proxy
    (2) proxy forward server
    (3) Server forward back proxy
    (4) proxy forward back client


    but am getting
    Connection accepted localhost/127.0.0.1:111
    Client sending "aBcDeFgHiJkLmNoPqRsTuVwXyZ" to serveur
    Problem reading back from server: java.io.OptionalDataException



    MY CLIENT


    //The server waits for connection and starts a thread when there is a connection request from a client. The server receives a String from the client and returns it to the client in uppercase.
    import java.net.*;
    import java.io.*;

    public class Client {

    ObjectInputStream Sinput; // to read the socker
    ObjectOutputStream Soutput; // towrite on the socket
    Socket socket;

    // Constructor connection receiving a socket number
    Client(int port) {
    // we use "localhost" as host name, the server is on the same machine
    // but you can put the "real" server name or IP address
    try {
    socket = new Socket("localhost", port);
    }
    catch(Exception e) {
    System.out.println("Error connectiong to server:" + e);
    return;
    }
    System.out.println("Connection accepted " +
    socket.getInetAddress() + ":" +
    socket.getPort());


    /* Creating both Data Stream */
    try
    {
    Sinput = new ObjectInputStream(socket.getInputStream());
    Soutput = new ObjectOutputStream(socket.getOutputStream());
    }
    catch (IOException e) {
    System.out.println("Exception creating new Input/output Streams: " + e);
    return;
    }
    // now that I have my connection
    String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
    // send the string to the server
    System.out.println("Client sending \"" + test + "\" to serveur");
    try {
    Soutput.writeObject(test);
    Soutput.flush();
    }
    catch(IOException e) {
    System.out.println("Error writting to the socket: " + e);
    return;
    }
    // read back the answer from the server
    String response;
    try {
    response = (String) Sinput.readObject();
    System.out.println("Read back from server: " + response);
    }
    catch(Exception e) {
    System.out.println("Problem reading back from server: " + e);
    }

    try{
    Sinput.close();
    Soutput.close();
    }
    catch(Exception e) {}
    }


    public static void main(String[] arg) {
    new Client(111);
    }
    }




    MY PROXY


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

    public class NewProxy {
    public static void main(String[] args) throws IOException {
    try {
    String host = "your Proxy Server";
    int remoteport = 100;
    int localport = 111;
    // Print a start-up message
    System.out.println("Starting proxy for " + host + ":" + remoteport
    + " on port " + localport);
    // And start running the server
    runServer(host, remoteport, localport); // never returns
    } catch (Exception e) {
    System.err.println(e);
    }
    }

    /**
    * runs a single-threaded proxy server on
    * the specified local port. It never returns.
    * @throws ClassNotFoundException
    */
    public static void runServer(String host, int remoteport, int localport)
    throws IOException, ClassNotFoundException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    //final byte[] request = new byte[10240];
    // byte[] reply = new byte[40960];


    while (true) {
    Socket client=null, server = null;
    try {
    // Wait for a connection on the local port
    client = ss.accept();

    final ObjectOutputStream Soutput = new ObjectOutputStream(client.getOutputStream());
    Soutput.flush();
    final ObjectInputStream Sinput = new ObjectInputStream(client.getInputStream());

    // Make a connection to the real server.
    // If we cannot connect to the server, send an error to the
    // client, disconnect, and continue waiting for connections.

    try {

    server = new Socket(host,remoteport);

    System.out.println("server and proxy connected!!");
    } catch (IOException e) {
    PrintWriter out = new PrintWriter(Soutput);
    out.print("Proxy server cannot connect to " + host + ":"
    + remoteport + ":\n" + e + "\n");
    out.flush();
    client.close();
    continue;
    }

    // Get server streams.
    final ObjectInputStream SinputServer = (ObjectInputStream) server.getInputStream();
    final ObjectOutputStream SoutputServer = (ObjectOutputStream) server.getOutputStream();


    // a thread to read the client's requests and pass them
    // to the server. A separate thread for asynchronous.
    Thread t = new Thread() {
    public void run() {
    String response;
    try {
    response = (String) Sinput.readObject();
    // while(response!=null){
    SoutputServer.writeObject(response);
    SoutputServer.flush();
    //}
    } catch (IOException e) {
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    // the client closed the connection to us, so close our
    // connection to the server.
    try {
    SoutputServer.close();
    } catch (IOException e) {
    }
    }
    };

    // Start the client-to-server request thread running
    t.start();

    // Read the server's responses
    // and pass them back to the client.
    String responseS;
    try {
    responseS = (String) SinputServer.readObject();
    // while(responseS!=null){
    Soutput.writeObject(responseS);
    Soutput.flush();
    // }
    } catch (IOException e) {
    }

    // The server closed its connection to us, so we close our
    // connection to our client.
    Soutput.close();
    } catch (IOException e) {
    System.err.println(e);
    } finally {
    try {
    if (server != null)
    server.close();
    if (client != null)
    client.close();
    } catch (IOException e) {
    }
    }
    }
    }
    }




    MY SERVER


    //The server code Server.java:

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

    /**
    * the client send a String to the server the server returns it in UPPERCASE thats all
    */
    public class Server {

    // the socket used by the server
    private static ServerSocket serverSocket;
    // server constructor
    Server(String host,int remoteport) throws ClassNotFoundException, IOException {

    /* create socket server and wait for connection requests */
    ServerSocket ss = new ServerSocket(remoteport);

    //final byte[] request = new byte[10240];
    // byte[] reply = new byte[40960];


    while (true) {
    Socket server=null;
    try {
    // Wait for a connection on the local port
    server = ss.accept();

    final ObjectOutputStream Soutput = new ObjectOutputStream(server.getOutputStream());
    Soutput.flush();
    final ObjectInputStream Sinput = new ObjectInputStream(server.getInputStream());



    try {
    String str = (String) Sinput.readObject();
    str = str.toUpperCase();
    Soutput.writeObject(str);
    Soutput.flush();
    }

    catch (IOException e) {
    System.out.println("Exception reading/writing Streams: " + e);
    return;

    }
    }
    catch (IOException e) {
    System.out.println("Exception reading/writing Streams: " + e);
    return;
    }
    }
    }




    // you must "run" server to have the server run as a console application
    public static void main(String[] arg) throws ClassNotFoundException, IOException {
    // start server on port 1500
    new Server("host",100);

    }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My proxy Server

    what i wanted was
    (1) Client forward proxy
    (2) proxy forward server
    (3) Server forward back proxy
    (4) proxy forward back client


    but am getting
    Connection accepted localhost/127.0.0.1:111
    Client sending "aBcDeFgHiJkLmNoPqRsTuVwXyZ" to serveur
    Problem reading back from server: java.io.OptionalDataException



    MY CLIENT


    //The server waits for connection and starts a thread when there is a connection request from a client. The server receives a String from the client and returns it to the client in uppercase.
    import java.net.*;
    import java.io.*;

    public class Client {

    ObjectInputStream Sinput; // to read the socker
    ObjectOutputStream Soutput; // towrite on the socket
    Socket socket;

    // Constructor connection receiving a socket number
    Client(int port) {
    // we use "localhost" as host name, the server is on the same machine
    // but you can put the "real" server name or IP address
    try {
    socket = new Socket("localhost", port);
    }
    catch(Exception e) {
    System.out.println("Error connectiong to server:" + e);
    return;
    }
    System.out.println("Connection accepted " +
    socket.getInetAddress() + ":" +
    socket.getPort());


    /* Creating both Data Stream */
    try
    {
    Sinput = new ObjectInputStream(socket.getInputStream());
    Soutput = new ObjectOutputStream(socket.getOutputStream());
    }
    catch (IOException e) {
    System.out.println("Exception creating new Input/output Streams: " + e);
    return;
    }
    // now that I have my connection
    String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
    // send the string to the server
    System.out.println("Client sending \"" + test + "\" to serveur");
    try {
    Soutput.writeObject(test);
    Soutput.flush();
    }
    catch(IOException e) {
    System.out.println("Error writting to the socket: " + e);
    return;
    }
    // read back the answer from the server
    String response;
    try {
    response = (String) Sinput.readObject();
    System.out.println("Read back from server: " + response);
    }
    catch(Exception e) {
    System.out.println("Problem reading back from server: " + e);
    }

    try{
    Sinput.close();
    Soutput.close();
    }
    catch(Exception e) {}
    }


    public static void main(String[] arg) {
    new Client(111);
    }
    }




    MY PROXY


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

    public class NewProxy {
    public static void main(String[] args) throws IOException {
    try {
    String host = "your Proxy Server";
    int remoteport = 100;
    int localport = 111;
    // Print a start-up message
    System.out.println("Starting proxy for " + host + ":" + remoteport
    + " on port " + localport);
    // And start running the server
    runServer(host, remoteport, localport); // never returns
    } catch (Exception e) {
    System.err.println(e);
    }
    }

    /**
    * runs a single-threaded proxy server on
    * the specified local port. It never returns.
    * @throws ClassNotFoundException
    */
    public static void runServer(String host, int remoteport, int localport)
    throws IOException, ClassNotFoundException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    //final byte[] request = new byte[10240];
    // byte[] reply = new byte[40960];


    while (true) {
    Socket client=null, server = null;
    try {
    // Wait for a connection on the local port
    client = ss.accept();

    final ObjectOutputStream Soutput = new ObjectOutputStream(client.getOutputStream());
    Soutput.flush();
    final ObjectInputStream Sinput = new ObjectInputStream(client.getInputStream());

    // Make a connection to the real server.
    // If we cannot connect to the server, send an error to the
    // client, disconnect, and continue waiting for connections.

    try {

    server = new Socket(host,remoteport);

    System.out.println("server and proxy connected!!");
    } catch (IOException e) {
    PrintWriter out = new PrintWriter(Soutput);
    out.print("Proxy server cannot connect to " + host + ":"
    + remoteport + ":\n" + e + "\n");
    out.flush();
    client.close();
    continue;
    }

    // Get server streams.
    final ObjectInputStream SinputServer = (ObjectInputStream) server.getInputStream();
    final ObjectOutputStream SoutputServer = (ObjectOutputStream) server.getOutputStream();


    // a thread to read the client's requests and pass them
    // to the server. A separate thread for asynchronous.
    Thread t = new Thread() {
    public void run() {
    String response;
    try {
    response = (String) Sinput.readObject();
    // while(response!=null){
    SoutputServer.writeObject(response);
    SoutputServer.flush();
    //}
    } catch (IOException e) {
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    // the client closed the connection to us, so close our
    // connection to the server.
    try {
    SoutputServer.close();
    } catch (IOException e) {
    }
    }
    };

    // Start the client-to-server request thread running
    t.start();

    // Read the server's responses
    // and pass them back to the client.
    String responseS;
    try {
    responseS = (String) SinputServer.readObject();
    // while(responseS!=null){
    Soutput.writeObject(responseS);
    Soutput.flush();
    // }
    } catch (IOException e) {
    }

    // The server closed its connection to us, so we close our
    // connection to our client.
    Soutput.close();
    } catch (IOException e) {
    System.err.println(e);
    } finally {
    try {
    if (server != null)
    server.close();
    if (client != null)
    client.close();
    } catch (IOException e) {
    }
    }
    }
    }
    }




    MY SERVER


    //The server code Server.java:

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

    /**
    * the client send a String to the server the server returns it in UPPERCASE thats all
    */
    public class Server {

    // the socket used by the server
    private static ServerSocket serverSocket;
    // server constructor
    Server(String host,int remoteport) throws ClassNotFoundException, IOException {

    /* create socket server and wait for connection requests */
    ServerSocket ss = new ServerSocket(remoteport);

    //final byte[] request = new byte[10240];
    // byte[] reply = new byte[40960];


    while (true) {
    Socket server=null;
    try {
    // Wait for a connection on the local port
    server = ss.accept();

    final ObjectOutputStream Soutput = new ObjectOutputStream(server.getOutputStream());
    Soutput.flush();
    final ObjectInputStream Sinput = new ObjectInputStream(server.getInputStream());



    try {
    String str = (String) Sinput.readObject();
    str = str.toUpperCase();
    Soutput.writeObject(str);
    Soutput.flush();
    }

    catch (IOException e) {
    System.out.println("Exception reading/writing Streams: " + e);
    return;

    }
    }
    catch (IOException e) {
    System.out.println("Exception reading/writing Streams: " + e);
    return;
    }
    }
    }




    // you must "run" server to have the server run as a console application
    public static void main(String[] arg) throws ClassNotFoundException, IOException {
    // start server on port 1500
    new Server("host",100);

    }
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My proxy Server

    Is this new thread for the same problem as this:
    http://www.javaprogrammingforums.com...rver-java.html

    Why are you starting a new thread instead of continuing to work on that other one?


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My proxy Server

    No i have have told to use this one.Yes it is for the same problem but I have created another proxy

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My proxy Server

    How is the debugging going? Have you found where the problem is yet? You need to add lots of println statements to show the execution progress as the code executes.
    One technique I found that helps show problems when working with client-server code on one PC using localhost is to add a cal to Thread sleep(50) to allow the code to push data through.

    Do you have a list of all the steps the code should take as it executes that you can use to compare against what steps the code actually takes. Then compare your list to what the code does to see where the problem is.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multithreaded Proxy Server java
    By hivesh in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 2nd, 2013, 12:19 PM
  2. Searching for a Proxy sever
    By hivesh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2013, 12:01 PM
  3. creating mysql proxy
    By shadihrr in forum JDBC & Databases
    Replies: 4
    Last Post: October 4th, 2012, 09:39 AM
  4. Simple Proxy Server, Bypassing. Help
    By jrdncchr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2011, 11:24 AM
  5. [SOLVED] Proxy authentication
    By jaya in forum Java Networking
    Replies: 2
    Last Post: January 27th, 2011, 04:04 AM

Tags for this Thread