本文整理汇总了Java中java.net.MulticastSocket类的典型用法代码示例。如果您正苦于以下问题:Java MulticastSocket类的具体用法?Java MulticastSocket怎么用?Java MulticastSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MulticastSocket类属于java.net包,在下文中一共展示了MulticastSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onResume
import java.net.MulticastSocket; //导入依赖的package包/类
@Override
protected void onResume() {
super.onResume();
fileReciveThread = new FileReciveThread();
fileReciveThread.start();
try {
socket = new MulticastSocket(portNum);
socket.setInterface(ip);
socket.setBroadcast(true);
group = InetAddress.getByName("224.0.0.1");
socket.joinGroup(new InetSocketAddress(group, portNum), networkInterface);
} catch (IOException e) {
e.printStackTrace();
}
}
示例2: PacketReceiver
import java.net.MulticastSocket; //导入依赖的package包/类
public PacketReceiver( MessageListener listener )
{
messageListener = listener; // set MessageListener
try // connect MulticastSocket to multicast address and port
{
// create new MulticastSocket
multicastSocket = new MulticastSocket(
MULTICAST_LISTENING_PORT );
// use InetAddress to get multicast group
multicastGroup = InetAddress.getByName( MULTICAST_ADDRESS );
// join multicast group to receive messages
multicastSocket.joinGroup( multicastGroup );
// set 5 second timeout when waiting for new packets
multicastSocket.setSoTimeout( 5000 );
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
}
示例3: RtpSocketUdp
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* This RTP socket implements a buffering mechanism relying on a FIFO of buffers and a Thread.
*/
public RtpSocketUdp(ConnectCheckerRtsp connectCheckerRtsp) {
super();
this.connectCheckerRtsp = connectCheckerRtsp;
senderReportUdp = new SenderReportUdp(connectCheckerRtsp);
senderReportUdp.reset();
packets = new DatagramPacket[bufferCount];
for (int i = 0; i < bufferCount; i++) {
packets[i] = new DatagramPacket(buffers[i], 1);
}
try {
mSocket = new MulticastSocket();
} catch (IOException e) {
e.printStackTrace();
}
}
示例4: RTPSocketAdapter
import java.net.MulticastSocket; //导入依赖的package包/类
public RTPSocketAdapter(InetAddress addr, int port, int ttl) throws IOException {
try {
if (addr.isMulticastAddress()) {
dataSock = new MulticastSocket(port);
ctrlSock = new MulticastSocket(port+1);
((MulticastSocket)dataSock).joinGroup(addr);
((MulticastSocket)dataSock).setTimeToLive(ttl);
((MulticastSocket)ctrlSock).joinGroup(addr);
((MulticastSocket)ctrlSock).setTimeToLive(ttl);
} else {
dataSock = new DatagramSocket(port, InetAddress.getLocalHost());
ctrlSock = new DatagramSocket(port+1, InetAddress.getLocalHost());
}
} catch (SocketException e) {
throw new IOException(e.getMessage());
}
this.addr = addr;
this.port = port;
}
示例5: UDPread0
import java.net.MulticastSocket; //导入依赖的package包/类
UDPread0(int iport, String isrcName, String[] ichanName, int idt) {
port = iport;
srcName = isrcName;
chanName = ichanName;
try { //open port for incoming UDP
if(multiCast != null) {
System.err.println("Multicast address: "+multiCast);
ms = new MulticastSocket(port);
ms.joinGroup(InetAddress.getByName(multiCast));
}
else {
ds = new DatagramSocket(port);
// ds.setSoTimeout(10); // non-blocking timeout
}
} catch (Exception e) { e.printStackTrace(); }
}
示例6: multicast
import java.net.MulticastSocket; //导入依赖的package包/类
/**
*
* @param multicastIP for example, 224.X.X.X or 239.X.X.X
* @param port
* @param bs
*/
public static void multicast(final String multicastIP, final int port, final byte[] sendData){
final WifiManager wifi = getWiFiManager();
MulticastLock multicastLock = wifi.createMulticastLock(String.valueOf(System.currentTimeMillis()));
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
try{
final MulticastSocket multicastSocket=new MulticastSocket(port);
multicastSocket.setLoopbackMode(true);
final InetAddress group = InetAddress.getByName(multicastIP);
multicastSocket.joinGroup(group);
final DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,port);
multicastSocket.send(packet);
}catch (final Throwable e) {
e.printStackTrace();
}
if (multicastLock != null) {
multicastLock.release();
multicastLock = null;
}
}
示例7: send
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* Send an outgoing multicast DNS message.
*
* @param out
* @exception IOException
*/
public void send(DNSOutgoing out) throws IOException {
if (!out.isEmpty()) {
byte[] message = out.data();
final DatagramPacket packet = new DatagramPacket(message, message.length, _group, DNSConstants.MDNS_PORT);
if (logger.isLoggable(Level.FINEST)) {
try {
final DNSIncoming msg = new DNSIncoming(packet);
if (logger.isLoggable(Level.FINEST)) {
logger.finest("send(" + this.getName() + ") JmDNS out:" + msg.print(true));
}
} catch (final IOException e) {
logger.throwing(getClass().toString(), "send(" + this.getName() + ") - JmDNS can not parse what it sends!!!", e);
}
}
final MulticastSocket ms = _socket;
if (ms != null && !ms.isClosed()) {
ms.send(packet);
}
}
}
示例8: sendTextMessageThroughUdp
import java.net.MulticastSocket; //导入依赖的package包/类
@Test
public void sendTextMessageThroughUdp() throws Exception {
// replace existing implementation for testing purposes
_testKit.removeService("DefaultGreetingService");
final MockHandler greetingService = _testKit.registerInOnlyService("DefaultGreetingService");
MulticastSocket clientSocket = new MulticastSocket();
InetAddress group = InetAddress.getByName("localhost");
byte[] datagramBody = PAYLOAD.getBytes(Charset.defaultCharset());
DatagramPacket packet = new DatagramPacket(datagramBody, 0, datagramBody.length, group, 3940);
clientSocket.send(packet);
// sleep a bit to receive message on camel side
clientSocket.close();
greetingService.waitForOKMessage();
final LinkedBlockingQueue<Exchange> recievedMessages = greetingService.getMessages();
assertThat(recievedMessages, is(notNullValue()));
final Exchange recievedExchange = recievedMessages.iterator().next();
String content = recievedExchange.getMessage().getContent(String.class);
// the receive content is trimmed because extra bytes appended to frame by receiver
assertThat(PAYLOAD, is(equalTo(content.trim())));
}
示例9: sendFrame
import java.net.MulticastSocket; //导入依赖的package包/类
public void sendFrame(final Frame frame) throws IOException {
final byte[] message = toValidMessage(frame);
final DatagramPacket packet = new DatagramPacket(message, 0, message.length, mcastAddress, mcastPort);
for (final MulticastSocket mcastSocket : mcastSockets) {
try {
sentMessages++;
mcastSocket.send(packet);
} catch (final IOException e) {
final String exceptionMessage = e.getMessage();
if (exceptionMessage.endsWith(NO_BUFFER_SPACE_AVAILABLE)
|| exceptionMessage.endsWith(NO_ROUTE_TO_HOST)) {
final NetworkInterface networkInterface = mcastSocket.getNetworkInterface();
final InetAddress mcastSocketInterface = mcastSocket.getInterface();
LOG.warn(createIgnoredWarning(exceptionMessage, networkInterface, mcastSocketInterface));
} else {
throw e;
}
}
}
}
示例10: sendDiscoveryBroacast
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* 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;
}
示例11: scanResposesForKeywords
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* 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);
}
示例12: retrieveResponse
import java.net.MulticastSocket; //导入依赖的package包/类
static String retrieveResponse() throws Exception {
String response = null;
MulticastSocket recSocket = setUpSocket();
int i = 0;
while (response == null) {
byte[] buf = new byte[2048];
DatagramPacket input = new DatagramPacket(buf, buf.length);
try {
recSocket.receive(input);
response = new String(input.getData());
} catch (SocketTimeoutException e) {
if (i >= 10) break;
i++;
}
}
return response;
}
示例13: RtpSocket
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* This RTP socket implements a buffering mechanism relying on a FIFO of buffers and a Thread.
* @throws IOException
*/
public RtpSocket() {
mCacheSize = 0;
mBufferQ = new ConcurrentLinkedQueue<PacketBufferClass>();
mReport = new SenderReport();
mAverageBitrate = new AverageBitrate();
mTransport = TRANSPORT_UDP;
mTcpHeader = new byte[] {'$',0,0,0};
mBufferInOut = new AtomicInteger();
mPayloadType = 96;
resetFifo();
try {
mSocket = new MulticastSocket();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
示例14: createSocket
import java.net.MulticastSocket; //导入依赖的package包/类
/**
* Creates multicast socket and joins multicast group.
*
* @throws IOException If fails to create socket or join multicast group.
* @return Multicast socket.
*/
private MulticastSocket createSocket() throws IOException {
MulticastSocket sock = new MulticastSocket(mcastPort);
sock.setLoopbackMode(false); // Use 'false' to enable support for more than one node on the same machine.
if (sockItf != null)
sock.setInterface(sockItf);
if (sock.getLoopbackMode())
U.warn(log, "Loopback mode is disabled which prevents nodes on the same machine from discovering " +
"each other.");
sock.joinGroup(mcastGrp);
if (ttl != -1)
sock.setTimeToLive(ttl);
return sock;
}
示例15: init
import java.net.MulticastSocket; //导入依赖的package包/类
synchronized public void init(InetAddress bindAddress, Router router, DatagramProcessor datagramProcessor) throws InitializationException {
this.router = router;
this.datagramProcessor = datagramProcessor;
try {
// TODO: UPNP VIOLATION: The spec does not prohibit using the 1900 port here again, however, the
// Netgear ReadyNAS miniDLNA implementation will no longer answer if it has to send search response
// back via UDP unicast to port 1900... so we use an ephemeral port
log.info("Creating bound socket (for datagram input/output) on: " + bindAddress);
localAddress = new InetSocketAddress(bindAddress, 0);
socket = new MulticastSocket(localAddress);
socket.setTimeToLive(configuration.getTimeToLive());
socket.setReceiveBufferSize(262144); // Keep a backlog of incoming datagrams if we are not fast enough
} catch (Exception ex) {
throw new InitializationException("Could not initialize " + getClass().getSimpleName() + ": " + ex);
}
}