我想做类似这篇文章的事情: 广播以检测服务器IP地址
并转移到PHP,这可能吗?
原因是我想向服务器发送广播套接字,服务器将返回消息然后我可以确定消息是我想要的还是不检测消息发送者的真实 IP。
代码如下:
<?php
error_reporting(E_ALL);
$address = "255.255.255.255";
$port = 10000;
/* Create a udp socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br/>";
} else {
echo "socket successfully created.<br/>";
}
echo "Attempting to connect to '$address' on port '$port'..."."<br/>";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "<br/>";
} else {
echo "successfully connected to $address."."<br/>";
}
$i="theMessage";
echo "Sending $i to server."."<br/>";
socket_write($socket, $i, strlen($i));
$input = socket_read($socket, 512);
echo "Response from server is: $input"."<br/>";
$ip="";
if($input=="I want")
$ip=socket_send_from_the_IP; // just psuedocode
echo "Closing socket...";
socket_close($socket);
?>
还是我问题的方向错了?我也认为我应该使用httprequest?
任何答案表示赞赏。