Search in sources :

Example 91 with DatagramSocket

use of java.net.DatagramSocket in project robovm by robovm.

the class OldDatagramPacketTest method test_getPort.

public void test_getPort() throws Exception {
    DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress.getLocalHost(), 1000);
    assertEquals("Incorrect port returned", 1000, dp.getPort());
    final DatagramSocket ss = new DatagramSocket();
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                DatagramPacket packet = new DatagramPacket(new byte[256], 256);
                ss.setSoTimeout(3000);
                ss.receive(packet);
                ss.send(packet);
            } catch (IOException e) {
                System.out.println("thread exception: " + e);
            }
        }
    });
    thread.start();
    DatagramSocket cs = new DatagramSocket();
    try {
        byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6 };
        DatagramPacket packet = new DatagramPacket(bytes, 6, InetAddress.getByName("localhost"), ss.getLocalPort());
        cs.send(packet);
        cs.setSoTimeout(3000);
        cs.receive(packet);
        cs.close();
        assertEquals(packet.getPort(), ss.getLocalPort());
    } finally {
        cs.close();
        ss.close();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 92 with DatagramSocket

use of java.net.DatagramSocket in project routerkeygenAndroid by routerkeygen.

the class AlcatelLucentKeygen method getKeys.

@Override
public List<String> getKeys() {
    if (getMacAddress().length() != 12) {
        setErrorCode(R.string.msg_errpirelli);
        return null;
    }
    DNSQuery dnsquery = new DNSQuery(getMacAddress(), 255, 1);
    DatagramSocket datagramsocket = null;
    try {
        datagramsocket = new DatagramSocket();
        datagramsocket.setSoTimeout(5000);
        int i = 0;
        boolean noReply = false;
        do {
            try {
                sendQuery(dnsquery, datagramsocket, InetAddress.getByName("hak.im"));
                noReply = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            ++i;
            if (i >= 3) {
                //Give up.
                return getResults();
            }
        } while (!noReply);
        getResponse(dnsquery, datagramsocket);
        addPassword(NSLookup.getKey(dnsquery));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (datagramsocket != null) {
            datagramsocket.close();
        }
    }
    return getResults();
}
Also used : DatagramSocket(java.net.DatagramSocket) DNSQuery(org.exobel.routerkeygen.utils.dns.DNSQuery) IOException(java.io.IOException)

Example 93 with DatagramSocket

use of java.net.DatagramSocket in project spring-framework by spring-projects.

the class SocketUtilsTests method findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable.

@Test(expected = IllegalStateException.class)
public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
    int port = SocketUtils.findAvailableUdpPort();
    DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
    try {
        // will only look for the exact port
        SocketUtils.findAvailableUdpPort(port, port);
    } finally {
        socket.close();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) Test(org.junit.Test)

Example 94 with DatagramSocket

use of java.net.DatagramSocket in project robovm by robovm.

the class DatagramChannelTest method testReceiveSend_Block_Empty_C2S.

public void testReceiveSend_Block_Empty_C2S() throws Exception {
    this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
    sendByChannel("", localAddr2);
    receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, "");
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 95 with DatagramSocket

use of java.net.DatagramSocket in project robovm by robovm.

the class DatagramChannelTest method testReceiveSend_NonBlock_Empty_C2S.

public void testReceiveSend_NonBlock_Empty_C2S() throws Exception {
    this.channel1.configureBlocking(false);
    this.channel2.configureBlocking(false);
    this.datagramSocket1 = new DatagramSocket(localAddr2.getPort());
    sendByChannel("", localAddr2);
    receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, "");
}
Also used : DatagramSocket(java.net.DatagramSocket)

Aggregations

DatagramSocket (java.net.DatagramSocket)294 DatagramPacket (java.net.DatagramPacket)137 IOException (java.io.IOException)112 SocketException (java.net.SocketException)74 InetAddress (java.net.InetAddress)63 InetSocketAddress (java.net.InetSocketAddress)46 UnknownHostException (java.net.UnknownHostException)33 Test (org.junit.Test)27 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 ServerSocket (java.net.ServerSocket)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 DatagramChannel (java.nio.channels.DatagramChannel)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)9 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)6