Με while loop του παρακάτω κώδικα (while (processWorking == true)...)
private const int listenPort = 514;
class UdpState
{
public UdpClient u;
public IPEndPoint e;
}
public static void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
Byte[] receiveBytes = u.EndReceive(ar, ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
LogMessageToFile(receiveString);
Console.WriteLine("Received:" + receiveString);
ProcessSocketData(receiveString);
//Console.WriteLine("Received: {0}", receiveString);
messageReceived = true;
}
private static void StartListener()
{
bool processWorking = true;
IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
while (processWorking == true)
{
try
{
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
}
catch (Exception ex)
{
Console.WriteLine("Exception:" + ex);
}
Console.Write(".");
Thread.Sleep(100);
}
}
void main(){
StartListener();
}
έχουμε exception:
an operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
Πως μπορούμε να αποφύγουμε να γεμίσει το buffer;