Θέλω να φτιάξω ένα .Net Service σε C# που να ακούει την πόρτα 514 UDP για εισερχόμενα μηνύματα syslog από άλλο server.. O παρακάτω κώδικας υλοποιεί μέχρι το σημείο που λαμβάνεται η πληροφορία από το ανοιχτό socket...θα ήθελα παρατηρήσεις και σχόλιά σας καθώς το αναπτύσσω χωρίς ιδιαίτερη γνώση σε αυτό τον τομέα και δεν ξέρω αν κινούμαι σωστά
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace SyslogService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
this.ServiceName = "SyslogServiceUDP514";
this.CanStop = true;
this.CanPauseAndContinue = true;
this.AutoLog = true;
}
//public void Listen(int backlog );
public static string GetStringFromSocket(Socket s)
{
Byte[] recBytes = new Byte[10001];
int i = 0;
string st = null;
if (s.Available > 0)
{
i = s.Receive(recBytes, 0, s.Available, SocketFlags.None);
char[] ac = Encoding.UTF8.GetChars(recBytes, 0, i);
foreach (char c in ac)
{
st += c.ToString();
}
}
return st;
}
protected override void OnStart(string[] args)
{
int port = 514;
string SocketData = null;
// create the socket
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
// bind the listening socket to the port
IPAddress hostIP = (Dns.GetHostEntry(IPAddress.Any.ToString())).AddressList[0];
IPEndPoint ep = new IPEndPoint(hostIP, port);
listenSocket.Bind(ep);
// start listening
listenSocket.Listen(5000);//5000 maximum connections
while (true) {
try {
SocketData = GetStringFromSocket(listenSocket.Accept());
//listenSocket.Receive();
}
catch (Exception ex) {
}
}
}
protected override void OnStop()
{
}
}
}