{"id":622,"date":"2015-04-12T00:57:41","date_gmt":"2015-04-12T07:57:41","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=622"},"modified":"2015-04-12T00:59:33","modified_gmt":"2015-04-12T07:59:33","slug":"using-popen-to-get-stdout","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=622","title":{"rendered":"Using popen to get stdout"},"content":{"rendered":"<p>\n\tThis is an example to check connection state for a specific port number. It returns command output.\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush:cpp;\">\r\nvoid* check_connection_state(void *data)\r\n{\r\n    uint32_t counter = 0;\r\n    char buf[512] = {0};\r\n    FILE *p = popen(&quot;netstat -an | grep -i PORT_NUM&quot;, &quot;r&quot;);\r\n    while(counter &gt; 120 || (fread(buf, 1, sizeof(buf), p) != 0))\r\n    {\r\n        counter++;\r\n        pclose(p);\r\n        sleep(1);\r\n        p = popen(&quot;netstat -an | grep -i PORT_NUM&quot;, &quot;r&quot;);\r\n    }\r\n    pthread_exit(0);\r\n}\r\n\r\nint main(int argc, char* argv[])\r\n{\r\n    \/\/ Check connection\r\n    pthread_t check_connection_thread;\r\n    pthread_create (&amp;check_connection_thread, NULL, check_connection_state, NULL);\r\n    pthread_join(check_connection_thread, NULL);\r\n    \r\n    return 0;\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an example to check connection state for a specific port number. It returns command output. &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-cc"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=622"}],"version-history":[{"count":2,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":624,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions\/624"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}