while (true)
{
//Byte[] data = server.Receive(ref remote);
try
{
IAsyncResult aresult = server.BeginReceive(new AsyncCallback(polaczenie), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
class UdpState
{
public IPEndPoint e;
public UdpClient u;
}
private void EndReceive(IAsyncResult result)
{
UdpClient u = (UdpClient)((UdpState)(result.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(result.AsyncState)).e;
byte[] ode = u.EndReceive(result, ref e);
string info = Encoding.ASCII.GetString(ode);
SetText(e.Address.ToString() + " " + e.Port.ToString());
while (!result.IsCompleted)
{
Thread.Sleep(200);
}
u.BeginReceive(new AsyncCallback(EndReceive), new UdpState() { e = e, u = u });
}
Ten kod wyrzuca błąd 0x80004005. A jak zabiorę linijkę:
MessageBox.Show(ex.ToString());
to program nie wyrzuca błędu, ale automatycznie się zamyka. Co robię źle?
Edit1:
Jak dam:
while (true)
{
try
{
server = new UdpClient();
remote = new IPEndPoint(IPAddress.Any, 18900);
server.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
server.Client.Bind(remote);
IAsyncResult aresult = server.BeginReceive(new AsyncCallback(EndReceive), new UdpState() { e = remote, u = server });
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
to działa,ale po chwili ram jest zapchany i komputer muli.