【问题标题】:Pushing data to webserver using tcp/ip php使用 tcp/ip php 将数据推送到网络服务器
【发布时间】:2015-07-24 08:45:49
【问题描述】:
<?php
error_reporting(~E_NOTICE);
set_time_limit (0);

$address = ""; //ip here
$port = ; //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);
        }
    }
}
?>

我正在使用上面的代码通过 tcp/ip 与客户端机器通信,通信过程运行良好,我能够读取和写入客户端输入的数据,现在我想将客户端输入的数据推送到 webserver(数据库)并存储到数据库中使用 php 并将该数据显示到浏览器中,我该怎么办?任何帮助表示赞赏..

【问题讨论】:

  • 你到底想做什么?检索套接字提供的 $input 并将其存储在数据库中?如果是,您可以轻松使用 PDO 进行数据库访问:php.net/manual/fr/book.pdo.php
  • 是的,&input,它由客户端通过 tcp/ip 发送,我能够读取设备中显示的显示,我希望将数据存储到数据库中,这是我的字段,我的数据库是表在下面添加,我需要按以下字段推送,请给出一些想法和一些示例..
  • 这是我的数据库表字段idbill_nobill_amountitem_namequantityrateamountextra,@986 987654332@;
  • 你收到的 $input 对应的是什么?
  • 如果客户发送 Hi ,我会回复 ok 并且在 dsiplay clint 消息之后,如 .. Ok.. Hi

标签: php tcp


【解决方案1】:

所以,让我们承认您的数据库包含一个存储您所有输入的表。该表可以命名为“inputs”,由一个主键(例如“idInput”(int auto increment))和一个名为“input”的经典文本字段组成

您可以做的是,在接收新输入的地方,将其存储在这个新表中。这就是为什么,首先,您需要通过 PHP 建立数据库连接。让我们使用 PDO:

try{
   $PDO = new PDO('mysql:host=localhost;dbname=mydatabase','root','');
   $PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
   $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ);
}catch(PDOException $e){
   exit ('Connection failed');
}

那么,紧接着

$n = trim($input);

你可以这样做:

$req = $PDO->prepare("INSERT INTO inputs(input) VALUES (:input)");
        $req->execute(array(
            "input" => $PDO->quote($input),
            ));

【讨论】:

  • 这是我的流程..流程:终端会将交易信息发送到服务器。请求 API: 5001?客户 ID|马赫 ID|帐单编号|帐单日期@物品名称|物品数量|物品价格|Amt$ 物品名称|物品数量|物品价格|Amt$ 物品名称|物品数量|物品价格|金额;折扣|总金额#
  • 以上是要存入数据库的字段
猜你喜欢
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多