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;
}
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());
}
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;
}
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();
}
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;
}
}
Aggregations