我有一台设备192.168.1.113可以将其数据传输到192.168.1.100

我希望PHP侦听这些传入的数据,然后将其提取并输入到MySQL数据库中。

我尝试了下面的代码,以尝试使数据回显到浏览器中,供初学者使用,以查看是否可以看到两者之间发送的数据:

<?php
error_reporting(~E_NOTICE);
set_time_limit (0);

$address = "192.168.1.100"; //ip here
$port = 8996; //port number  here
$max_clients = 10;

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, $address , 5000) )
{
$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 "Waiting for incoming connections... \n";

//array of client sockets
$client_socks = array();

//array of sockets to read
$read = array();

//start loop to listen for incoming connections and process existing
connections
while (true)
{
//prepare array of readable client sockets
$read = array();

//first socket is the master socket
$read[0] = $sock;

//now add the existing client sockets
for ($i = 0; $i < $max_clients; $i++)
{
    if($client_socks[$i] != null)
    {
        $read[$i+1] = $client_socks[$i];
    }
}

//now call select - blocking call
if(socket_select($read , $write , $except , null) === false)
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not listen on socket : [$errorcode] $errormsg \n");
}

//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read))
{
    for ($i = 0; $i < $max_clients; $i++)
    {
        if ($client_socks[$i] == null)
        {
            $client_socks[$i] = socket_accept($sock);

            //display information about the client who is connected
            if(socket_getpeername($client_socks[$i], $address, $port))
            {
                echo "Client $address : $port is now connected to us. \n";
            }

            //Send Welcome message to client
            $message = "Welcome to php socket server version 1.0 \n";
            $message .= "Enter a message and press enter, and i shall reply
back \n";


            socket_write($client_socks[$i] , $message);
            break;
        }
    }
  }

//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{
    if (in_array($client_socks[$i] , $read))
    {
        $input = socket_read($client_socks[$i] , 1024);

        if ($input == null)
        {
            //zero length string meaning disconnected, remove and close the
socket
            unset($client_socks[$i]);
            socket_close($client_socks[$i]);
        }

        $n = trim($input);

        $output = "OK ... $input";

        echo "Sending output to client \n";

        //send response to client
        socket_write($client_socks[$i] , $output);
    }
   }
}
?>


但是它只是挂在浏览器中,不会执行任何操作。我知道会遇到数据,因为Windows中有一个监听数据的工具(请参见图片)。

我在做什么错,如何提取这些数据并将其放入MySQL数据库?

最佳答案

二进制socket_read被阻止,请参见http://php.net/manual/en/function.socket-read.php上的前2条注释。他们都确认PH​​P_BINARY_READ socket_read正在阻止。

我建议使用带有读取超时的socket_select。
一些摘要:

 $defaultRecvTimeout=750;

socket_set_option($socket6,SOL_SOCKET,SO_RCVTIMEO,$this->millisecToSolArray(self::$defaultRecvTimeout));

        // wait for data to be available, up to timeout
        $r1 = array($this->socket);
        $w = null;
        $e = array($this->socket);
        $readTimeout = socket_get_option($this->socket,SOL_SOCKET,SO_RCVTIMEO);
        $res = socket_select($r1,$w,$e,$readTimeout['sec'],$readTimeout['usec']);

        // check
        if ($res === false) throw new SocketTransportException('Could not examine socket; '.socket_strerror(socket_last_error()), socket_last_error());
        if (!empty($e)) throw new SocketTransportException('Socket exception while waiting for data; '.socket_strerror(socket_last_error()), socket_last_error());
        if (empty($r1)) return false; // Nothing to read, return;
        $d = "";
        $r = 0;
        $buf = '';
        $r += socket_recv($this->socket,$buf,1000,MSG_DONTWAIT);
        if ($r === false) throw new SocketTransportException('Could not read '.$length.' bytes from socket; '.socket_strerror(socket_last_error()), socket_last_error());
        $d .= $buf;
        return $d;


这不是我的代码,我在某个地方找到了它,不幸的是忘记了值得归功于谁。但是,sockettransport.class.php是用于套接字处理的不错的类:https://github.com/paulv888/cronjobs/blob/3f8683ed1dff0a343dff7114fa2edc50a525c190/myclasses/sockettransport.class.php

实际上是原始文件来自这里:https://github.com/onlinecity/php-smpp

09-11 17:43
查看更多