Code example for DatagramSocket

Methods: connectgetBroadcastsetBroadcast

        assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress());
    public void test_setBroadcastZ() throws Exception { 
        int[] ports = Support_PortManager.getNextPortsForUDP(3);
        DatagramSocket theSocket = new DatagramSocket(ports[0]);
        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 (Exception ex) {
        // now validate that we can connect to the broadcast address when 
        // setBroadcast is true 
        theSocket.connect(new InetSocketAddress(InetAddress.getByAddress(theBytes), ports[2]));
    public void test_getBroadcast() throws Exception { 
        DatagramSocket theSocket = new DatagramSocket();
Stop searching for code, let great code find you!  Add Codota to your java IDE