私はUDPにBeginReceiveを使用する方法を学ぼうとしています.これが私が持っているものです:
Console.WriteLine("Initializing SNMP Listener on Port:" + port + "...");
UdpClient client = new UdpClient(port);
//UdpState state = new UdpState(client, remoteSender);
try
{
client.BeginReceive(new AsyncCallback(recv), null);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void recv(IAsyncResult res)
{
int port = 162;
UdpClient client = new UdpClient(port);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 162);
byte[] received = client.EndReceive(res, ref RemoteIpEndPoint);
Console.WriteLine(Encoding.UTF8.GetString(received));
client.BeginReceive(new AsyncCallback(recv), null);
}
何も起こらず、コードは recv メソッドを呼び出すことさえせずに終了します。何故ですか ?
編集:
追加した :-
Console.ReadLine();
今、それは私に以下の行で例外を与えています:
Only one usage of each socket address is normally permitted.
試した:-
try
{
client.BeginReceive(new AsyncCallback(recv), client);
}
private static void recv(IAsyncResult res)
{
// int port = 162;
try
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 162);
byte[] received = res.AsyncState.EndReceive(res, ref RemoteIpEndPoint);
Console.WriteLine(Encoding.UTF8.GetString(received));
res.AsyncState.BeginReceive(new AsyncCallback(recv), null);
}
catch (Exception e)
{
Console.WriteLine(e);
}
エラー:
'object' does not contain a definition for 'EndReceive' and no extension method 'EndReceive' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)