title: PHP - Socket connection - Send commands to a server from an input form
body: <p>I'm trying to connect to a server using php socket. I'm using the following php code which works fine:</p>
  
  <pre><code>&lt;?php
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  $host = "192.168.1.2";
  $port=15000;
  
  settype($buffer,"string");
  
  //$buffer="0 MAIN VERSION\0"; 
  
  $len= strlen($buffer);
  
  socket_connect($sock, $host, $port);
  socket_sendto($sock, $buffer, $len, 0 , $host, $port);
  
  
  echo( socket_read($sock, 65535) );
  
  socket_close($sock);
  ?&gt;
  </code></pre>
  
  <p>I want to create a form and input the $buffer variable from that form and then process it.
  The problem is that the socket connection does not get connected nor I receive any error. 
  Here is the form code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;TEST CMD&lt;/title&gt;
  
  &lt;form action="send_cmd.php" method="POST" &gt;
  
  COMMAND:&lt;input type="text" name="cmd" size="70"&gt;
  
  &lt;input type="submit" value="send cmd"&gt;
  
  &lt;/form&gt; 
  &lt;/head&gt;
  </code></pre>
  
  <p>And here is the send_cmd.php:</p>
  
  <pre><code>&lt;?php
  
  settype($buffer,"string");
  
  $buffer= $_POST["cmd"];
  
  $len= strlen($buffer);
  
  ECHO $buffer;
  ECHO $len;
  
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('ERROR');
  $host = "192.168.1.2";
  $port=15000;
  socket_connect($sock, $host, $port) or die('ERROR');;
  socket_sendto($sock, $buffer, $len, 0 , $host, $port);
  
  echo $buffer;
  echo( "ANSWER: &lt;P&gt;".socket_read($sock, 65535) );
  
  socket_close($sock); 
  ?&gt;
  </code></pre>
  
  <p>What is the problem? Should be working but it doesn't. Thanks!</p>