【发布时间】:2020-03-30 21:21:43
【问题描述】:
我正在尝试通过以太网(从控制器)接收 UDP 数据并遇到了一些问题。我知道控制器正在发送数据,因为我可以在wireshark 上看到它通过,但我尝试过的所有事情都没有奏效。下面的代码是我发现能够接收我想要的数据的最接近的代码。 更多信息:控制器IP和端口是192.168.82.27:1743,我端接收IP和端口是192.168.82.21:1740
public class UDPListener
{
static UdpClient client = new UdpClient(1740);
public static void Main()
{
try
{
client.BeginReceive(new AsyncCallback(recv), null);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
while (true)
{
}
}
//CallBack
private static void recv(IAsyncResult res)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 1743);
byte[] received = client.EndReceive(res, ref RemoteIpEndPoint);
//Process code
Console.WriteLine(RemoteIpEndPoint + " : " + Encoding.ASCII.GetString(received));
client.BeginReceive(new AsyncCallback(recv), null);
}
}
【问题讨论】:
标签: c# visual-studio udp ethernet udpclient