套接字发送在终端中输入的数据

时间:2016-06-27 13:11:55

标签: php linux sockets terminal

我想创建一个侦听连接的Socket,并将我在终端中输入的数据(想要使用php script.php运行)发送给所有客户端

任何方向都看起来?

最好的问候, 千斤顶

2 个答案:

答案 0 :(得分:0)

此脚本执行您想要的操作,并将客户端发送的数据写入您的终端,使用responseText ""运行:

php script.php

答案 1 :(得分:0)

以下程序可以帮助您发送和接收数据。

error_reporting(E_ALL | E_STRICT);


$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);


$from = *Destination IP*;
$port = 50001;
$msg = "@SET0123014002";
$len = strlen($msg);

//for sending message
socket_sendto($socket, $msg, 0, 0, $from, $port);

print "Time: " . date("%r") . "\n";

socket_close($socket);

echo "$msg and $len was Sent<br>";

$socket1 = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

//to Receive data from terminal.
socket_bind($socket1, '*Your System IP*', 50001);
socket_recvfrom($socket1, $buf, 12,0, $from, $port);

echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;