尝试发送UDP数据包时绑定异常



我正在为我编写以下代码以与一个人聊天的同行聊天应用程序。该代码适用于Localhost(127.0.0.1),但不适用于任何特定的IP地址(192.168.43.118),并抛出BindException。请帮助。

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 Sendern" + 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 Recn" + 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的文本方法中。我要去哪里?

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

如果要测试发件人,并且在不同线程上的同一Java程序中的接收器,则必须使用两个不同的套接字。

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

最新更新