DatagramPacket request = new DatagramPacket(字节数组, 长度, InetAddress, 端口); DatagramSocket socket = new DatagramSocket(); socket.send(request);
记错了。有三种传输, TCP是unicast,UDP是MuiltiCast和BroadCast Broadcast的范围太大,会造成broadcast storm, multicast是让交换机只发送到某一个特定组的节点,所以multicast storm的概率要小,性能好一些这给你一个MuiltiCast的例子 The receiving-socket // Import some needed classes import sun.net.*; import java.net.*;// Which port should we listen to int port = 5000; // Which address String group = "225.4.5.6";// Create the socket and bind it to port 'port'. MulticastSocket s = new MulticastSocket(port);// join the multicast group s.joinGroup(InetAddress.getByName(group)); // Now the socket is set up and we are ready to receive packets// Create a DatagramPacket and do a receive byte buf[] = byte[1024]; DatagramPacket pack = new DatagramPacket(buf, buf.length); s.receive(pack);// Finally, let us do something useful with the data we just received, // like print it on stdout :-) System.out.println("Received data from: " + pack.getAddress().toString() + ":" + pack.getPort() + " with length: " + pack.getLength()); System.out.write(pack.getData(),0,pack.getLength()); System.out.println();// And when we have finished receiving data leave the multicast group and // close the socket s.leaveGroup(InetAddress.getByName(group); s.close();The sending-socket // Import some needed classes import sun.net.*; import java.net.*;// Which port should we send to int port = 5000; // Which address String group = "225.4.5.6"; // Which ttl int ttl = 1;// Create the socket but we don't bind it as we are only going to send data MulticastSocket s = new MulticastSocket();// Note that we don't have to join the multicast group if we are only // sending data and not receiving// Fill the buffer with some data byte buf[] = byte[10]; for (int i=0; i<buf.length; i++) buf[i] = (byte)i; // Create a DatagramPacket DatagramPacket pack = new DatagramPacket(buf, buf.length, InetAddress.getByName(group), port); // Do a send. Note that send takes a byte for the ttl and not an int. s.send(pack,(byte)ttl);// And when we have finished sending data close the socket s.close();
好像只能用D类地址(224.0.0.1 to 239.255.255.255)最为广播组地址,需要交换机的支持。
for(int i =1; i<=255; i++){
String ip_address = "192.168.60."+i;
//TODO:do your operation here
}
InetAddress如果用168.191.236.255, 就表示对网段168.191.236广播,255是个特殊的主机地址
DatagramPacket request = new DatagramPacket(字节数组, 长度, InetAddress, 端口);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
Broadcast的范围太大,会造成broadcast storm, multicast是让交换机只发送到某一个特定组的节点,所以multicast storm的概率要小,性能好一些这给你一个MuiltiCast的例子
The receiving-socket
// Import some needed classes
import sun.net.*;
import java.net.*;// Which port should we listen to
int port = 5000;
// Which address
String group = "225.4.5.6";// Create the socket and bind it to port 'port'.
MulticastSocket s = new MulticastSocket(port);// join the multicast group
s.joinGroup(InetAddress.getByName(group));
// Now the socket is set up and we are ready to receive packets// Create a DatagramPacket and do a receive
byte buf[] = byte[1024];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
s.receive(pack);// Finally, let us do something useful with the data we just received,
// like print it on stdout :-)
System.out.println("Received data from: " + pack.getAddress().toString() +
":" + pack.getPort() + " with length: " +
pack.getLength());
System.out.write(pack.getData(),0,pack.getLength());
System.out.println();// And when we have finished receiving data leave the multicast group and
// close the socket
s.leaveGroup(InetAddress.getByName(group);
s.close();The sending-socket
// Import some needed classes
import sun.net.*;
import java.net.*;// Which port should we send to
int port = 5000;
// Which address
String group = "225.4.5.6";
// Which ttl
int ttl = 1;// Create the socket but we don't bind it as we are only going to send data
MulticastSocket s = new MulticastSocket();// Note that we don't have to join the multicast group if we are only
// sending data and not receiving// Fill the buffer with some data
byte buf[] = byte[10];
for (int i=0; i<buf.length; i++) buf[i] = (byte)i;
// Create a DatagramPacket
DatagramPacket pack = new DatagramPacket(buf, buf.length,
InetAddress.getByName(group), port);
// Do a send. Note that send takes a byte for the ttl and not an int.
s.send(pack,(byte)ttl);// And when we have finished sending data close the socket
s.close();