通过Udp协议实现Json数据收发

本白在入职新公司接到的第一个任务需求,厂家已经写好了一个Udp监听程序,让我给被监听的ip地址和端口发送几个参数要以Json的形式,特来记录一下!

1.首先写一段测试代码,模拟服务器端和客户端实现数据收发

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//服务器端,实现接收数据并回传数据
public class TestServer {

 private DatagramSocket datagramSocket;

 private final int port = 1234;//指定监听的端口,由客户端向该端口发送数据
 
 public static void main(String[] args) throws SocketException {
     new TestServer ().service();
 }

 public TestServer() throws SocketException {
     datagramSocket = new DatagramSocket(port);
     System.out.println("服务器启动");
 }
 
 public String echo(String msg) {
     return "echo:" + msg;
 }

 public void service() {
     while (true) {
         try {
             DatagramPacket packet = new DatagramPacket(new byte[512], 512);
             datagramSocket.receive(packet);
             String msg = new String(packet.getData(), 0, packet.getLength());
             //输出客户端的IP,端口,发送的内容
             System.out.println(packet.getAddress() + "/" + packet.getPort() + " msg:" + msg);
             //这里会将接收到的数据直接发回去,可以换成Json数据,类似于下面客户端发送的数据
             packet.setData(echo(msg).getBytes());
             datagramSocket.send(packet);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 }
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

//客户端,实现发送数据并接收数据
public class TestClient {
    private String remoteHost = "xx.xx.xx.xx";//服务器端的IP地址
    private int remotePort = 1234;//服务器监听的端口,即向该端口发送数据
    private DatagramSocket datagramSocket;

    public TestClient() throws SocketException {
        datagramSocket = new DatagramSocket();
    }

    public static void main(String[] args) throws SocketException {
        new TestClient().talk();
    }

    public void talk() {
        try {
            InetAddress address = InetAddress.getByName(remoteHost);
            将数据包装成Json字符串
            JsonObject object = new JsonObject();
            object.addProperty("msgId", "1111");
            object.addProperty("msgType", 111);
            object2.addProperty("LightMode", 1);
            object2.addProperty("Brightness", 10);
            String content = object.toString();
            System.out.println(content);//将发送的数据打印在控制台观察
//发送数据
            byte[] buffer = content.getBytes();
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, remotePort);
            datagramSocket.send(packet);
//接收数据
            DatagramPacket inputPacket = new DatagramPacket(new byte[512], 512);
            datagramSocket.receive(inputPacket);
            System.out.println(new String(inputPacket.getData(), 0, inputPacket.getLength()));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            datagramSocket.close();
        }
    }
}

包装成Json数据需要添加jar包,版本可自己选择:gson-2.8.0.jar

2.记录一下遇到的问题

  1. 在包装Json数据的时候,利用的是addProperty(property,value)方法,property名称打错,例如应该是msgId,写成了msgID
  2. IP要写正确
  3. 厂家那边的数据回传是调用我写的另一个http接口,get还是post方式一定要与厂家一致

你可能感兴趣的:(通过Udp协议实现Json数据收发)