Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 基于UDP客户端的广播到所有其他可用PC_Sockets_Exception_Tcp_Udp - Fatal编程技术网

Sockets 基于UDP客户端的广播到所有其他可用PC

Sockets 基于UDP客户端的广播到所有其他可用PC,sockets,exception,tcp,udp,Sockets,Exception,Tcp,Udp,我有一台连接到内联网的电脑。我假设同一内联网上有n台其他PC。现在,一个udp服务器在所有这些n台PC上运行。现在,我的问题是,我不知道其中包含udp服务器的PC的数量。因此,我需要广播一条消息,称为“abc”,对于该消息,每个带有udp服务器的pc将返回其自己的ip。现在,我如何将我的消息“abc”广播到网络中所有可用的n台PC。 ///SENDER SOCKET InetAddress group = null; try { group = Inet

我有一台连接到内联网的电脑。我假设同一内联网上有n台其他PC。现在,一个udp服务器在所有这些n台PC上运行。现在,我的问题是,我不知道其中包含udp服务器的PC的数量。因此,我需要广播一条消息,称为“abc”,对于该消息,每个带有udp服务器的pc将返回其自己的ip。现在,我如何将我的消息“abc”广播到网络中所有可用的n台PC。

    ///SENDER SOCKET
     InetAddress group = null;
    try {
        group = InetAddress.getByName("192.168.49.255");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    int port =1212;
    DatagramSocket socket = null;
    try {
        socket = new DatagramSocket(port);
    } catch (SocketException e) {
        e.printStackTrace();

    }
    try {
        socket.setBroadcast(true);
    } catch (SocketException e) {
        e.printStackTrace();
    }


    /////RECEIVE SOCKET
    DatagramSocket rsocket = null;
    try {
        rsocket = new DatagramSocket(1513);
    } catch (SocketException e) {
        e.printStackTrace();
    }
    try {
        rsocket.setSoTimeout(2000);
    } catch (SocketException e) {
        e.printStackTrace();
    }
现在你的发送和接收

String message_to_send = "STRING_TO_SEND";
        byte[] ultimo = message_to_send.getBytes();
        DatagramPacket ultimopaquete = new DatagramPacket(ultimo, ultimo.length, group, port);
           try {
               socket.send(ultimopaquete);
           } catch (IOException e) {
               e.printStackTrace();
           }

        /*AFTER DONE WAIT FOR SERVERS RESPONSE*/


        boolean ack = false;
        int countertoend=0;
        boolean cancelcheck = false;
        while (!ack) {

            byte[] recibir = new byte[5]; ////expected byte size
            DatagramPacket pkgrecibir = new DatagramPacket(recibir, 0, recibir.length);
            Log.d("UDP", "S: Receiving...");

            try {
                rsocket.receive(pkgrecibir);
            } catch (SocketTimeoutException e) {
                Log.d("timeout","TIMEOUT EXCEPTION");
                if (mandarfaltan) {
                    try {
                        socket.send(ultimopaquete); /// send again if timeout until counter =7 to make sure 
                    } catch (IOException q) {
                        q.printStackTrace();
                    }
                    if (countertoend ==7)
                    {
                        Log.d("error","timeout error no response to done");
                        socket.close();
                        rsocket.close();
                        break;
                    }
                    cancelcheck = true;
                    countertoend++;

                } else {
                    Log.d("ACK", "TIMEOUT CANELING PASSING TO DONE");
                    socket.close();
                    rsocket.close();
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            /////HERE YOU CAN OPTAIN ADDRESS OF RECEIVED PACKAGES 
            pkgrecibir.getAddress().getHostAddress();
            /////You can put the address on an array or something similar and you will get your results



        }
我想象它与其他编程语言类似。
关于

连接
DatagramSocket
时,您不会得到
BindException
。绑定时就可以得到它。您需要发布代码、异常及其堆栈跟踪,但您已经错误地描述了您的问题。我在尝试创建DatagramSocket(端口)时收到BindException,如上所述。您“上面提到的”是“当我连接到与UDP相同的端口时”,这是不正确的,正如我上面提到的。就在上面,黑白相间。您仍然没有发布代码、异常或堆栈跟踪。我已经更改了问题。你能帮我一下吗。