帮朋友问,一个很小的程序

winewinder 2003-09-15 09:05:05
通过向服务器的udp1434端口发送一个内容为02的一个字节的数据包,服务器如果安装了mssql服务的话就会返回mssql的信息。
我写了这样个扫描一个c类网络的小东西,编译通过,但是运行不对,请大家帮忙!
运行的时候是这样的:
C:\>sqlscan 211.92.45
start scaning,please wait...
start sending Udp packet to 211.92.45.1...
mssql server is not installed on 211.92.45.1

start sending Udp packet to 211.92.45.2...
^C
C:\>
第一个ip地址扫描正常,但是扫描第二个就死掉了一样~~~
不知道为什么


代码如下:
//利用udp1434端口探测mssql服务
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class sqlscan
{
private static UdpClient myclient;
public static void Main(string[] args)
{
if(args.Length==0)
{
Console.WriteLine("######################################################");
Console.WriteLine("scan mssql server by udp 1434 port");
Console.WriteLine("Thanks awen who come from patching.net");
Console.WriteLine("Usage:sqlscan IP");
Console.WriteLine("Example:sqlscan 192.168.0");
Console.WriteLine("######################################################");
}
else
{
Console.WriteLine("start scaning,please wait...");
//生成c类地址
string ip = args[0];
for(int i = 1;i<=254;i++)
{
ip = args[0]+"."+i.ToString();
IPAddress host = IPAddress.Parse(ip);
IPEndPoint point;
myclient = new UdpClient();
byte[] buffer = {02};
Console.WriteLine("start sending Udp packet to "+ip+"...");
try
{
point = new IPEndPoint(host,1434);
//发送数据包
int re = myclient.Send(buffer,1,point);
byte [] data = myclient.Receive(ref point);
if(data.Length<=0)
{
Console.WriteLine("faild to send udp packet to"+ip+"...");
}
else
{
Console.WriteLine("send udp packet to "+ip+"sucess...");
//接受返回信息
String strdata = Encoding.ASCII.GetString(data);
Console.Write(strdata);
myclient.Close();
}
}
catch
{
//捕获异常
Console.WriteLine("mssql server is not installed on "+ip+"\n");
//break;
}
}
}
}
}
...全文
94 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
winewinder 2003-09-16
  • 打赏
  • 举报
回复
感谢各位
winewinder 2003-09-15
  • 打赏
  • 举报
回复
请高手多多指教啦
zhongwanli 2003-09-15
  • 打赏
  • 举报
回复
没做过,学习也是好事!
winewinder 2003-09-15
  • 打赏
  • 举报
回复
可是为什么第一个ip可以通过,只在第二个ip死住了呢?
sarcophile 2003-09-15
  • 打赏
  • 举报
回复
Receive 是阻塞调用,如果没有可读的字节,就一直不返回,要么改为异步调用,要么用多线程
树猫 2003-09-15
  • 打赏
  • 举报
回复
数据堵塞
_weiKun_ 2003-09-15
  • 打赏
  • 举报
回复
用Socket的非阻塞编程即可解决。
wolfpzp 2003-09-15
  • 打赏
  • 举报
回复
是在这句的时候停的
byte [] data = myclient.Receive(ref point);
wolfpzp 2003-09-15
  • 打赏
  • 举报
回复
我试的时候是第三个才出错

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧