UDP工具类UDPUtil

package com.alibaba.otter.canal.utils;

import com.alibaba.otter.canal.constants.ModuleEnum;
import com.alibaba.otter.canal.custom.CanalLogUtil;

import java.io.IOException;
import java.net.*;

/**
 * @author kaikai_zheng
 * @version 1.0.0
 * @className UDPUtil
 * @description //向指定UDP地址端口发送报文
 * @data 2020-07-27 09:35
 */
public class UDPUtil {

    private static final String LOGSTASH_IP= "127.0.0.1";
    private static int PORT = 10000;

    private static DatagramSocket ds;

    public static void udpSendMssage(String data){
        byte[] bytes = data.getBytes();
        try {
            InetAddress inetAddress = InetAddress.getByName(LOGSTASH_IP);
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length,inetAddress, PORT);
            if (null == ds) {
                ds = new DatagramSocket();
            }
            ds.send(dp);
//            ds.close();
        } catch (IOException e) {
            CanalLogUtil.error(ModuleEnum.CANAL_CLIENT.getCode(),"send udp message fail,err", e);
            e.printStackTrace();
        }
    }
}

 

posted @ 2020-07-31 15:04  骑着蜗牛看海呀  阅读(603)  评论(0编辑  收藏  举报