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抓包: