接收端和发送端是两个类。
基本数据类型:
//发送端
public static void clientData() throws Exception {
DatagramSocket client = new DatagramSocket(8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
//操作数据类型+数据
dos.writeChar(12);//int
dos.writeUTF("nihao");//String
dos.flush();//要刷新一下,不刷新可能会报EOFException错误
byte[] datas = baos.toByteArray();
DatagramPacket packet = //下面的第四个参数是创建本机的,9999要和接收端那指定的端口一致,不然会丢失数据包
new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999));
client.send(packet);
client.close();
}
//接收端
public static void serverData() throws Exception {
// Address already in use: Cannot bind 这个错误是由于同一个机子同一协议下端口冲突
DatagramSocket server = new DatagramSocket(9999);
byte[] container = new byte[1024*60];//最多只能操作1024*60的数据,太大会报错
DatagramPacket packet = new DatagramPacket(container,0,container.length);
server.receive(packet);
byte[] datas = packet.getData();
DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
//顺序与读取一致,不然会报EOFException错误
int a = dis.readChar();
String s = dis.readUTF();
System.out.println(a);
server.close();
}
可以发现,用的就是IO流的数据流。
对象:
//来个对象
class Dog implements java.io.Serializable{
private int age;
private String coloer;
//private transient String coloer; 加了transient不会被序列化
public Dog(int age, String coloer) {
this.age = age;
this.coloer = coloer;
}
public String shout(){
return "汪汪";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColoer() {
return coloer;
}
public void setColoer(String coloer) {
this.coloer = coloer;
}
}
//发送端
public static void clientObject() throws Exception {
DatagramSocket client = new DatagramSocket(8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
//操作数据类型+数据
oos.writeChar(12);//int
oos.writeUTF("nihao");//String
//对象 String,Date这两个类都实现了java.io.Serializable
oos.writeObject("字符串对象");
oos.writeObject(new Date());
Dog dog = new Dog(2,"黄色");
oos.writeObject(dog);
oos.flush();//要刷新一下,不刷新可能会报EOFException错误
byte[] datas = baos.toByteArray();
DatagramPacket packet = //下面的第四个参数是创建本机的,9999要和接收端那指定的端口一致,不然会丢失数据包
new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999));
client.send(packet);
client.close();
}
//接收端
public static void serverObject() throws Exception {
// Address already in use: Cannot bind 这个错误是由于同一个机子同一协议下端口冲突
DatagramSocket server = new DatagramSocket(9999);
byte[] container = new byte[1024*60];//最多只能操作1024*60的数据,太大会报错
DatagramPacket packet = new DatagramPacket(container,0,container.length);
server.receive(packet);
byte[] datas = packet.getData();
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(
new ByteArrayInputStream(datas)));
//顺序与读取一致,不然会报EOFException错误
int a = ois.readChar();
String s = ois.readUTF();
Object str = ois.readObject();
Object date = ois.readObject();
Object d = ois.readObject();
System.out.println();
if(str instanceof String){
String sObject = (String)str;
System.out.println(sObject);
}
if(date instanceof Date){
Date dateObject = (Date)date;
System.out.println(dateObject);
}
if(d instanceof Dog){
Dog dogObject = (Dog) d;
System.out.println(dogObject.getColoer()+"---"+dogObject.shout());
}
server.close();
}
用的是IO流的对象流
文件、图片:
//发送端
public static void clientFile() throws Exception {
DatagramSocket client = new DatagramSocket(8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//最多只能操作1024*60的数据,太大会报错
byte[] datas =TestButtJoint.fileToByteArray("src/child.jpg");
DatagramPacket packet = //下面的第四个参数是创建本机的,9999要和接收端那指定的端口一致,不然会丢失数据包
new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999));
client.send(packet);
client.close();
}
//接收端
public static void serverFile() throws Exception {
// Address already in use: Cannot bind 这个错误是由于同一个机子同一协议下端口冲突
DatagramSocket server = new DatagramSocket(9999);
byte[] container = new byte[1024*60];//最多只能操作1024*60的数据,太大会报错
DatagramPacket packet = new DatagramPacket(container,0,container.length);
server.receive(packet);
byte[] datas = packet.getData();
TestButtJoint.byteArrayToFile(datas,"src/child1.jpg");
server.close();
}
代码里的TestButtJoint是IO流的对接流,用的里面的两个方法。