发送方:设置发送和接收方的SocketAddress ip和端口,实例化DatagramSocket对象来传递数据。将数据转化为byte数组后用DatagramPacket 打包数据,用DategramSocket对象的send()方法发送。
接收方:new一个SocketAddress对象,实例化一个DatagramSocket对象传入本地地址,准备好一个DatagramPacket对象来预备接受,通过DatagramSocket的receive方法将packet对象传入,好让packet的属性被赋值,通过转型接受即可得到对应数据。
代码如下:
发送方:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import sun.management.snmp.jvminstr.JvmThreadInstanceEntryImpl.ThreadStateMap.Byte0;
public class Send {
public static void main(String[] args) throws Exception {
// 发送方地址
SocketAddress sendAd = new InetSocketAddress("127.0.0.1", 4210);
// 接收方地址
SocketAddress receiveAd = new InetSocketAddress("127.0.0.1", 5310);
// 发送地的快递员
DatagramSocket socket = new DatagramSocket(sendAd);
// 发送的数据
String str = "顺丰快递您的包裹";
// 转成字节发送
byte[] bt = str.getBytes();
System.out.println(bt.length);
// 打包
DatagramPacket packet = new DatagramPacket(bt, bt.length, receiveAd);
// 由指定对象发送
socket.send(packet);
}
}
接收方:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
public class Recieve {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 自己地址
SocketAddress iAd = new InetSocketAddress("127.0.0.1", 5310);
// 接收地的快递员
DatagramSocket socket = new DatagramSocket(iAd);
byte[] buf = new byte[1024];
// 自家邮箱
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// 收快递
socket.receive(packet);
String str = new String(buf);
System.out.println(str.trim());
}
}
UDP组播:
发送:和一对一传播差不多。将地址换为InetAddress,传输数据的对象换为MulticastSocket的,DatagramPacket的对象将数据打包,要用该对象的send方法发送。
接收:地址为组播地址,InetAddress,预备好byte数组、String、DatagramPacket对象。
实例化一个MulitcastSocket对象传入端口,用该方法的joinGroup()方法将该地址传入。
用该对象的receive(packet)方法接收,对应转型即可得到发送的数据。
发送方:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
public class Sender {
public static void main(String[] args) throws Exception {
//组播地址
InetAddress address= InetAddress.getByName("230.0.0.1");
//发送的数据
String str = "组播数据";
byte[] bt = str.getBytes();
DatagramPacket packet = new DatagramPacket(bt,bt.length,address,9091);
MulticastSocket cSocket = new MulticastSocket();
cSocket.send(packet);
}
}
接收方:
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
public class Reciever {
public static void main(String[] args) throws Exception {
//组播地址
InetAddress address= InetAddress.getByName("230.0.0.1");
//预接收的数据
byte[] bt = new byte[1024];
DatagramPacket packet = new DatagramPacket(bt,bt.length);
MulticastSocket cSocket = new MulticastSocket(9091);
cSocket.joinGroup(address);
cSocket.receive(packet);
String str = new String(bt);
System.out.println(str.trim());
}
}