Code example for DatagramSocket

Methods: close, connect, getBroadcast, setBroadcast

0
        } 
    } 
 
    public void test_setBroadcastZ() throws Exception { 
        int[] ports = Support_PortManager.getNextPortsForUDP(3);
        DatagramSocket theSocket = new DatagramSocket(ports[0]);
        theSocket.setBroadcast(false);
        byte theBytes[] = { -1, -1, -1, -1 };
 
        // validate we cannot connect to the broadcast address when 
        // setBroadcast is false 
        try { 
            theSocket.connect(new InetSocketAddress(InetAddress
                    .getByAddress(theBytes), ports[1]));
            assertFalse(
                    "No exception when connecting to broadcast address with setBroadcast(false)", 
                    theSocket.getBroadcast());
        } catch (SocketException ex) {
            //expected 
        } 
 
        // now validate that we can connect to the broadcast address when 
        // setBroadcast is true 
        theSocket.setBroadcast(true);
        theSocket.connect(new InetSocketAddress(InetAddress
                        .getByAddress(theBytes), ports[2]));
 
        theSocket.close();
        try { 
            theSocket.setBroadcast(false);
            fail("SocketException was not thrown.");
        } catch(SocketException se) {
            //expected 
        } 
    }