简体   繁体   English

异常:java.net.BindException:无法分配请求的地址

[英]Exception : java.net.BindException: Cannot assign requested address

I want to send and receive datagram socket but I'm getting exception java.net.BindException: Cannot assign requested address .我想发送和接收数据报套接字,但出现异常java.net.BindException: Cannot assign requested address I passed the correct ipaddress of the server which I want to communicate and correct port no.我传递了我想要通信的服务器的正确 ipaddress 和正确的端口号。

try {
    SocketAddress sockaddr = new InetSocketAddress("203.100.77.54", 8000);
    DatagramSocket sock = new DatagramSocket(sockaddr);
    DatagramPacket pack = new DatagramPacket(bData, bData.length);
    sock.send(pack);
} catch (FileNotFoundException fnfe) {
    Log.e(LOG_TAG, "FileNotFoundException");
} catch (SocketException se) {
    Log.e(LOG_TAG, "SocketException");
} catch (UnknownHostException uhe) {
    Log.e(LOG_TAG, "UnknownHostException");
} catch (IOException ie) {
    Log.e(LOG_TAG, "IOException");
}

Please help me.请帮帮我。

DatagramSockets aren't created with a target address. DatagramSockets 不是用目标地址创建的。 They are created with their own local bind-address, or none, which causes a default bind when first used.它们是用自己的本地绑定地址创建的,或者没有,这会在第一次使用时导致默认绑定。 The target address is specified when constructing the DatagramPacket, or in the connect() method.目标地址在构造 DatagramPacket 时指定,或在 connect() 方法中指定。

try like this像这样尝试

 String messageStr = "Hello Android!";
 int server_port = 8000;
 DatagramSocket s = new DatagramSocket();
 InetAddress local = InetAddress.getByName("203.100.77.54");
 int msg_length = messageStr.length();
 byte[] message = messageStr.getBytes();
 DatagramPacket p = new DatagramPacket(message, msg_length, local, server_port);
 s.send(p);

Here's a more high-level answer:这是一个更高级的答案:

Direct UDP, like direct TCP, is meant for a specific address, like Bob.直接 UDP 与直接 TCP 一样,适用于特定地址,例如 Bob。 So if I'm sending the packets to Bob, then you aren't allowed to listen for them -- you can only listen for yourself.因此,如果我将数据包发送给 Bob,则不允许您监听它们——您只能监听您自己。 So if you try to open a listener for Bob, your device tells you that you aren't allowed.因此,如果您尝试为 Bob 打开侦听器,您的设备会告诉您不允许。

Unless you are using multicast UDP or something like that, you can only listen to things that are meant to be sent directly to you, hence the IP or whatever address must be that devices own address.除非您使用多播 UDP 或类似的东西,否则您只能收听直接发送给您的内容,因此 IP 或任何地址必须是该设备自己的地址。

暂无
暂无

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

相关问题 java.net.BindException:无法分配请求的地址 - java.net.BindException: Cannot assign requested address java.net.BindException:无法在Heroku上分配请求的地址 - java.net.BindException: Cannot assign requested address on Heroku 遇到 java.net.bindexception 的问题无法分配请求的地址 - Problem running into java.net.bindexception cannot assign requested address java.net.BindException:无法分配请求的地址,java.io.IOException:打开的文件太多 - java.net.BindException: Cannot assign requested address, java.io.IOException: Too many open files java.net.BindException:打开太多连接时无法分配请求的地址 - java.net.BindException: Cannot assign requested address when opening too many connections GNetLib-java.net.BindException:无法分配请求的地址:JVM_Bind - GNetLib - java.net.BindException: Cannot assign requested address: JVM_Bind 创建FlumeDStream java.net.BindException时,在Yarn错误上发生Spark流式传输:无法分配请求的地址 - Spark streaming on Yarn Error while creating FlumeDStream java.net.BindException: Cannot assign requested address tomcat 错误:java.net.BindException:无法分配请求的地址(绑定失败) - tomcat error: java.net.BindException: Cannot assign requested address (Bind failed) java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址) - java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address) 在jdeveloper java.net.BindException中运行weblogic:无法分配请求的地址:JVM_Bind - Run weblogic in jdeveloper java.net.BindException: Cannot assign requested address: JVM_Bind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM