my simple php script simply hangs up when i see in firebug
<?php
require 'FirePHPCore/fb.php';
ob_start();
session_start();
FB::log('Log message');
//FB::info('Info message');
//FB::warn('Warn message');
//FB::error('Error message');
FB::info('i m inside');
$fileCount = $_POST['count'];
$data = "i-" . $fileCount;
FB::info('data for server ' . $data);
$address = '127.0.0.1';
$port = 5555;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);
FB::info('socket connect ');
$len = strlen($data);
$status = socket_sendto($socket, $data, $len, 0, $address, $port);
$input = socket_read($socket, 1024);
echo $input;
socket_close($socket);
FB::log('i m deadout..');
What is the reason behind this problem please help?
thanks. The server (java) running on same machine.
I found out this problem when i saw firebug it simply displays loading icon.
And when i remove the socket programming part it works properly.
php socket connections are opened in blocking mode by default and you need to set using socket_set_nonblock. For an example;
$this->soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_set_nonblock($this->soc);
or
$socket = socket_create_listen(1223);
socket_set_nonblock($socket);
socket_accept($socket);
Related
I am having problem with socket_read() function in my php project.
I have gps device which is sending me position data to my server hosted on Azure VM with Apache2 web server on it.
Problem is that this function should return string after creating, binding, listening and socket accepting but it return something that I can not read. It returns data like this $p86i9"45C # but it should be like this:
*HQ,9170383669,V1,185625,V,2234.9299,N,11354.3998,E,000.00,000,011119,FFFFBBFF,220,05,00064,1261#
Here is my code:
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '10.0.0.4';
$port = 5050;
echo "Starting ... \n";
$sock = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP) or die("Blabla".socket_strerror(socket_last_error())."\n");
socket_bind($sock, $address, $port) or die("Blabla".socket_strerror(socket_last_error($sock))."\n");
socket_listen($sock, 5) or die("Blabla".socket_strerror(socket_last_error($sock))."\n");
do {
$msgsock = socket_accept($sock) or die("Blabla".socket_strerror(socket_last_error($sock))."\n");
$buf = socket_read($msgsock, 2048, PHP_BINARY_READ) or die("blabla ".socket_strerror(socket_last_error($msgsock))."\n");
echo "$buf\n";
} while (true);
socket_close($sock);
?>
I have a device which sends data in every 5 seconds using tcp and i am using php socket programming
and my code isn't working
this code print nothing and when i use this code without loop(while loop) then it works
i want to continue listen the data that my client sends in every 5 seconds
please help me out....thanks in advance.
<?php
error_reporting(E_ALL);
set_time_limit (0);
$host = '103.21.XX.XX';
$port = 60000;
$con = 1;
$word = "";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die(socket_strerror(socket_last_error()));
$result = socket_bind($socket, $host, $port) or die(socket_strerror(socket_last_error($socket)));
$result = socket_listen($socket) or die(socket_strerror(socket_last_error($socket)));
while ($con==1) {
# code...
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 2048) or die("Could not read input\n");
$input=(String)$input;
echo $input;
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
if($con == 1)
{
$word .= $input;
}
}
echo $word;
?>
I have a socket server program written in PHP and run by Fedora 21-apache, listening on a port.
<?php
set_time_limit (0);
$address = '1.2.3.4';
$port = "19000";
$con = 1;
$word = "";
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$bind = socket_bind($sock, $address, $port) or die("Could not bind to socket\n");
socket_listen($sock);
while ($con == 1)
{
$client = socket_accept($sock);
$input = socket_read($client, 50);
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
else {
$input = trim($input) . "," . date('Y-m-d H:i:s') . "\n";
$file= "/home/xyz/data/" . "uls_" . date("Ymd_His") . ".dat";
file_put_contents($file, $input);
}
}
?>
The client is actually a device. Currently there is only one device running.
As per the documentation provided by the device,
"It acts as a TCP client and opens a TCP socket session to the Server. The
device then sends a message and disconnects the socket session. Failed connections force retries"
Now I am able to get data from this client. The problem is that after say 4-5 Hours client is not able to push data.The server socket hangs.
The netstat -taun command shows following.
tcp 0 0 1.2.3.4:19000 0.0.0.0:* LISTEN
tcp 0 0 1.2.3.4:19000 3.4.5.6:20721 ESTABLISHED
Sometimes more than one client connections could be seen.
I can confirm that the client is still running during this time. I tried to connect to this server socket through another client socket script.
The result says, client request made but server socket did not respond.
If restart the web server, and run the server script again, everyrthing works normal for sometime.
Could anyone help me identify the problem.
I ran with the same probleme, i fixed it by closing the resource built by socket_accept()
while ($con == 1)
{
$client = socket_accept($sock);
$input = socket_read($client, 50);
if ($input == 'exit')
{
$close = socket_close($sock);
$con = 0;
}
else {
$input = trim($input) . "," . date('Y-m-d H:i:s') . "\n";
$file= "/home/xyz/data/" . "uls_" . date("Ymd_His") . ".dat";
file_put_contents($file, $input);
}
// #param2 - 1 => block writing in socket ...
socket_shutdown ($client , 1) ;
socket_close ($client ) ;
}
I want to extract data from the port and want to display it using php.please someone tell me the code in general.I tried the following code somewhere got like it may be from your site only
<?php
// Server IP address
$address = "localhost";
// Port to listen
$port =80;
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock,$address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);
// read 1024 bytes from client
$input = socket_read($client, 1024);
// write received gprs data to the file
writeToFile('gprs.log', $input);
socket_close($client);
socket_close($mysock);
?>
<?php
function writeToFile($strFilename, $strText) {
if($fp = #fopen($strFilename,"w")) {
$contents = fwrite($fp, $strText);
fclose($fp);
return true;
} else {
return false;
}
}
?>
but this is giving error
If the error is call to undefined function socket_create
You'll need to install (or enable) the Socket PHP extension: http://www.php.net/manual/en/sockets.installation.php
remove ; before extension=sockets.so or extension=sockets.dll
If the error is socket_bind(): unable to bind address
You'll need to use higher privileged user to run the script, for example use sudo script.php
I have a GPS tracker which sends data via GPRS to a specific IP, and specific port
I need a script in php which receives the data and write it to a txt file.
Perhaps the following code will help. You can place it in a loop it keeps listening for the next message.
<?php
// Server IP address
$address = "xx.xxx.xxx.xxx";
// Port to listen
$port = 80;
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock,$address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);
// read 1024 bytes from client
$input = socket_read($client, 1024);
// write received gprs data to the file
writeToFile('gprs.log', $input);
socket_close($client);
socket_close($mysock);
?>
<?php
function writeToFile($strFilename, $strText) {
if($fp = #fopen($strFilename,"w")) {
$contents = fwrite($fp, $strText);
fclose($fp);
return true;
} else {
return false;
}
}
?>