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

时间:2015-07-03 05:25:51

标签: sockets exception tcp udp

我有一台连接到内联网的电脑。我假设同一个内部网中还有其他个人电脑。现在,一个udp服务器运行在所有这些n个pc上。现在,我的问题是我不知道有udp服务器的PC的数量。所以我需要播放一条消息说'abc',每台带有udp服务器的电脑将返回自己的ip。现在我如何将我的消息'abc'广播到我网络中可用的所有n个电脑。

1 个答案:

答案 0 :(得分:0)

在java中

    ///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



        }

我想象其他编程语言与它类似。 此致