syslog客户端java实现

  1 //package com.tony.util;
  2 import java.io.*;    
  3 import java.net.*;    
  4     
  5 /**
  6  * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息.   
  7  * Title: SyslogClient
  8  * Description: none
  9  * Copyright @ 2012~2015 HP<br>
 10  * @author Tony
 11  * @createDate 2015年8月18日
 12  * @version v1.0
 13  */
 14 public class SyslogClient {    
 15     private byte[] buffer = new byte[1024];    
 16     
 17     private DatagramSocket ds = null;    
 18     
 19     /**  
 20      * 构造函数,创建UDP客户端  
 21      * @throws Exception  
 22      */    
 23     public SyslogClient() throws Exception {    
 24         ds = new DatagramSocket();    
 25     }    
 26         
 27     /**  
 28      * 设置超时时间,该方法必须在bind方法之后使用.  
 29      * @param timeout 超时时间  
 30      * @throws Exception  
 31      */    
 32     public final void setSoTimeout(final int timeout) throws Exception {    
 33         ds.setSoTimeout(timeout);    
 34     }    
 35     
 36     /**  
 37      * 获得超时时间.  
 38      * @return 返回超时时间  
 39      * @throws Exception  
 40      */    
 41     public final int getSoTimeout() throws Exception {    
 42         return ds.getSoTimeout();    
 43     }    
 44     
 45     public final DatagramSocket getSocket() {    
 46         return ds;    
 47     }    
 48  
 49     /**  
 50      * 接收从指定的服务端发回的数据.  
 51      * @param lhost 服务端主机  
 52      * @param lport 服务端端口  
 53      * @return 返回从指定的服务端发回的数据.  
 54      * @throws Exception  
 55      */    
 56     public final String receive(final String lhost, final int lport)    
 57             throws Exception {    
 58         DatagramPacket dp = new DatagramPacket(buffer, buffer.length);    
 59         ds.receive(dp);    
 60         String info = new String(dp.getData(), 0, dp.getLength());    
 61         return info;    
 62     }    
 63     
 64     /**  
 65      * 关闭udp连接.  
 66      */    
 67     public final void close() {    
 68         try {    
 69             ds.close();    
 70         } catch (Exception ex) {    
 71             ex.printStackTrace();    
 72         }    
 73     }   
 74     
 75     /**  
 76      * 向指定的服务端发送数据信息.  
 77      * @param host 服务器主机地址  
 78      * @param port 服务端端口  
 79      * @param bytes 发送的数据信息  
 80      * @return 返回构造后俄数据报  
 81      * @throws IOException  
 82      */    
 83     public final DatagramPacket send(final String host, final int port,    
 84             final byte[] bytes) throws IOException {    
 85         DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress    
 86                 .getByName(host), port);    
 87         ds.send(dp);    
 88         return dp;    
 89     }
 90     
 91     /**  
 92      * 测试客户端发包和接收回应信息的方法.  
 93      * @param args  
 94      * @throws Exception  
 95      */    
 96     public static void main(String[] args) throws Exception {    
 97         SyslogClient client = new SyslogClient();   
 98         //这里我们在本机测试,使用本机IP即可
 99         String serverHost = "192.168.2.71";    
100         int serverPort = 514;  
101         int i = 0;
102         while (true){
103             String log = "test syslog --- " + i++;
104             client.send(serverHost, serverPort, log.getBytes());   
105             Thread.sleep(1000);
106         }
107      //   String info = client.receive(serverHost, serverPort);    
108      //   System.out.println("服务端回应数据:" + info);
109     }    
110 }  

通过wireshark抓包:

原文地址:https://www.cnblogs.com/porkerface/p/13048456.html