UDP协议:低延时,自身不能保证传输数据的可靠性,但是可以采取措施来保证其可靠性,由于它的低延时性,导致在一些特殊场景下(灾难预警)无法使用。
UDP常用在单播,组播,广播通信中。
实现UDP网络通信需要用到:
DatagramSocket类:可以创建套接字并绑定接口
DatagramPacket类:数据报–数据传输的载体
需要注意的是,DatagramPacket类中的DatagramPacket()构造方法中的参数只能为 字节数组byte[]类型
所以在传输数据时还需要用到字节流,进行数据格式的转换。
发送方对数据的处理: 基本数据类型--------> 字节数组:
ByteArrayOutputStream<-------->DataOutputStream
复合数据类型(对象)---------->字节数组:
ByteArrayOutputStream<-------->ObjectOutputStream 接收方处理数据即用到对应的input流。
下面我们直接来看代码:
接受信息的类:
package cn.mafang;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Reciever {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DatagramSocket receiver=new DatagramSocket(10010);// 1.创建套接字
byte[] data=new byte[1024];
DatagramPacket packet=new DatagramPacket(data,data.length);// 2.创建数据报
try {
receiver.receive(packet); // 3.接受发送方传来的数据
ByteArrayInputStream bis=new ByteArrayInputStream(data);
ObjectInputStream ois=new ObjectInputStream(bis);
try {
Student stu=(Student) ois.readObject();
System.out.println("接受到的数据由"+packet.getSocketAddress()+"发过来的");
System.out.println(stu.toString());
String mess="我管你叫啥呢!!!!别理我!!!";
byte[] message=mess.getBytes();
//接收方向发送方回复消息
DatagramPacket sen=new DatagramPacket(message,message.length,InetAddress.getLocalHost(),10023);
receiver.send(sen);
System.out.println("我向"+packet.getSocketAddress()+"回复了消息");
ois.close();
bis.close();
receiver.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
发送信息的类:
package cn.mafang;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Sender {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DatagramSocket send=new DatagramSocket(10023);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Student student=new Student(134, "张三");
try {
oos.writeObject(student);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] data=baos.toByteArray();
try {
// 2
DatagramPacket packet=new DatagramPacket(data,data.length,InetAddress.getLocalHost(),10010);
try {
send.send(packet);
System.out.println("我已经向"+packet.getPort()+"端口发送了数据");
byte[] data2=new byte[10];
DatagramPacket re=new DatagramPacket(data2,data2.length);
send.receive(re);
System.out.println("我收到了消息:"+new String(data2));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
send.close();
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
发送的信息为对象:
package cn.mafang;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int ID;
private String name;
public Student(int id,String name){
this.ID=id;
this.name=name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "学号:"+this.ID+" 姓名:"+this.name;
}
}