Search in sources :

Example 16 with DatagramSocket

use of java.net.DatagramSocket in project jcollectd by collectd.

the class UdpReceiver method getSocket.

public DatagramSocket getSocket() throws IOException {
    if (_socket == null) {
        if (_bindAddress == null) {
            _socket = new DatagramSocket(_port);
        } else {
            InetAddress addr = InetAddress.getByName(_bindAddress);
            if (addr.isMulticastAddress()) {
                MulticastSocket mcast = new MulticastSocket(_port);
                if (_ifAddress != null) {
                    mcast.setInterface(InetAddress.getByName(_ifAddress));
                }
                mcast.joinGroup(addr);
                _socket = mcast;
            } else {
                _socket = new DatagramSocket(_port, addr);
            }
        }
    }
    return _socket;
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) InetAddress(java.net.InetAddress)

Example 17 with DatagramSocket

use of java.net.DatagramSocket in project jcollectd by collectd.

the class ReceiverTest method testBound.

public void testBound() throws Exception {
    DatagramSocket socket = getReceiver().getSocket();
    assertTrue(socket.isBound());
    getLog().info("Bound to LocalPort=" + socket.getLocalPort() + ", LocalAddress=" + socket.getLocalAddress().getHostAddress());
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 18 with DatagramSocket

use of java.net.DatagramSocket in project jcollectd by collectd.

the class ReceiverTest method createSocket.

protected DatagramSocket createSocket() throws IOException {
    DatagramSocket socket = new DatagramSocket();
    _receiver.setListenAddress("127.0.0.1");
    return socket;
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 19 with DatagramSocket

use of java.net.DatagramSocket in project jcollectd by collectd.

the class ReceiverTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    _receiver = new UdpReceiver();
    DatagramSocket socket = createSocket();
    _receiver.setSocket(socket);
    _receiverThread = new ReceiverThread();
    _receiverThread.start();
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 20 with DatagramSocket

use of java.net.DatagramSocket in project android_frameworks_base by ParanoidAndroid.

the class DnsPinger method handleMessage.

@Override
public void handleMessage(Message msg) {
    switch(msg.what) {
        case ACTION_PING_DNS:
            DnsArg dnsArg = (DnsArg) msg.obj;
            if (dnsArg.seq != mCurrentToken.get()) {
                break;
            }
            try {
                ActivePing newActivePing = new ActivePing();
                InetAddress dnsAddress = dnsArg.dns;
                newActivePing.internalId = msg.arg1;
                newActivePing.timeout = msg.arg2;
                newActivePing.socket = new DatagramSocket();
                // Set some socket properties
                newActivePing.socket.setSoTimeout(SOCKET_TIMEOUT_MS);
                // Try to bind but continue ping if bind fails
                try {
                    newActivePing.socket.setNetworkInterface(NetworkInterface.getByName(getCurrentLinkProperties().getInterfaceName()));
                } catch (Exception e) {
                    loge("sendDnsPing::Error binding to socket " + e);
                }
                newActivePing.packetId = (short) sRandom.nextInt();
                byte[] buf = mDnsQuery.clone();
                buf[0] = (byte) (newActivePing.packetId >> 8);
                buf[1] = (byte) newActivePing.packetId;
                // Send the DNS query
                DatagramPacket packet = new DatagramPacket(buf, buf.length, dnsAddress, DNS_PORT);
                if (DBG) {
                    log("Sending a ping " + newActivePing.internalId + " to " + dnsAddress.getHostAddress() + " with packetId " + newActivePing.packetId + ".");
                }
                newActivePing.socket.send(packet);
                mActivePings.add(newActivePing);
                mEventCounter++;
                sendMessageDelayed(obtainMessage(ACTION_LISTEN_FOR_RESPONSE, mEventCounter, 0), RECEIVE_POLL_INTERVAL_MS);
            } catch (IOException e) {
                sendResponse(msg.arg1, -9999, SOCKET_EXCEPTION);
            }
            break;
        case ACTION_LISTEN_FOR_RESPONSE:
            if (msg.arg1 != mEventCounter) {
                break;
            }
            for (ActivePing curPing : mActivePings) {
                try {
                    /** Each socket will block for {@link #SOCKET_TIMEOUT_MS} in receive() */
                    byte[] responseBuf = new byte[2];
                    DatagramPacket replyPacket = new DatagramPacket(responseBuf, 2);
                    curPing.socket.receive(replyPacket);
                    // Check that ID field matches (we're throwing out the rest of the packet)
                    if (responseBuf[0] == (byte) (curPing.packetId >> 8) && responseBuf[1] == (byte) curPing.packetId) {
                        curPing.result = (int) (SystemClock.elapsedRealtime() - curPing.start);
                    } else {
                        if (DBG) {
                            log("response ID didn't match, ignoring packet");
                        }
                    }
                } catch (SocketTimeoutException e) {
                // A timeout here doesn't mean anything - squelsh this exception
                } catch (Exception e) {
                    if (DBG) {
                        log("DnsPinger.pingDns got socket exception: " + e);
                    }
                    curPing.result = SOCKET_EXCEPTION;
                }
            }
            Iterator<ActivePing> iter = mActivePings.iterator();
            while (iter.hasNext()) {
                ActivePing curPing = iter.next();
                if (curPing.result != null) {
                    sendResponse(curPing.internalId, curPing.packetId, curPing.result);
                    curPing.socket.close();
                    iter.remove();
                } else if (SystemClock.elapsedRealtime() > curPing.start + curPing.timeout) {
                    sendResponse(curPing.internalId, curPing.packetId, TIMEOUT);
                    curPing.socket.close();
                    iter.remove();
                }
            }
            if (!mActivePings.isEmpty()) {
                sendMessageDelayed(obtainMessage(ACTION_LISTEN_FOR_RESPONSE, mEventCounter, 0), RECEIVE_POLL_INTERVAL_MS);
            }
            break;
        case ACTION_CANCEL_ALL_PINGS:
            for (ActivePing activePing : mActivePings) activePing.socket.close();
            mActivePings.clear();
            break;
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

DatagramSocket (java.net.DatagramSocket)266 DatagramPacket (java.net.DatagramPacket)120 IOException (java.io.IOException)98 SocketException (java.net.SocketException)71 InetAddress (java.net.InetAddress)57 InetSocketAddress (java.net.InetSocketAddress)45 UnknownHostException (java.net.UnknownHostException)26 Test (org.junit.Test)25 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 ServerSocket (java.net.ServerSocket)14 DatagramChannel (java.nio.channels.DatagramChannel)13 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)8 ByteBuffer (java.nio.ByteBuffer)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)5