簡體   English   中英

嘗試發送 UDP 數據包時綁定異常

[英]Bind Exception when trying to send UDP packet

我正在制作一個點對點聊天應用程序,為此我編寫了以下代碼用於與一個人聊天。 此代碼適用於 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 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 的文本方法中。 我哪里錯了?

如果要讀取,請不要綁定遠程地址。 只需創建一個本地 UDP 套接字(它將是一個服務器套接字),然后讀取/寫入其緩沖區。 您只能綁定到本地網絡接口,並且必須在此之前進行初始化。 因此,您無法綁定到未使用的 Wifi 或未插入的以太網適配器。

如果要在不同線程上測試同一個 java 程序中的發送者和接收者,則必須使用兩個不同的套接字。

提示:使用InetAddress.getByName(IPv4)進行字符串 IP 輸入,您不需要字節數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM