繁体   English   中英

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

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

我有Java代理,下面附有完整的代码。

启动时,我不断收到以下错误。

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

我已经使用tcpview并使用netstat完成了搜索,但是没有使用端口9339 ...

我的代码如下:

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

如您所见,远程端口是9339,本地端口是自动分配的。

我不知道我要去哪里错了,我将不胜感激。

新问题-原始已修复。

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

该代码将运行,并且除了起始代理文本外,什么都不会打印。

您的程序正在创建2个试图在同一端口上侦听的套接字:

 ServerSocket s = new ServerSocket(0);

在您的main()方法中,然后

ServerSocket ss = new ServerSocket(localport);

在runServer()方法中,您将其传递给与main()中创建的套接字相同的端口。 仅使用这些套接字之一,例如,将在main()中创建的ServerSocket传递给runServer()方法。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM