Wrote two small programs, mainly for Linux and Windows under the TCP port detection, my own telnet can not meet the needs of my batch detection, in my eyes this type of port detection program is the most critical is the time-out limit, if the port can not be old long before returning results, a bit uncomfortable, In the premise of not changing the system default timeout time, the introduction of the socket non-blocking mode to achieve the purpose of the timeout limit, the following is the specific program
1. TCP Port Detection program under Windows
Introduction: Introducing Select Mode as a timeout limit
1 //TCP Port Check Program in Windows2 3 4#include <stdio.h>5#include <winsock2.h>6 7 #pragmaComment (lib, "Ws2_32.lib")8 9 Ten intMainintargcChar*argv[]) One { A SOCKET sockfd; - sockaddr_in sockaddr; - intPort; theUnsignedLongIP; - wsadata WSA; - inttimeout=2, ret; - structTimeval TV; + structFd_set FS; -UnsignedLongUL =1; + A at if(ARGC! =4) - { -printf"Usage:%s IP Port timeout\n", argv[0]); - return-1; - } - if((Ip=inet_addr (argv[1]))==Inaddr_none) in { -printf"IP Address error\n"); to return-1; + } - if((Port=atoi (argv[2]))==0) the { *printf"Port error\n"); $ return-1;Panax Notoginseng } - if((Timeout=atoi (argv[3]))==0) the { +printf"Timeout error\n"); A return-1; the } +WSAStartup (Makeword (1,1), &WSA);//Initialize Ws2_32.dll - $ if((SOCKFD = socket (af_inet, Sock_stream, ipproto_tcp)) <=0)//Create a TCP socket $ { -printf"Create Socket fail!\n"); - return-2; the } - //socket Non-block Mode setWuyi if(ret = ioctlsocket (SOCKFD, Fionbio, (unsignedLong*) &ul) = =socket_error) the { - closesocket (SOCKFD); Wu return-1; - } Aboutsockaddr.sin_family =af_inet; $Sockaddr.sin_port =htons (port); -Sockaddr.sin_addr. S_un. S_ADDR = inet_addr (argv[1]); - - //connect to target IP with Port AConnect (SOCKFD, (SOCKADDR *) &sockaddr,sizeof(SOCKADDR)); + theTv.tv_sec =timeout; -Tv.tv_usec =0; $Fd_zero (&FS); theFd_set (sockfd,&FS); theRET =Select(sockfd+1,null,&fs,null,&TV); the if(ret<0) the { -printf"Select error\n"); in closesocket (SOCKFD); the return-1; the } About Else if(ret = =0) the { theprintf"%s%d Connect fail!\n", argv[1],port); the closesocket (SOCKFD); + return-1; - } the ElseBayi { theprintf"%s%d Connected success!\n", argv[1],port); the closesocket (SOCKFD); - } -WSACleanup ();//Clean up Ws2_32.dll the return 0; the}
2. TCP port detection program under Linux
#include <stdlib.h>#include<sys/socket.h>#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<string.h>#include<netdb.h>#include<time.h>#include<errno.h>intMainintargcChar*argv[]) { intSockfd=socket (Af_inet,sock_stream,0); structsockaddr_in Client; Charip[ -]; intPort; inttimeout; structTimeval TV; if(ARGC! =4) {printf ("Usage:%s IP Port timeout\n", argv[0]); Exit (1); } strcpy (IP, argv[1]); Port= Atoi (argv[2]); Timeout= Atoi (argv[3]); Bzero (&client,sizeof(structsockaddr_in)); Client.sin_family=af_inet; Client.sin_addr.s_addr=inet_addr (IP); Client.sin_port=htons (port); Tv.tv_sec=5; Tv.tv_usec=0; //set timeout with setsockopt if(SetSockOpt (Sockfd,sol_socket,so_rcvtimeo, (Char*) &TV,sizeof(TV)) <0) {perror ("setsockopt failed\n"); Exit (1); } if(SetSockOpt (Sockfd,sol_socket,so_sndtimeo, (Char*) &TV,sizeof(TV)) <0) {perror ("setsockopt failed\n"); Exit (1); } if(!connect (SOCKFD, (structsockaddr*) &client,sizeof(structsockaddr_in))) {printf ("Connect ok\n"); }Else{printf ("Connect fail\n"); } close (SOCKFD); return 0;}
3. Summary
Code is not difficult, the key is to improve efficiency, sometimes simple things better, continue to work hard
TCP Socket Port Check