gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:21:31 24 4
gpt4 key购买 nike

我有 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() 方法。

关于java.net.BindException : Address already in use: JVM_Bind BUT NO port using it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27628563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com