#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
#define ZERO (fd_set *)0
int iptoint( const char *ip )
{
return ntohl( inet_addr( ip ) );
}
int inttoip( int ip_num, char *ip )
{
strcpy( ip, (char *) inet_ntoa( htonl( ip_num ) ) );
}
int connect_test( const char *proxy, int port )
{
int sock;
struct sockaddr_in s_addr;
struct fd_set mask;
struct timeval timeout;
unsigned long flag = 1;
/*创建套接字*/
sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock == -1 )
{
return 0;
}
s_addr.sin_family=AF_INET;
s_addr.sin_addr.s_addr = htonl( iptoint( proxy ) ); // 要扫描的地址
s_addr.sin_port = htons( port ); // 要扫描的端口
/*调用ioctlsocket()设置套接字为非阻塞模式*/
if ( fcntl( sock, F_SETFL, &flag ) == -1 )
{
return 0;
}
/*调用connect()连接远程主机端口*/
connect( sock, ( struct sockaddr* ) &s_addr, sizeof( s_addr ) );
timeout.tv_sec = 5; // 超时限制为18秒
timeout.tv_usec = 0;
FD_ZERO( &mask ); // 清空集合mask
FD_SET( sock, &mask ); // 将sock放入集合mask中
int len = sizeof( int );
int error;
/*用select() 处理扫描结果*/
switch( select( sock + 1, ZERO, &mask, ZERO, &timeout ) )
{
/*select error*/
case -1:
close( sock );
return 0;
/*超时*/
case 0:
close( sock );
return 0;
/**/
default:
printf( "123\n" );
/*针对防火墙*/
getsockopt( sock, SOL_SOCKET, SO_ERROR, &error, &len );
/*关闭sock*/
close( sock );
printf( "error:%d\n", error );
if ( error == 0 )
return 1;
else
return 0;
}
}
int main()
{
struct timeval st,st1;
gettimeofday( &st, NULL );
if ( connect_test( "78.41.17.231", 880 ) )
{
printf( "连接成功\n" );
}
else
{
printf( "连接失败\n" );
}
gettimeofday( &st1, NULL );
printf( "用时:%d 秒\n", st1.tv_sec - st.tv_sec );
return 0;
}
有个问题,如果IP可以连接,但端口没有开放,程序会很快返回。但是如果IP无法连接,程序则只会在超时后返回。
阅读(2018) | 评论(1) | 转发(0) |