简体   繁体   中英

How to send raw data to an IP utilizing php

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:

echo -n "teststring" | nc xx.xx.xx.xx 4000 echo -n "teststring" | nc xx.xx.xx.xx 4000 on a unix terminal (Mac OS X) (the x represent a placeholder for the ip of the device)

or using Putty (under Windows XP) with ip, port and selecting "RAW" as the connection type.

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).

see the sample code below:

$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<pre>".socket_strerror(socket_last_error())."</pre>";
}

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);                                                     

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).

<!-- language: none -->
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

In this exampe I used RAW.

I run these example from the shell using php phpscriptinshell.php 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.

So finally I got the error output:

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

Warning: socket_write(): unable to write to socket [22]: Invalid argument in /volume2/web/phpscriptinshell.php on line 37 ficus> php phpscriptinshell.php

When I use other options for the protocol. eg 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.

So my questions are:

1) Why I get "Invalid argument" when I try to write to the socket?

2) What happens when using TCP as protocol argument?

3) What should be the correct way to send data using php?

4) What would you suggest for solving the problem?

Thanks a lot for your hints!

As a second try following Barmers Hint I tried the same script but replacing

$mysocket = socket_create(AF_INET, SOCK_RAW,255);

by

$mysocket = socket_create(AF_INET, SOCK_STREAM,SOL_TCP);

and again with

$mysocket = socket_create(AF_INET, SOCK_STREAM,6);

following the second hint I used as second script:

$socket = stream_socket_server("tcp://62.32.29.210:4000", $errno, $errstr);

if (!$socket) {
echo "$errstr ($errno)<br />\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);
}

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!

You want:

$mysocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

SOCK_RAW is for when you need to access packet headers or for some low-level protocols like ICMP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM