简体   繁体   English

如何使用php将原始数据发送到IP

[英]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). 我试图用PHP脚本将原始数据发送到嵌入式设备(基于WizFi wlan模块的Wifi Shield在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) unix终端上的echo -n "teststring" | nc xx.xx.xx.xx 4000 (Mac OS X)(x代表设备ip的占位符)

or using Putty (under Windows XP) with ip, port and selecting "RAW" as the connection type. 或使用Putty(在Windows XP下)使用ip,port并选择“RAW”作为连接类型。

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). 为了达到同样的目的,我尝试在php脚本中建立一个套接字(因为我喜欢通过Web表单将数据发送到设备)。

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). socket create命令的相应协议(参数的最后一个整数)是:(我从服务器的/ etc / protocols复制的列表(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. 在这个例子中,我使用了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. 我使用php phpscriptinshell.php从shell运行这些示例,因为我遇到了使用SOCK_RAW选项的麻烦,因为我不允许在基于Web的php脚本上运行命令。

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 套接字连接似乎成功!尝试发送dataPHP警告:socket_write():无法写入套接字[22]:第37行/volume2/web/phpscriptinshell.php中的参数无效

Warning: socket_write(): unable to write to socket [22]: Invalid argument in /volume2/web/phpscriptinshell.php on line 37 ficus> php phpscriptinshell.php 警告:socket_write():无法写入socket [22]:第37行的/volume2/web/phpscriptinshell.php中的参数无效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. 例如1对于ICMP我没有错误但也没有数据被传输。 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. 当我使用0作为IP时,我得到“无法建立套接字:[93]不支持协议”我使用6作为TCP,终端变得无法响应,并且wifi芯片失去了与网络的连接。

So my questions are: 所以我的问题是:

1) Why I get "Invalid argument" when I try to write to the socket? 1)当我尝试写入套接字时为什么会出现“无效参数”?

2) What happens when using TCP as protocol argument? 2)使用TCP作为协议参数时会发生什么?

3) What should be the correct way to send data using php? 3)使用php发送数据的正确方法是什么?

4) What would you suggest for solving the problem? 4)你有什么建议解决这个问题?

Thanks a lot for your hints! 非常感谢您的提示!

As a second try following Barmers Hint I tried the same script but replacing 作为Barmers Hint的第二次尝试,我尝试了相同的脚本,但更换了

$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. SOCK_RAW适用于需要访问数据包标头或某些低级协议(如ICMP)的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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