title: How to send raw data to an IP utilizing php
body: <p>I´m trying to send raw data with a php script to an embedded device (Wifi Shield based on WizFi wlan module hocked up on a Arduino Leonardo).
  I can successfully send data to the device by performing: </p>
  
  <p><code>echo -n "teststring" | nc xx.xx.xx.xx 4000</code> on a unix terminal (Mac OS X) (the x represent a placeholder for the ip of the device)</p>
  
  <p>or
   using Putty (under Windows XP) with ip, port and selecting "RAW" as the connection type.</p>
  
  <p>To achieve the same I tried to establish a socket in a php script (since I´d like to send data through a web form to the device).</p>
  
  <p>see the sample code below:</p>
  
  <pre><code>$mysocket = socket_create(AF_INET, SOCK_RAW,255);  
  
  if ($mysocket === false) {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);
    die("could not establish socket: [$errorcode] $errormsg");
  }
  
  // connect to socket
  if (socket_connect($mysocket, 'xx.xx.xx.xx', 4000)) {
    $text = "Socket connection seems successful!";
  } else {
    $text = "Unable to connect&lt;pre&gt;".socket_strerror(socket_last_error())."&lt;/pre&gt;";
  }
  
  echo $text;
  
  echo "Mystring: ";
  echo $mystring;
  
  echo "try to send data";
  
  
  // send string to server
  socket_write($mysocket, $mystring, strlen($mystring)) or die("Could not send data");
  $errorcode = socket_last_error();
  $errormsg = socket_strerror($errorcode);
  
  echo "errorcode:";
  echo $errorcode;
  echo $errormsg;
  
  socket_close($mysocket);                                                     
  </code></pre>
  
  <p>The corresponding protocols for the socket create command (last integer of the arguments) are:
  (the list I copied from /etc/protocols of my server (Synology NAS).</p>
  
  <pre><code>&lt;!-- language: none --&gt;
  ip      0       IP
  icmp    1       ICMP
  igmp    2       IGMP
  ggp     3       GGP
  tcp     6       TCP
  pup     12      PUP
  udp     17      UDP
  idp     22      IDP
  raw     255     RAW
  </code></pre>
  
  <p>In this exampe I used RAW.</p>
  
  <p>I run these example from the shell using 
  <code>php phpscriptinshell.php</code> 
  since I got into troubles using the SOCK_RAW option because I was not permitted to run the command over a web based php script.</p>
  
  <p>So finally I got the error output:</p>
  
  <p>Socket connection seems successful!try to send dataPHP Warning:  socket_write(): unable to write to socket [22]: Invalid argument in /volume2/web/phpscriptinshell.php on line 37</p>
  
  <p>Warning: socket_write(): unable to write to socket [22]: Invalid argument in /volume2/web/phpscriptinshell.php on line 37
  ficus> php phpscriptinshell.php </p>
  
  <p>When I use other options for the protocol. e.g. 1 for ICMP I get no error but also no data is transmitted.
  When I use 0 for IP i get "could not establish socket: [93] Protocol not supported"
  Wehn I use 6 for TCP the terminal becomes unresponcife and the wifi chip looses its connection to the network.</p>
  
  <p>So my questions are:</p>
  
  <p>1) Why I get "Invalid argument" when I try to write to the socket?</p>
  
  <p>2) What happens when using TCP as protocol argument?</p>
  
  <p>3) What should be the correct way to send data using php?</p>
  
  <p>4) What would you suggest for solving the problem?</p>
  
  <p>Thanks a lot for your hints!</p>
  
  <p>As a second try following Barmers Hint I tried the same script but replacing</p>
  
  <pre><code>$mysocket = socket_create(AF_INET, SOCK_RAW,255);
  </code></pre>
  
  <p>by</p>
  
  <pre><code>$mysocket = socket_create(AF_INET, SOCK_STREAM,SOL_TCP);
  </code></pre>
  
  <p>and again with </p>
  
  <pre><code>$mysocket = socket_create(AF_INET, SOCK_STREAM,6);
  </code></pre>
  
  <p>following the second hint I used as second script:</p>
  
  <pre><code>$socket = stream_socket_server("tcp://62.32.29.210:4000", $errno, $errstr);
  
  if (!$socket) {
  echo "$errstr ($errno)&lt;br /&gt;\n";
  
  } else {
  while ($conn = stream_socket_accept($socket)) {
  fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
  
  fclose($conn);
  }
  fclose($socket);
  }
  </code></pre>
  
  <p>Sorry I´m not so familiar with the formatting so please apologize. And thanks to the one that made my post look nice last time!</p>