{"id":54,"date":"2005-07-31T08:30:49","date_gmt":"2005-07-31T13:30:49","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/php\/54"},"modified":"2010-01-19T23:11:51","modified_gmt":"2010-01-20T04:11:51","slug":"php-to-tcp-socket-form","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/PHP\/54\/","title":{"rendered":"PHP to  TCP Socket Form"},"content":{"rendered":"<p>This PHP program generates an HTML form which you can use to create a TCP socket to a remote device and send data to it.  This assumes the remote device accepts the socket connection on the port you choose to connect to.<\/p>\n<p>Thanks to John Schimmel for writing the code.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/networked objects\" rel=\"tag\">networked objects<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/networks\" rel=\"tag\">networks<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<pre class=\"brush:php; light:true\">\r\n[include file=\"..\/..\/code\/php\/phpToSocketForm.php\" clean=\"true\"]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This assumes the remote device accepts the socket connection on the port you choose to connect to.Thanks to John Schimmel for writing the code.&lt;?php\/*by John Schimmel, john.schimmel@gmail.comMay 9, 2005This script will display a html form.  On submit, the form will call itself and then check to see if the form element &#8216;text1&#8217; is set. If set a socket is made to the define IP address and port and the value of &#8216;text1&#8217; is sent down the pipe.The socket then disconnects and the form is redisplayed with the value of &#8216;text1&#8242; in the html form&#8217;s textbox.IMPORTANT: If you are using an XPORT or COBOX change the connect mode to C4so no IP address is being concatenating with the incoming message to the pic&#8230;.unless you like it that way.*\/$xportIP = &#8220;128.122.151.44&#8221;; \/\/IP adddress to connect to$port = 10001; \/\/ port number of IPif ((isset($_POST[&#8220;text1&#8221;])) &#38;&#38; (isset($_POST[&#8220;ip&#8221;])) &#38;&#38; (isset($_POST[&#8220;port&#8221;]))) { \/\/if a filled textbox was submitted $data = $_POST[&#8220;text1&#8221;]; $ip = $_POST[&#8220;ip&#8221;]; $port = $_POST[&#8220;port&#8221;]; $fp = fsockopen ($ip, $port, $errno, $errstr, 30); \/\/open the socket if (!$fp) { \/\/if the socket does not exist echo &#8220;$errstr ($errno)\\n&#8221;; } else { \/\/if the socket exists fputs ($fp, $data); \/\/ send the data down the hole to the other end echo &#8220;&lt;font size=&#8217;2&#8242; color=&#8217;navy&#8217;&gt;Message Sent: &#8221; .<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-PHP"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":3,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":814,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/54\/revisions\/814"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}