0

I will post my code below, a little background. I am trying to connect to a gameserver on port 9339. my local port changes each time. The aim is to pass the packets through the proxy and display the info in the command line.

The client connects to the remote host using bluestacks which is running the game.

Code:

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

public class proxy {
  public static void main(String[] args) throws IOException {
    try {
      String host = "gamea.clashofclans.com";
      int remoteport = 9339;
      ServerSocket ss = new ServerSocket(0);
      int localport = ss.getLocalPort();
      ss.setReuseAddress(true);
      // 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,ss); // never returns
      System.out.println("Started proxy!");
    } catch (Exception e) {
      System.err.println(e);
    }
  }

  /**
   * runs a single-threaded proxy server on
   * the specified local port. It never returns.
   */
  public static void runServer(String host, int remoteport, int localport, ServerSocket ss)
      throws IOException {

    final byte[] request = new byte[2048];
    byte[] reply = new byte[4096];

    while (true) {
      Socket client = null, server = null;
      try {
        // Wait for a connection on the local port
        client = ss.accept();
        System.out.println("Client Accepted!");

        final InputStream streamFromClient = client.getInputStream();
        final OutputStream streamToClient = client.getOutputStream();

        // 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("Client connected to server.");
        } catch (IOException e) {
          PrintWriter out = new PrintWriter(streamToClient);
          out.print("Proxy server cannot connect to " + host + ":"
              + remoteport + ":\n" + e + "\n");
          out.flush();
          client.close();
          System.out.println("Client disconnected");
          continue;
        }

        // Get server streams.
        final InputStream streamFromServer = server.getInputStream();
        final OutputStream streamToServer = 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() {
            int bytesRead;
            try {
              while ((bytesRead = streamFromClient.read(request)) != -1) {
                streamToServer.write(request, 0, bytesRead);
                streamToServer.flush();
              }
            } catch (IOException e) {
            }

            // the client closed the connection to us, so close our
            // connection to the server.
            try {
              streamToServer.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.
        int bytesRead;
        try {
          while ((bytesRead = streamFromServer.read(reply)) != -1) {
            streamToClient.write(reply, 0, bytesRead);
            streamToClient.flush();
          }
        } catch (IOException e) {
        }

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

Basically the last thing that is printed out is "Starting proxy for gamea.clashofclans.com:9339 on port (whatever it chose).

Hopefully someone can help me.

5
  • I don't see any condition when your loop fails or exit in your runserver method. So I am guessing your loop runs forever. that could be the issue. Also you have lots of hungry exception, where the whole trace is eaten by your code. Please print them at least and see if you encounter any exceptions. Commented Dec 26, 2014 at 16:59
  • @Jimmy Not sure what you mean.. Commented Dec 26, 2014 at 17:02
  • have you implemented anything when while (true) { ...} exits ? you might want to add break; somewhere , probably after server closes.also dont leave catch (IOException e) { }. print it. Commented Dec 26, 2014 at 17:06
  • Ok, i've added some error handling and added a break when server closes but still no luck.. still only the same 1 output line. Do I have to configure the game to go through the proxy or something? Commented Dec 26, 2014 at 17:16
  • you can reference the answer below. Hope it helps Commented Dec 26, 2014 at 17:35

2 Answers 2

2

I have this problem too, I don`t have enough time to correct this but i think using thread is that is why all mistake.

check your proxy for working on browser setting( May be proxy had problem)

If not, I suggest to don`t use thread. maybe mutual exclusion occurs.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is correct.It is working fine so you don't need any fix. What is happening is , your serverSocket in your proxy class is waiting for client to connect. that's why it is not going forward. What you need to do is, create a client and connect to it.

follow the step :

  1. run your proxy.
  2. then run your client

for the client, you can use this code,

    public static void main(String[] args) throws IOException {
    try {
        int remoteport = 9339;
        String host="127.0.0.1";
        makeConnection(host, remoteport);
        System.out.println("connection successful!");
    } catch (Exception e) {
        System.err.println(e);
    }
}

public static void makeConnection(String host, int remoteport)  throws IOException {

    while (true) {
        Socket client = null;
        try {
            client = new Socket(host, remoteport);
            System.out.println("Client connected to server.");
            break;
        } catch (IOException e) {
            System.err.println(e);
        } finally {
                if (client != null)
                    client.close();
                if (client != null)
                    client.close();
        }
    }
}

11 Comments

Tried this, now I get an infinite loop (even when the game isn't running) of "Client connected to server"
this should be separate question. please post another question for this topic with proper title. Your connection problem is fixed.
You have lots of logical problem in your code. If this answer helped you , mark it as solved. and tag me in your next question so that I can provide you the solution of the looping and proxy not getting started problem. You can see request coming in your server from my client already in your log.
He isn't 'writing a client only'. He is writing a proxy. He needs the ServerSocket.
@EJP that's what I thought, should I put it back to how it was and wait for another answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.