下面列出了java.net.MulticastSocket#setSoTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a new instance from the given {@link MulticastSocket}.
*
* @param socket the {@link MulticastSocket} which is used by this instance
*/
public OioDatagramChannel(MulticastSocket socket) {
super(null);
boolean success = false;
try {
socket.setSoTimeout(SO_TIMEOUT);
socket.setBroadcast(false);
success = true;
} catch (SocketException e) {
throw new ChannelException(
"Failed to configure the datagram socket timeout.", e);
} finally {
if (!success) {
socket.close();
}
}
this.socket = socket;
config = new DefaultOioDatagramChannelConfig(this, socket);
}
private synchronized void startDiscovery() {
try {
logger.debug("Starting Homematic CCU discovery scan");
String configuredBroadcastAddress = networkAddressService.getConfiguredBroadcastAddress();
if (configuredBroadcastAddress != null) {
broadcastAddress = InetAddress.getByName(configuredBroadcastAddress);
}
if (broadcastAddress == null) {
logger.warn("Homematic CCU discovery: discovery not possible, no broadcast address found");
return;
}
socket = new MulticastSocket();
socket.setBroadcast(true);
socket.setTimeToLive(5);
socket.setSoTimeout(800);
sendBroadcast();
receiveResponses();
} catch (Exception ex) {
logger.error("An error was thrown while running Homematic CCU discovery {}", ex.getMessage(), ex);
} finally {
scanFuture = null;
}
}
Multicast(final Tracker tracker) throws IOException {
this.tracker = tracker;
multicast = new MulticastSocket(port);
multicast.setLoopbackMode(loopbackMode);
multicast.setTimeToLive(timeToLive);
multicast.joinGroup(address.getAddress());
multicast.setSoTimeout((int) heartRate);
listenerThread = new Thread(new Listener());
listenerThread.setName("MulticastDiscovery: Listener");
listenerThread.setDaemon(true);
listenerThread.start();
final Broadcaster broadcaster = new Broadcaster();
timer = new Timer("MulticastDiscovery: Broadcaster", true);
timer.scheduleAtFixedRate(broadcaster, 0, heartRate);
}
/**
* Create a new instance from the given {@link MulticastSocket}.
*
* @param socket the {@link MulticastSocket} which is used by this instance
*/
public OioDatagramChannel(MulticastSocket socket) {
super(null);
boolean success = false;
try {
socket.setSoTimeout(SO_TIMEOUT);
socket.setBroadcast(false);
success = true;
} catch (SocketException e) {
throw new ChannelException(
"Failed to configure the datagram socket timeout.", e);
} finally {
if (!success) {
socket.close();
}
}
this.socket = socket;
config = new DefaultDatagramChannelConfig(this, socket);
}
/**
* Broadcasts a SSDP discovery message into the network to find provided
* services.
*
* @return The Socket the answers will arrive at.
* @throws UnknownHostException
* @throws IOException
* @throws SocketException
* @throws UnsupportedEncodingException
*/
private MulticastSocket sendDiscoveryBroacast()
throws UnknownHostException, IOException, SocketException, UnsupportedEncodingException {
InetAddress multicastAddress = InetAddress.getByName("239.255.255.250");
final int port = 1900;
MulticastSocket socket = new MulticastSocket(port);
socket.setReuseAddress(true);
socket.setSoTimeout(130000);
socket.joinGroup(multicastAddress);
byte[] requestMessage = DISCOVER_MESSAGE.getBytes("UTF-8");
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, multicastAddress,
port);
socket.send(datagramPacket);
return socket;
}
public MulticastSearch(final String host, final int port) throws IOException {
final InetAddress inetAddress = InetAddress.getByName(host);
multicast = new MulticastSocket(port);
multicast.joinGroup(inetAddress);
multicast.setSoTimeout(500);
}
private static void test() throws Exception {
final String hostname = "google.com";
final String localhost = "localhost";
final MulticastSocket datagramSocket = new MulticastSocket();
datagramSocket.setSoTimeout(10000);
short ttl = 1;
final InetAddress receiverAddress = InetAddress.getByName(hostname);
while (ttl < 100) {
try {
byte[] buffer = "0123456789".getBytes();
datagramSocket.setTimeToLive(ttl++);
final DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length, receiverAddress, 80);
datagramSocket.send(sendPacket);
buffer = new byte[10];
final DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
datagramSocket.receive(receivePacket);
System.out.println("ttl=" + ttl + " address=" + receivePacket.getAddress().getHostAddress() + " data="
+ new String(receivePacket.getData()));
Thread.sleep(1000);
} catch (final SocketTimeoutException e) {
System.out.println("timeout ttl=" + ttl);
}
}
}
/**
* Scans all messages that arrive on the socket and scans them for the
* search keywords. The search is not case sensitive.
*
* @param socket
* The socket where the answers arrive.
* @param keywords
* The keywords to be searched for.
* @return
* @throws IOException
*/
private String scanResposesForKeywords(MulticastSocket socket, String... keywords) throws IOException {
// In the worst case a SocketTimeoutException raises
socket.setSoTimeout(2000);
do {
logger.debug("Got an answer message.");
byte[] rxbuf = new byte[8192];
DatagramPacket packet = new DatagramPacket(rxbuf, rxbuf.length);
socket.receive(packet);
String foundIp = analyzePacket(packet, keywords);
if (foundIp != null) {
return foundIp;
}
} while (true);
}
/**
* Blocking call
*/
public static boolean recvData(MulticastSocket s, byte[] buffer) throws IOException {
s.setSoTimeout(100);
// Create a DatagramPacket and do a receive
final DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
try {
s.receive(pack);
} catch (SocketTimeoutException e) {
return false;
}
// We have finished receiving data
return true;
}
private UDPTransport(String address, int port, int ttl, int soTimeout) throws IOException {
// InetSocketAddress
inetSocketAddress = new InetSocketAddress(address, port);
// Create the socket but we don't bind it as we are only going to send data
// Note that we don't have to join the multicast group if we are only sending data and not receiving
multicastSocket = new MulticastSocket();
multicastSocket.setReuseAddress(true);
multicastSocket.setSoTimeout(soTimeout);
multicastSocket.setTimeToLive(ttl);
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(address);
socket.setSoTimeout(msTimeOut);
return socket;
}