简体   繁体   English

java.net.BindException:地址已在使用中:JVM_Bind但没有端口在使用它

[英]java.net.BindException: Address already in use: JVM_Bind BUT NO port using it

I have java proxy for which I have attached the full code below. 我有Java代理,下面附有完整的代码。

I am constantly getting the following error when starting it. 启动时,我不断收到以下错误。

java.net.BindException: Address already in use: JVM_Bind

I have used tcpview and done searches with netstat but nothing is using the port 9339... 我已经使用tcpview并使用netstat完成了搜索,但是没有使用端口9339 ...

My code is as follows: 我的代码如下:

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 s = new ServerSocket(0);
      int localport = s.getLocalPort();
      // 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.
   */
  public static void runServer(String host, int remoteport, int localport)
      throws IOException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

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

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

As you can see, the remote port is 9339 and the local port is auto assigned. 如您所见,远程端口是9339,本地端口是自动分配的。

I have no idea where I am going wrong, I would greatly appreciate some assistance. 我不知道我要去哪里错了,我将不胜感激。

New problem - Original Fixed. 新问题-原始已修复。

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
    } 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 {
    // Create a ServerSocket to listen for connections with

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

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

The code will run and all but nothing prints out except the starting proxy text. 该代码将运行,并且除了起始代理文本外,什么都不会打印。

Your program is creating 2 sockets that tries to listen on the same port: 您的程序正在创建2个试图在同一端口上侦听的套接字:

 ServerSocket s = new ServerSocket(0);

in your main() method, and then you have 在您的main()方法中,然后

ServerSocket ss = new ServerSocket(localport);

in your runServer() method, which you pass in the same port as the socket created in main(). 在runServer()方法中,您将其传递给与main()中创建的套接字相同的端口。 Use only one of these sockets, eg pass in the ServerSocket you create in main() to the runServer() method. 仅使用这些套接字之一,例如,将在main()中创建的ServerSocket传递给runServer()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.BindException:地址已在使用中:JVM_Bind - java.net.BindException: Address already in use: JVM_Bind ServerSocket端口问题(java.net.BindException:已在使用的地址:JVM_Bind) - ServerSocket port issue (java.net.BindException: Address already in use: JVM_Bind) “ java.net.BindException:已在使用的地址:JVM_Bind <null> :8080”,即使我使用netstat -ano检查了端口 - “java.net.BindException: Address already in use: JVM_Bind <null>:8080” even though i checked the port by using netstat -ano java.net.BindException:已使用的地址:JVM_Bind多客户机服务器 - java.net.BindException: Address already in use: JVM_Bind Multi Client Server java.net.BindException:已在使用的地址:JVM_Bind <null>:80 - java.net.BindException: Address already in use: JVM_Bind <null>:80 修复java.net.BindException:已使用的地址:JVM_Bind - Fixing java.net.BindException: Address already in use: JVM_Bind 如何解决“java.net.BindException:地址已在使用:JVM_Bind”错误? - How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error? Tomcat:java.net.BindException:地址已在使用:JVM_Bind - Tomcat : java.net.BindException: Address already in use: JVM_Bind 线程“ Thread-0” java.net.BindException中的异常:已经使用的地址:JVM_Bind位于 - Exception in thread “Thread-0” java.net.BindException: Address already in use: JVM_Bind at 当我在Eclipse Mars中启动Jetty时,“ java.net.BindException:地址已在使用中:JVM_Bind” - “java.net.BindException: Address already in use: JVM_Bind” when I start Jetty in eclipse mars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM