0 Replies Latest reply: Feb 10, 2013 8:33 AM by 990219 RSS

    Proxy Server not working

    990219
      Am trying to make my program run such as :
      Client request Proxy
      Proxy Forward to server
      Server Respond back to Proxy
      Proxy Forward back to Client
      but am getting :

      Exception creating new Input/output Streams: java.net.SocketException: Connection reset



      CLIENT SIDE


      //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 {
           
           public static void main(String[] arg) {
                

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

           // Constructor connection receiving a socket number
           
                // 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", 6000);
                }
                catch(Exception e) {
                     System.out.println("Error connectiong to server:" + e);
                     return;
                }
                System.out.println("Connection accepted " +
                          socket.getInetAddress() + ":" +
                          socket.getPort());
                while(true){
                /* 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 = "gdfgfdgdf";
                // 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) {}
           }
           }


           }




      Server Side



      //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 ServerSocket serverSocket;
           
           public static void main(String[] arg) {
                // start server on port 1500
                //Socket socket;
                ObjectInputStream Sinput;
                ObjectOutputStream Soutput;
           // server constructor
           
                
                /* create socket server and wait for connection requests */
                try
                {
                     ServerSocket serverSocket = new ServerSocket(6500);
                     System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());

                     while(true)
                     {
                          Socket server = serverSocket.accept(); // accept connection
                          System.out.println("Proxy and server connected");
                          
                          
                          try
                          {
                               // create output first
                               Soutput = new ObjectOutputStream(server.getOutputStream());
                               Soutput.flush();
                               Sinput = new ObjectInputStream(server.getInputStream());
                          }
                          catch (IOException e) {
                               System.out.println("Exception creating new Input/output Streams: " + e);
                               return;
                          }
                          
                          // read a String (which is an object)
                          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;                    
                          }
                          // will surely not happen with a String
                          catch (ClassNotFoundException o) {                    
                          }
                          finally {
                               try {
                                    Soutput.close();
                                    Sinput.close();
                               }
                               catch (Exception e) {                         
                               }
                          }
                          
                }
                }
                catch (IOException e) {
                     System.out.println("Exception on new ServerSocket: " + e);
                }
                
                
                     
                     
                }
           }

           


      Proxy Side



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

      public class Proxy{
      //public static ServerSocket server = null;
      //public static Socket client = null;


      public static void main(String[] args) throws ClassNotFoundException
      {
      try
      {
      ServerSocket server = new ServerSocket(6000);
      // Socket clientsocket = null;

      while(true)
      {
      Socket client = server.accept();


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

                



           // Get server streams.
           Socket clientsocket=new Socket(InetAddress.getLocalHost(),6500);
      final ObjectInputStream SinputServer = (ObjectInputStream) clientsocket.getInputStream();
      final ObjectOutputStream SoutputServer = (ObjectOutputStream) clientsocket.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() {
      int response;
      try {
      response = Sinput.read();
      // while(response!=null){
      SoutputServer.writeObject(response);
      SoutputServer.flush();
      //}
      } catch (IOException e) {
      }

      // 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);
      }


      }



      }

      Edited by: 987216 on Feb 10, 2013 6:32 AM