Code example for DatagramSocket

Methods: connectdisconnectgetInetAddressgetPort

0
            ds.connect(inetAddress, portNumber);
        } 
    } 
 
    public void test_disconnect() throws Exception { 
        DatagramSocket ds = new DatagramSocket();
        InetAddress inetAddress = InetAddress.getLocalHost();
        ds.connect(inetAddress, 0);
        ds.disconnect();
        assertNull("Incorrect InetAddress", ds.getInetAddress());
        assertEquals("Incorrect Port", -1, ds.getPort());
 
        ds = new DatagramSocket();
        inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4");
        ds.connect(inetAddress, 0);
        ds.disconnect();
        assertNull("Incorrect InetAddress", ds.getInetAddress());
        assertEquals("Incorrect Port", -1, ds.getPort());
    } 
 
Stop searching for code, let great code find you!  Add Codota to your java IDE