Answer the question
In order to leave comments, you need to log in
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";
}
.................
?>
Answer the question
In order to leave comments, you need to log in
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);
?>
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
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 questionAsk a Question
731 491 924 answers to any question