Ticket #6168: syslogd.cs

File syslogd.cs, 1.0 KB (added by andreasf, 10 years ago)

test case: simple receiver as alternative to a real syslogd

Line 
1// Copyright (c) 2010 Andreas Faerber <andreas.faerber@web.de>
2// All rights reserved. Distributed under the terms of the MIT License.
3
4using System;
5using System.Net;
6using System.Net.Sockets;
7using System.Text;
8
9class Test {
10 static void Main() {
11 UdpClient client = new UdpClient(514);
12 client.EnableBroadcast = true;
13 Console.WriteLine("{0}", client.Client.LocalEndPoint);
14 while (true) {
15 IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
16 byte[] data = client.Receive(ref ep);
17 int n = -1;
18 for (int i = 0; i < data.Length - 3; i++) {
19 if (data[i] == 0xEF && data[i + 1] == 0xBB && data[i + 2] == 0xBF) {
20 n = i;
21 break;
22 }
23 }
24 if (n >= 0) {
25 if (false) {
26 string prefix = Encoding.ASCII.GetString(data, 0, n);
27 Console.Write("{0}", prefix);
28 }
29 n += 3;
30 string message = Encoding.UTF8.GetString(data, n, data.Length - n);
31 Console.WriteLine("{0}", message);
32 } else {
33 string message = Encoding.ASCII.GetString(data);
34 Console.WriteLine("{0} von {1}", message, ep);
35 }
36 }
37 }
38}