K
K
KvoLt2016-07-20 09:46:57
PHP
KvoLt, 2016-07-20 09:46:57

How to get data transmitted on TCP port using PHP?

The telephone exchange transmits the CDR data to a predetermined IP on a predetermined port.
Task: to collect this data and add it to the MySQL database
The command netcat -l -v 50000 > /home/it/log.txt works fine, writes data to a file.
But with PHP it doesn't work.

<?php
error_reporting(E_ALL);
$service_port = 50000;
$address = '127.0.0.1';
/* Создать сокет TCP/IP. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "Не удалось выполнить функцию socket_create(): причина: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}
echo "Попытка соединиться с хостом '$address' по порту '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "Не получилось выполнить функцию socket_connect().\nПричина: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}
.................
?>

At this point I get the error PHP Warning: socket_connect(): unable to connect [111]: Connection refused in /home/it/test2.php
Any ideas how to overcome this?
In general, in the right direction I think or should I use something else?
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
KvoLt, 2016-07-20
@KvoLt

to Alexander @NeiroNx
Yes, I saw that topic, thanks.
But, as far as I understand, netcat is just listening to my own port, to which the station sends data.
Similarly, I'm trying to do it in PHP
Here is a construction that correctly receives data from the S8500

<?php
error_reporting(E_ALL);

$host = "0.0.0.0";
$port = 50000;
// No Timeout 
set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

while ($result)
{
$input = socket_read($spawn, 94) or die("Could not read input\n");
echo  $input, "\n";
}

socket_close($spawn);
socket_close($socket);
?>

The station is configured to issue CDR data on port 50000
In ch sys cdr , the issuance of a 94-character string is configured

A
Alexander, 2016-07-20
@NeiroNx

Here you are trying to connect to yourself.
A similar question was found by Google Receiving a data stream through a TCP socket in PHP

V
Vladimir Dubrovin, 2016-07-20
@z3apa3a

you need to use not socket_connect, but socket_listen (accept connections on the specified port) + in the socket_accept loop (process each incoming connection).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question