客户端UDP广播发送服务类型

时间:2016-10-27 18:39:02

标签: java service udp client broadcast

我想编写一个Java代码,其中客户端使用UDP广播发送服务类型

public void run()
{
    try
    {
        host = InetAddress.getByName(hostname);
        socket = new DatagramSocket (null);
        packet=new DatagramPacket (new byte[100], 0,host, port);
        socket.send (packet);
        packet.setLength(100);
        socket.receive (packet);
        socket.close ();
        byte[] data = packet.getData ();
        String time=new String(data);  // convert byte array data into string
        System.out.println(time);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }`
}
}

0 个答案:

没有答案