无法使用 ServerSocket.socketBind 分配请求的地址 [英] Cannot assign requested address using ServerSocket.socketBind

查看:33
本文介绍了无法使用 ServerSocket.socketBind 分配请求的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试设置套接字服务器时,收到一条错误消息:

When I'm trying to set up a socket server, I've got an error message:

Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at java.net.ServerSocket.<init>(ServerSocket.java:106)
    at socketyserver.SocketyServer.main(SocketyServer.java:12)
Java Result: 1

整个代码尽可能简单:

public static void main(String[] args) throws UnknownHostException, IOException
{
    ServerSocket serverSocket;
    serverSocket = new ServerSocket(9999);
}

我 100% 确定我的端口已转发,Windows 防火墙已关闭.没有什么会阻止端口 9999.还有什么地方会出错?

I'm 100% sure that my ports are forwarded, Windows Firewall is off. Nothing blocks port 9999. What else can go wrong?

推荐答案

正如其他人所指出的,它很可能与另一个使用端口 9999 的进程有关.在 Windows 上,运行命令:

As other people have pointed out, it is most likely related to another process using port 9999. On Windows, run the command:

netstat -a -n | grep "LIST"

它应该列出任何占用端口的东西.当然,您随后必须在任务管理器中手动终止这些程序.如果这仍然不起作用,请替换该行:

And it should list anything there that's hogging the port. Of course you'll then have to go and manually kill those programs in Task Manager. If this still doesn't work, replace the line:

serverSocket = new ServerSocket(9999);

与:

InetAddress locIP = InetAddress.getByName("192.168.1.20");
serverSocket = new ServerSocket(9999, 0, locIP);

当然把192.168.1.20替换成你的实际IP地址,或者使用127.0.0.1.

Of course replace 192.168.1.20 with your actual IP address, or use 127.0.0.1.

这篇关于无法使用 ServerSocket.socketBind 分配请求的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆