{"id":565,"date":"2011-01-28T00:30:38","date_gmt":"2011-12-27T05:51:50","guid":{"rendered":"http:\/\/kraftek.com\/blog\/socktest\/"},"modified":"2011-01-28T00:30:38","modified_gmt":"2011-12-27T05:51:50","slug":"socktest","status":"publish","type":"post","link":"\/blog\/index.php\/socktest\/","title":{"rendered":"socktest"},"content":{"rendered":"<p>Have you ever wanted to test a connection to only one port, and found nmap overkill, and you like to do it in C?<br \/>\nNormally I&#8217;d recommend something like<br \/>\necho &#8220;~.&#8221; | telnet -r $H $X 2&gt;&amp;1| grep -i connected | wc -l | sed &#8216;s\/ \/\/g&#8217;`<br \/>\nBut, if the host doesn&#8217;t answer the default tcp\/ip timeout for a socket is 3 minutes!!!! You have to wait long<br \/>\ntime&#8230;<br \/>\nYou don&#8217;t want to wait too long for that, so you can use a program to test a socket connection, like socktest.c,<br \/>\nit tries to connect and if it can&#8217;t within 5 seconds it will bail out.<br \/>\nyou should compile with gcc, if using solaris &#8220;gcc -o socktest -lsocket -lnsl socktest.c&#8221; will suffice.<br \/>\nSo, for instance you want to test port 80 and port 44 in google.com:<br \/>\nbash-3.00$ .\/socktest google.com 80 ; echo $?<br \/>\nConnection to port successful<br \/>\nbash-3.00$ .\/socktest google.com 44 ; echo $?<br \/>\nERROR: Timeout or error() 4 &#8211; Interrupted system call<br \/>\n1<br \/>\nYou can get the exit code 0 as successful, 1 as not successful.<br \/>\nSource here:<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;netdb.h&gt;<br \/>\n#include &lt;errno.h&gt;<br \/>\n#include &lt;sys\/fcntl.h&gt;<br \/>\n\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\/\/ Roberto Dircio Palacios-Macedo<br \/>\n\/\/ socktest &#8211; program to test a connection<br \/>\n\/\/ to a host in a specific port<br \/>\n\/\/ usage &#8211; .\/socktest &lt;host&gt; &lt;port&gt;<br \/>\n\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nvoid error(char *msg)<br \/>\n{<br \/>\nperror(msg);<br \/>\nexit(0);<br \/>\n}<br \/>\nint main(int argc, char *argv[])<br \/>\n80\/433<br \/>\n{<br \/>\nint res,valopt;<br \/>\nstruct timeval tv;<br \/>\nfd_set myset;<br \/>\nsocklen_t lon;<br \/>\nint soc, portno, n;<br \/>\nstruct sockaddr_in serv_addr;<br \/>\nstruct hostent *server;<br \/>\nchar buffer[256];<br \/>\nif (argc &lt; 3) {<br \/>\nfprintf(stderr,&#8221;usage %s hostname portn&#8221;, argv[0]);<br \/>\nexit(1);<br \/>\n}<br \/>\nportno = atoi(argv[2]);<br \/>\nserver = gethostbyname(argv[1]);<br \/>\nsoc = socket(AF_INET, SOCK_STREAM, 0);<br \/>\nif (soc &lt; 0) {<br \/>\nerror(&#8220;ERROR: opening socket&#8221;);<br \/>\nexit(1);<br \/>\n}<br \/>\nif (server == NULL) {<br \/>\nfprintf(stderr,&#8221;ERROR: no such hostn&#8221;);<br \/>\nexit(1);<br \/>\n}<br \/>\nbzero((char *) &amp;serv_addr, sizeof(serv_addr));<br \/>\nserv_addr.sin_family = AF_INET;<br \/>\nbcopy((char *)server-&gt;h_addr,<br \/>\n(char *)&amp;serv_addr.sin_addr.s_addr,<br \/>\nserver-&gt;h_length);<br \/>\nserv_addr.sin_port = htons(portno);<br \/>\nlong arg = fcntl(soc, F_GETFL, NULL);<br \/>\narg |= O_NONBLOCK;<br \/>\nfcntl(soc, F_SETFL, arg);<br \/>\nres=connect(soc, &amp;serv_addr,sizeof(serv_addr));<br \/>\nif (res &lt; 0) {<br \/>\nif (errno == EINPROGRESS) {<br \/>\ntv.tv_sec = 5;<br \/>\ntv.tv_usec = 0;<br \/>\nFD_ZERO(&amp;myset);<br \/>\nFD_SET(soc, &amp;myset);<br \/>\nif (select(soc+1, NULL, &amp;myset, NULL, &amp;tv) &gt; 0) {<br \/>\nlon = sizeof(int);<br \/>\ngetsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&amp;valopt) , &amp;lon);<br \/>\nif (valopt) {<br \/>\nfprintf(stderr, &#8220;ERROR: Error in connection() %d &#8211; %sn&#8221;, valopt, strerror(valopt) );<br \/>\nexit(1);<br \/>\n}<br \/>\n}<br \/>\nelse {<br \/>\nfprintf(stderr, &#8220;ERROR: Timeout or error() %d &#8211; %sn&#8221;, valopt, strerror(valopt) );<br \/>\nexit(1);<br \/>\n}<br \/>\n}<br \/>\n81\/433<br \/>\nelse {<br \/>\nfprintf(stderr, &#8220;ERROR: Error connecting %d &#8211; %sn&#8221;, errno, strerror(errno)) ;<br \/>\nexit(1);<br \/>\n}<br \/>\n}<br \/>\narg = fcntl(soc, F_GETFL, NULL);<br \/>\narg &amp;= (~O_NONBLOCK) ;<br \/>\nfcntl(soc, F_SETFL, arg);<br \/>\nfprintf(stdout, &#8220;Connection to port successfuln&#8221;);<br \/>\nexit(0);<br \/>\n}<br \/>\nEnjoy!<br \/>\n82\/433<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to test a connection to only one port, and found nmap overkill, and you like to do it in C? Normally I&#8217;d recommend something like echo &#8220;~.&#8221; | telnet -r $H $X 2&gt;&amp;1| grep -i connected | wc -l | sed &#8216;s\/ \/\/g&#8217;` But, if the host doesn&#8217;t answer the default [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-565","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=565"}],"version-history":[{"count":0,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/565\/revisions"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}