socketBind分配请求的地址

socketBind分配请求的地址

本文介绍了无法使用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"

它应该列出任何阻碍端口的内容.当然,您必须去手动在Task Manager中杀死那些程序.如果仍然无法解决问题,请替换以下行:

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分配请求的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:50