1. 在terminal以curl指令傳送json格式單次封包 至GELP HTTP input
=> (port 設為 12202)
ex. curl -XPOST http://140.116.163.151:12202/gelf -d ‘{“short_message”:”trytryRS”}’
ex. curl -XPOST http://140.116.163.151:12202/gelf -d ‘{“short_message”:”trytryRS”, “host”:”rongson”, “facility”:”rongsons mac”, “_test”:”hello”}’
2. 以tcp client傳送單次plaintext封包
=> (port 12203)
import java.io.*; import java.net.*; class TcpClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); Socket clientSocket = new Socket("140.116.163.151", 12203); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); System.out.print("Input: "); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); System.out.println("OVER"); //modifiedSentence = inFromServer.readLine(); //System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }
3. udp client傳送單次plaintext封包
=> (port 12204)
import java.io.*; import java.net.*; // 1. 本程式必須與 UdpServer.java 程式搭配執行,先執行 UdpServer 再執行本程式。 // 2. 本程式必須有一個參數,指定伺服器的 IP。 // 用法範例: java UdpClient 127.0.0.1 public class udpClient extends Thread { int port; // port : 連接埠 InetAddress server; // InetAddress 是 IP, 此處的 server 指的是伺服器 IP String msg; // 欲傳送的訊息,每個 UdpClient 只能傳送一個訊息。 public static void main(String args[]) throws Exception { for (int i=0; i<100; i++) { // 建立 UdpClient,設定傳送對象與傳送訊息。 udpClient client = new udpClient(args[0], 12204, "UdpClient : "+i+"th message"); client.run(); // 啟動 UdpClient 開始傳送。 } } public udpClient(String pServer, int pPort, String pMsg) throws Exception { port = pPort; // 設定連接埠 server = InetAddress.getByName(pServer); // 將伺服器網址轉換為 IP。 msg = pMsg; // 設定傳送訊息。 } public void run() { try { byte buffer[] = msg.getBytes(); // 將訊息字串 msg 轉換為位元串。 // 封裝該位元串成為封包 DatagramPacket,同時指定傳送對象。 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, server, port); DatagramSocket socket = new DatagramSocket(); // 建立傳送的 UDP Socket。 socket.send(packet); // 傳送 socket.close(); // 關閉 UDP socket. } catch (Exception e) { e.printStackTrace(); } // 若有錯誤產生,列印函數呼叫堆疊。 } }
4. 以Java TCPClient 寫入Json格式 傳送到 graylog GELF TCP input
import java.io.*; import java.net.*; import org.json.*; class TcpClientJson { private String host; private int port; private Socket socket; private final String DEFAULT_HOST = "localhost"; public void connect(String host, int port) throws IOException { this.host = host; this.port = port; socket = new Socket(host, port); System.out.println("Client has been connected.."); } public JSONObject receiveJSON() throws IOException { InputStream in = socket.getInputStream(); ObjectInputStream i = new ObjectInputStream(in); JSONObject line = null; try { line = (JSONObject) i.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return line; } public void sendJSON() throws IOException { JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("RS", "Hello Java Json"); jsonObject2.put("host", "RS"); jsonObject2.put("facility", "test"); jsonObject2.put("_foo","bar"); jsonObject2.put("short_message","hello"); OutputStream out = socket.getOutputStream(); DataOutputStream o = new DataOutputStream(out); System.out.println("Hello sendJson"); o.writeBytes(jsonObject2.toString()+'\0'); System.out.println(jsonObject2.toString()); out.flush(); //System.out.println("Sent to server: " + " " + jsonObject2.get("key").toString()); } public static void main(String argv[]) throws Exception { TcpClientJson client = new TcpClientJson(); try{ client.connect("140.116.221.54", 12299); // For JSON call sendJSON(JSON json) & receiveJSON(); client.sendJSON(); //client.receiveJSON(); } catch (ConnectException e) { System.err.println(client.host + " connect refused"); return; } catch(UnknownHostException e){ System.err.println(client.host + " Unknown host"); client.host = client.DEFAULT_HOST; return; } catch (NoRouteToHostException e) { System.err.println(client.host + " Unreachable"); return; } catch (IllegalArgumentException e){ System.err.println(client.host + " wrong port"); return; } catch(IOException e){ System.err.println(client.host + ' ' + e.getMessage()); System.err.println(e); } finally { try { client.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getUserString(){ BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Input: "); String sentence="hello"; try{ sentence = inFromUser.readLine(); }catch(IOException e){ System.out.println(e); } return sentence; } }
* 記得在java tcpclient端傳送前,要在結尾加入+’\0’,否則graylog gelf tcp input會收到流量,但不會顯示資料
參考:https://github.com/Graylog2/graylog2-server/issues/643
還有part.2
http://rongson.twbbs.org/wordpress/sending-differen…g-server(part-2)/