请教udp端口扫描的程序如何写?(最好由源码,不胜感激)

hfrabbit 2003-06-05 06:09:11
我写的扫描程序不稳定,对超过两跳的主机扫描总是有问题,扫描算法如下:
void TScanUdpPort::ScanPort()
{
struct sockaddr_in addr_dest,addr_src;
unsigned int h_socket,h_sock;
int state;
char *recv_buf;
char *udp_data="1234";
int timeout=1000;

if((h_socket = socket( PF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("create socket error.\n");
}

state = setsockopt(h_socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
if(state == SOCKET_ERROR)
{
printf("error set send sockopt.\n");
}
state = setsockopt(h_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
if(state == SOCKET_ERROR)
{
printf("error set send sockopt.\n");
}

memset(&addr_dest,0,sizeof(addr_dest));
addr_dest.sin_family = AF_INET;
addr_dest.sin_addr.s_addr = inet_addr( lp_info->addr_dest);

recv_buf = (char*)malloc(MAX_PACKET);
memset(recv_buf,0, MAX_PACKET);
memset(&addr_src,0,sizeof(addr_src));

for(int i=0; i< lp_info->udpport.portsize; i++)
{
printf(" the port num = %d <--->",lp_info->udpport.port[i]);
addr_dest.sin_port = htons( lp_info->udpport.port[i]);

state = sendto( h_socket, udp_data, sizeof(udp_data), 0, (struct sockaddr*)&addr_dest, sizeof(addr_dest));
if( state == SOCKET_ERROR)
{
printf("send message error.\n");
continue;
}

int len = sizeof(addr_src);

state = recvfrom(h_socket, recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&addr_src,&len);
if(GetLastError() == WSAECONNRESET)
printf(" this port is closed.\n");
if(GetLastError() == WSAETIMEDOUT )
printf(" this port is opening.\n");
}
}
...全文
98 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
通信技术
创建于2007-08-27

2790

社区成员

2.8w+

社区内容

通信技术相关讨论
社区公告
暂无公告