使用Socket类的SendTo方法与ReceiveFrom方法即可实现UDP端口扫描 ,亲测有效。具体可以自行搜索两个方法的作用。
//扫描UDP端口
public void scanUdp(object X)
{
IPEndPoint udp = X as IPEndPoint;
IPAddress ip = udp.Address;
int p = udp.Port;
string data = "HELLO WORLD!";
byte[] buffer = Encoding.ASCII.GetBytes(data);
try
{
byte[] buffer2 = new byte[1024];
Socket UDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UDP.SendTo(buffer, udp);
EndPoint remoteEndPoint = udp;
UDP.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
try
{
UDP.ReceiveFrom(buffer2, ref remoteEndPoint);
AddText1("IP: "+ip.ToString()+"的 "+ p+ " 号UDP端口开放\r\n");
}
catch (Exception e)
{
AddText1("IP: " + ip.ToString() + "的 " + p + " 号UDP端口未开放\r\n");
}
}
catch
{
}
}