使用PHP广播检测服务器IP地址

时间:2013-07-24 08:10:51

标签: php sockets ip broadcastreceiver

我想做这样的事情: broadcast to detect Server IP address

并转移到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?

任何回答都表示赞赏。

0 个答案:

没有答案