【问题标题】:Bind Exception when trying to send UDP packet尝试发送 UDP 数据包时出现绑定异常
【发布时间】:2019-03-01 09:21:51
【问题描述】:

我正在制作一个点对点聊天应用程序,为此我编写了以下代码用于与一个人聊天。此代码适用于本地主机(127.0.0.1),但不适用于任何特定的 IP 地址(192.168.43.118)并引发绑定异常。请帮忙。

import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class communicate {
    public static void main(String args[]) throws Exception {
        communicate ob = new communicate();
        String ip = "192.168.43.118";
        ob.text(ip);
    }
    public void text(String friend_ip) throws Exception{
        System.out.println("Press 'Exit' to exit the chat");
        int send_port = 40002;
        int receive_port = 40002;
        //InetAddress inetaddr = InetAddress.getByName(friend_ip);
        byte[] ipAddr = new byte[] { (byte)192, (byte)168, (byte)43, (byte)118 };
        System.out.println(ipAddr.length);
        InetAddress inetaddr = InetAddress.getByAddress(ipAddr);
        System.out.println("B");
        DatagramSocket serverSocket = new DatagramSocket(receive_port, inetaddr);
        System.out.println("YO");
        Runnable send = new SendMsg(send_port, friend_ip, serverSocket);
        Runnable receive = new GetMsg(friend_ip, receive_port, serverSocket);
        Thread t1 = new Thread(send);
        Thread t2 = new Thread(receive);
        t1.start();
        t2.start();
    }

    class SendMsg implements Runnable {
        private DatagramSocket senderSocket;
        private int send_port;
        private String sendto_ip;
        SendMsg(int port, String friend_ip, DatagramSocket ds)throws Exception {
            senderSocket = new DatagramSocket(64432);
            send_port = port;
            sendto_ip = friend_ip;
        }
        public void run(){
            try {
                while(true) {
                String sendString;
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                sendString = br.readLine();
                byte[] sendData = sendString.getBytes();
                byte[] ipAddr = new byte[] { (byte)192, (byte)168, (byte)43, (byte)118 };
                InetAddress inetAddress = InetAddress.getByAddress(ipAddr);
                DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, inetAddress, send_port);
                senderSocket.send(sendPacket);
                System.out.println("Message Sent");
                }
            }
            catch(Exception e) {
                System.out.println("Exc at Sender\n" + e);
            }
            finally {
                if(senderSocket != null) senderSocket.close();
            }
        }

    }

    class GetMsg implements Runnable{
        private DatagramSocket serverSocket;
        private String friend_ip;
        private int receive_port;

        GetMsg(String ip, int port, DatagramSocket ds) throws Exception{
            friend_ip = ip;
            receive_port = port;
            serverSocket = ds;
        }
        public void run(){
            try {
                while(true) {
                byte[] receiveData = new byte[10000];
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                String message = new String (receivePacket.getData(), 0, receivePacket.getLength());
                System.out.println(friend_ip + ": " + message);
                }
            }
            catch(Exception e) {
                System.out.println("Exc at Rec\n" + e);
            }
            finally {
                if(serverSocket != null) serverSocket.close();
            }
        }
    }
}


当我在终端上运行它时,会显示以下输出

Press 'Exit' to exit the chat
4
B
Exception in thread "main" java.net.BindException: Cannot assign requested address (Bind failed)
        at java.base/java.net.PlainDatagramSocketImpl.bind0(Native Method)
        at java.base/java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:131)
        at java.base/java.net.DatagramSocket.bind(DatagramSocket.java:394)
        at java.base/java.net.DatagramSocket.<init>(DatagramSocket.java:244)
        at java.base/java.net.DatagramSocket.<init>(DatagramSocket.java:301)
        at communicate.text(communicate.java:21)
        at communicate.main(communicate.java:10)

由于未打印“YO”,因此错误似乎在我尝试创建 DatagramSocket 的文本方法中。我哪里错了?

【问题讨论】:

  • 您正在尝试绑定到非本地 IP 地址。为什么你使用三个套接字,而实际上只使用其中两个仍然是个谜。

标签: java sockets exception


【解决方案1】:

如果要读取,不要绑定远程地址。只需创建一个本地 UDP 套接字(它将是一个服务器套接字),然后从其缓冲区读取/写入。您只能绑定到本地网络接口,并且必须先初始化这些接口。因此,您无法绑定到未使用的 Wifi 或未插入的以太网适配器。

如果你想在同一个java程序的不同线程上测试一个发送者和一个接收者,你必须使用两个不同的socket。

提示:字符串IP输入使用InetAddress.getByName(IPv4),不需要字节数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    相关资源
    最近更新 更多