我想开发使用PHP的GPS跟踪系统。我在车辆上安装了GPS跟踪设备,并且可以通过SMS设置IP地址和端口。所以该设备将数据发送到给定的服务器端口。我的理想是,使用PHP Socket我会监听套接字并将其存储到MySQL数据库中。后来我可以做任何我想要的东西无法连接套接字:[111]连接拒绝
现在的问题是我无法接收来自设备的消息。请看看我的代码第一
服务器端套接字代码:(server.php)
<?php
$_SOCKET = "166.62.10.183";
$_PORT = 5544;
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if(!socket_bind($sock, $_SOCKET , $_PORT))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Listening on \n SOCKET: ".$_SOCKET."\n PORT: ".$_PORT."\n";
echo "Waiting for incoming connections... \n";
//start loop to listen for incoming connections
while (true)
{
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client , $address , $port))
{
echo "Client $address : $port is now connected to us. \n";
}
//read data from the incoming socket
$input = socket_read($client, 1024000);
$response = "OK .. $input";
echo $response;
}
?>
客户端测试代码:(client.php)
<?php
//Creating a Socket
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "1:: ---- Socket Created </br><br>";
//Connecting to Server
if(!socket_connect($sock, "166.62.10.183", 5544)){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't connect socket: [$errorcode] $errormsg </br>");
}
echo "2:: ---- Connection Established </br></br>";
//Send message to Server
$message = "Message from Web\r\n";
if(!socket_send($sock, $message, strlen($message), 0)){
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't not send data: [$errorcode] $errormsg </br>");
}
echo "3:: ---- Message sent </br></br>";
// ?>
当我运行客户端.php代码来自同一台服务器,我可以在server.php中看到消息(我通过SSH运行“php server.php”)。当我从任何其他服务器上运行的代码,我看到以下错误消息
1 :: ----插槽创建
无法连接插座:[111]拒绝连接
我想是因为这个原因,我无法接收来自GPS设备的消息。指导我解决这个问题。
您是否有可能阻塞连接的防火墙?你为什么将协议指定为'0'?你不想'SOL_TCP'? –
我检查并且能够telnet到服务器上的端口22,但不是在5544上,这意味着它不能用于外部访问。根据您的云提供商是否具有防火墙或您的计算机启用了防火墙,您需要启用端口5544以进行外部访问 –
Thanks @ Don'tPanic。我尝试了所有可用的协议。但它仍然没有工作。我已联系托管服务提供商,他们已经开通了该端口。仍然没有运气 – Kalaivanan