本文整理汇总了Java中java.net.MulticastSocket.send方法的典型用法代码示例。如果您正苦于以下问题:Java MulticastSocket.send方法的具体用法?Java MulticastSocket.send怎么用?Java MulticastSocket.send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.MulticastSocket
的用法示例。
在下文中一共展示了MulticastSocket.send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: 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);
}
}
}
示例3: pingListener
import java.net.MulticastSocket; //导入方法依赖的package包/类
protected void pingListener() {
try {
byte[] buf = new byte[256];
String testData = "M-SEARCH * HTTP/1.1\nHOST: " + Configuration.UPNP_MULTICAST_ADDRESS + ":" + Configuration.UPNP_DISCOVERY_PORT + "ST: urn:schemas-upnp-org:device:CloudProxy:1\nMAN: \"ssdp:discover\"\nMX: 3";
buf = testData.getBytes();
MulticastSocket socket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
InetAddress group = InetAddress.getByName(Configuration.UPNP_MULTICAST_ADDRESS);
DatagramPacket packet;
packet = new DatagramPacket(buf, buf.length, group, Configuration.UPNP_DISCOVERY_PORT);
socket.send(packet);
socket.close();
}
catch (IOException e) {
log.warn("Error pinging listener. " + e.getMessage());
}
}
示例4: 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;
}
}
}
}
示例5: 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;
}
示例6: 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())));
}
示例7: test
import java.net.MulticastSocket; //导入方法依赖的package包/类
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);
}
}
}
示例8: main
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* @param args
* @throws UException
*/
public static void main(String[] args) throws Exception {
int port = 6789;
String sendMessage = "adbzz";
InetAddress inetAddress = InetAddress.getByName("228.5.6.255");
DatagramPacket datagramPacket = new DatagramPacket(sendMessage.getBytes(), sendMessage.length(), inetAddress, port);
MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.send(datagramPacket);
}
示例9: main
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) throws Exception{
int port=9527;
int aport=9528;
InetAddress groupAddress=InetAddress.getByName("224.1.1.1");
MulticastSocket server=new MulticastSocket(port);
server.joinGroup(groupAddress);
MulticastSocket client=new MulticastSocket();
client.joinGroup(groupAddress);
byte[] buffer=new byte[65507];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
while(true){
server.receive(packet);
String line=new String(packet.getData(),0,packet.getLength(),"UTF-8");
if("quit".equalsIgnoreCase(line.trim())){
server.close();
System.exit(0);
}
else{
System.out.println("Message from client: "+ line);
packet.setLength(buffer.length);
String response="Server response��"+line;
byte[] datas=response.getBytes("UTF-8");
DatagramPacket responsePacket=new DatagramPacket(datas,datas.length,groupAddress,aport);
client.send(responsePacket);
Thread.sleep(100);
}
}
}
示例10: main
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) throws Exception{
int port=9527;
int aport=9528;
InetAddress groupAddress=InetAddress.getByName("224.1.1.1");
MulticastSocket serverSocket=new MulticastSocket(aport);
serverSocket.joinGroup(groupAddress);
byte[] buffer=new byte[65507];
DatagramPacket receivePacket=new DatagramPacket(buffer,buffer.length);
MulticastSocket socket=new MulticastSocket();
socket.joinGroup(groupAddress);
BufferedReader systemIn=new BufferedReader(new InputStreamReader(System.in));
boolean flag=true;
while(flag){
String command=systemIn.readLine();
byte[] datas=command.getBytes("UTF-8");
DatagramPacket packet=new DatagramPacket(datas,datas.length,groupAddress,port);
socket.send(packet);
if(command==null || "quit".equalsIgnoreCase(command.trim())){
flag=false;
System.out.println("Client quit!");
socket.leaveGroup(groupAddress);
socket.close();
serverSocket.leaveGroup(groupAddress);
serverSocket.close();
continue;
}
serverSocket.receive(receivePacket);
String receiveResponse=new String(receivePacket.getData(),0,receivePacket.getLength(),"UTF-8");
System.out.println(receiveResponse);
}
}
示例11: call
import java.net.MulticastSocket; //导入方法依赖的package包/类
@Override
public Object call() throws Exception {
InetAddress multicastAddress = InetAddress.getByName(this.SSDP_HOST);
MulticastSocket socket = new MulticastSocket(SSDP_PORT);
socket.setReuseAddress(true);
socket.setSoTimeout(10000);
socket.joinGroup(multicastAddress);
byte[] packetBuffer = SSDP_REQUEST().getBytes("UTF-8");
DatagramPacket mSearchPacket = new DatagramPacket(packetBuffer, packetBuffer.length, multicastAddress, SSDP_PORT);
socket.send(mSearchPacket);
/**
* Should run until either:
* - A compatible device is found
* - The socket times out (10000ms)
*/
while(true){
byte[] responseBuffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(packet);
String packetAddress = packet.getAddress().getHostAddress();
String fullResponse = new String(responseBuffer, 0, packet.getLength());
/**
* Make sure the response is not from localhost and the USN contains the HEOS urn.
*/
if(!packetAddress.equals(InetAddress.getLocalHost().getHostAddress())){
if(fullResponse.contains(SSDP_ST)){
return packetAddress;
}
}
}
}
示例12: findHeosIp
import java.net.MulticastSocket; //导入方法依赖的package包/类
public String findHeosIp() throws UnknownHostException, IOException{
InetAddress multicastAddress = InetAddress.getByName(this.SSDP_HOST);
MulticastSocket socket = new MulticastSocket(SSDP_PORT);
socket.setReuseAddress(true);
socket.setSoTimeout(10000);
socket.joinGroup(multicastAddress);
byte[] packetBuffer = SSDP_REQUEST().getBytes("UTF-8");
DatagramPacket mSearchPacket = new DatagramPacket(packetBuffer, packetBuffer.length, multicastAddress, SSDP_PORT);
socket.send(mSearchPacket);
/**
* Should run until either:
* - A compatible device is found
* - The socket times out
*/
while(true){
byte[] responseBuffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(packet);
String packetAddress = packet.getAddress().getHostAddress();
String fullResponse = new String(responseBuffer, 0, packet.getLength());
/**
* Make sure the response is not from localhost and the USN contains the HEOS urn.
*/
if(!packetAddress.equals(InetAddress.getLocalHost().getHostAddress())){
if(fullResponse.contains(SSDP_ST)){
return packetAddress;
}
}
}
}
示例13: muc
import java.net.MulticastSocket; //导入方法依赖的package包/类
public void muc() throws IOException{
MulticastSocket msocket = new MulticastSocket(port);
msocket.joinGroup(InetAddress.getByName("239.12.6.254"));
msocket.setTimeToLive(4);
String content = "";
DatagramPacket packet = new DatagramPacket(
content.getBytes(), content.getBytes().length, InetAddress.getByName("10.12.6.24"), port);
msocket.send(packet);
}
示例14: send
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* Send an outgoing multicast DNS message.
*
* @param out
* @throws java.io.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);
}
}
}
示例15: discover
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* Sends a discovery packet for the specified service and listens for reply packets, notifying
* a callback as services are discovered.
* @param serviceType the type of service to query in mDNS, e.g. {@code "_example._tcp.local"}
* @param callback receives callbacks with {@link Result} objects as answers are decoded from
* incoming reply packets.
* @param timeout duration in milliseconds to wait for answer packets. If {@code 0}, this method
* will listen forever.
* @throws IOException
*/
public static void discover(String serviceType, Callback callback, int timeout) throws IOException {
if (timeout < 0) throw new IllegalArgumentException();
InetAddress group = InetAddress.getByName(MULTICAST_GROUP_ADDRESS);
MulticastSocket sock = new MulticastSocket(); // binds to a random free source port
if (DEBUG) System.out.println("Source port is " + sock.getLocalPort());
byte[] data = discoverPacket(serviceType);
if (DEBUG) System.out.println("Query packet:");
if (DEBUG) hexdump(data, 0, data.length);
DatagramPacket packet = new DatagramPacket(data, data.length, group, PORT);
sock.setTimeToLive(255);
sock.send(packet);
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
long endTime = 0;
if (timeout != 0) {
endTime = System.currentTimeMillis() + timeout;
}
while (true) {
if (timeout != 0) {
int remaining = (int) (endTime - System.currentTimeMillis());
if (remaining <= 0) {
break;
}
sock.setSoTimeout(remaining);
}
try {
sock.receive(packet);
} catch (SocketTimeoutException e) {
break;
}
if (DEBUG) System.out.println("\n\nIncoming packet:");
if (DEBUG) hexdump(packet.getData(), 0, packet.getLength());
Result result = decode(packet.getData(), packet.getLength());
if (callback != null) {
callback.onResult(result);
}
}
}