By Jerry


2020-06-30 03:50:31 8 Comments

Using the PHP stream_socket_server connection.

I want to capture data received from socket into a record in MySQL. I'm able to capture the data, however, it is split up into multiple records and I'm not able concatenate the stream together. I tried increasing the chunk size, but it didn't matter.

$server = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errorMessage);
$client_socks = array();
while(true)
{
        $read_socks = $client_socks;
        $read_socks[] = $server;
if(in_array($server, $read_socks))
{
        $new_client = stream_socket_accept($server);

        if ($new_client) 
        {
                $ip_address = stream_socket_get_name($new_client, true);
                $client_socks[] = $new_client;
        }
        unset($read_socks[ array_search($server, $read_socks) ]);
}
        $data = "";

        foreach($read_socks as $sock)
        {
                stream_set_chunk_size($sock, 131072);
                $data = fread($sock, 131072);
                if(!$data)
                {
                        unset($client_socks[ array_search($sock, $client_socks) ]);
                        @fclose($sock);
                        continue;
                }
        }

        $conn = new mysqli($servername, $username, $password,$dbname);
        $user_agent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'UNDEFINED';
        $stmt = $conn->prepare("INSERT INTO xxxdata(data,ipaddress,useragent) VALUES (?,?,?)");
        $stmt->bind_param("sss",$data,$ip_address,$user_agent);
        $conn->close();
}

MySQL output appears like:

MYSQL OUTPUT

None of the data columns get larger than 15k and they vary. It's on JPG file coming over, 42k in size.

0 comments

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Saving image from PHP URL

  • 2009-04-07 06:50:46
  • riad
  • 721908 View
  • 405 Score
  • 12 Answer
  • Tags:   php image

34 Answered Questions

15 Answered Questions

[SOLVED] MySQL: Insert record if not exists in table

  • 2010-07-02 09:23:29
  • Rupert
  • 714714 View
  • 386 Score
  • 15 Answer
  • Tags:   mysql

27 Answered Questions

[SOLVED] Retrieving the last record in each group - MySQL

4 Answered Questions

[SOLVED] mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists

  • 2017-02-10 06:43:45
  • Rajadip
  • 187758 View
  • 187 Score
  • 4 Answer
  • Tags:   mysql sockets

23 Answered Questions

[SOLVED] Find duplicate records in MySQL

  • 2009-05-12 18:24:21
  • Chris Bartow
  • 765543 View
  • 654 Score
  • 23 Answer
  • Tags:   mysql duplicates

39 Answered Questions

[SOLVED] Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

  • 2011-03-21 10:33:36
  • Lambivist
  • 1314606 View
  • 386 Score
  • 39 Answer
  • Tags:   mysql

8 Answered Questions

[SOLVED] close vs shutdown socket?

  • 2010-11-11 23:38:21
  • user188276
  • 242364 View
  • 214 Score
  • 8 Answer
  • Tags:   c sockets networking

35 Answered Questions

[SOLVED] What is the difference between a port and a socket?

13 Answered Questions

[SOLVED] How can I capture the result of var_dump to a string?

  • 2008-09-26 13:23:02
  • Mark Biek
  • 343075 View
  • 608 Score
  • 13 Answer
  • Tags:   php string var-dump

Sponsored Content