我想在PHP中编写和读取套接字上的消息
我的写信是= 82,10,1,6,150,249
我的阅读信息是= 82,10,1,6,148,297
我写这段代码,但不要耍。
提示:我的消息是数组
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$message = array('1'=> '82', '2'=>'10', '3'=>'1' ,'4'=>'6' ,'5'=>'150' ,'6'=>'249');
$len = strlen($message);
socket_sendto($sock, $message, $len, 0, '192.168.43.232', 5050);
socket_close($sock)
答案 0 :(得分:0)
$host = "192.168.1.1";
$port = 8080;
//Create Packet
$TurnOn = array(82, 10, 1, 6, 150, 249);
foreach ($TurnOn as $chunk) {
$message .= chr($chunk);
}
//Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$socket) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
//Connect socket to remote server
if (!socket_connect($socket, $host, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [$errorcode] $errormsg \n");
}
//Send the message to the server
if (!socket_send($socket, $message, strlen($message), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
socket_close($socket);