Java代理套接字未连接到客户端

时间:2014-12-26 16:52:08

标签: java sockets proxy

我将在下面发布我的代码,一些背景知识。 我正在尝试连接到端口9339上的游戏服务器。我的本地端口每次都会更改。目的是通过代理传递数据包并在命令行中显示信息。

客户端使用正在运行游戏的bluestack连接到远程主机。

代码:

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) {
        }
      }
    }
  }
}

基本上打印出的最后一件事是#34;在端口上启动gamea.clashofclans.com:9339的代理(无论选择什么)。

希望有人可以帮助我。

2 个答案:

答案 0 :(得分:2)

我也有这个问题,我没有足够的时间来纠正这个问题,但我认为使用线程就是为什么所有的错误。

检查代理是否正在处理浏览器设置(可能是代理有问题)

如果没有, 我建议不要使用线程。可能会发生互斥。

答案 1 :(得分:0)

您的代码是正确的。它运行正常,因此您不需要任何修复。发生的事情是,您的代理类中的serverSocket正在等待客户端连接。这就是为什么它没有前进的原因。您需要做的是,创建一个客户端并连接到它。

按照以下步骤操作:

  1. 运行您的代理。
  2. 然后运行您的客户
  3. 对于客户端,您可以使用此代码,

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