我一直遇到这个错误: “新 ServerSocket 上的异常:java.net.BindException:无法分配请求的地址:JVM_Bind”。 我尝试使用 netstat 来确保端口(1500)上没有任何东西正在运行。有什么建议吗?
package server;
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private ServerSocket serverSocket;
Server(int Port)
{
/* create socket server and wait for connection requests */
try
{
serverSocket = new ServerSocket(1500, 0, InetAddress.getByName("69.61.210.196"));
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
while(true)
{
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");
}
}
catch (IOException e)
{
System.out.println("Exception on new ServerSocket: " + e);
}
}
public static void main(String[] args)
{
new Server(1500);
}
}
最佳答案
听起来您尝试绑定(bind)的IP地址是您网络的外部IP地址,而不是您的机器IP。
建议尝试绑定(bind)到 127.0.0.1
或 0.0.0.0
。
在 Windows ipconfig
和 Linux ifconfig
上通过命令行获取计算机的 IP 使用情况。
关于java.net.BindException :JVM Bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886765/