php Socket Socket_bind() unable to bind address [10013] - php

My situation:
I have a program which send on a port (4448) a lot of data from some machines.
I can retrieve that datas from a telnet connection.
telnet steps:
launch as cmd as ADMIN
write telnet and send
write "open localhost 4448" and send
write "GET xx,yy"
after this i just read on screen info in live
xx and yy are numbers which identifies the machine
Now lets go on php side I have my index.php but when I execute socket bind it gives me back an error (reported under the code) and about the code, this come from: http://php.net/manual/en/sockets.examples.php and its the first example, i read and tried this solution also https://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP which use 2 php files, in both cases i get the same error...
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = 'localhost';
$port = 4448;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "GET xx,yy";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
and this is the error i get:
Warning: socket_bind(): unable to bind address [10013]: An attempt was
made to access a socket in a way forbidden by its access permissions
I have already looked for administrator privileges and I think its all right.
Extra info: I am working with PHP 7, my web server is Apache in Xampp package.

It's possible another program is already using that port. Try using a different port. If that works you can try finding the process using the port and terminating it. This tool from sysinternals may help: https://learn.microsoft.com/en-us/sysinternals/downloads/tcpview
If using a different port also fails, a possiblity is that you have a firewall that doesn't allow bind. Try disabling your firewall temporarily.

Related

How to run PHP Port Listener Script with WAMP not PHP CLI?

I hope that I do not ask a spam question because I can not find any question about it.
I write a socket port listener to listen an specific IP:PORT using PHP to receives TCP Packets from GPS Tracking Devices.
When I run script with this command:
$php.exe -q My/Script/Files/Path.PHP
everything is okay and my Server port listener works successfully and I receive data from devices but when I want open that with my browser (wamp -> localhost) it's said that
Warning: socket_bind(): unable to bind address [10049]: The requested address is not valid in its context.
How I can Solve This Problem?
Best Regards.
Edit One:
My Server Script Code:
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '*.*.*.*'; //I put Here My System Local IPV4 Address (163.*.*.*)
$port = *****; //I put Here My Server Opened Port Number and I turned off the firewall to test
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 1) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if ($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
}
$buf = trim($buf);
$clientResponse = explode(',', $buf);
foreach($clientResponse as $key=>$value) {
echo $key . ' : ' . $value . "<br />";
}
socket_close($msgsock);
socket_close($sock);
?>
As I said before:
When I run this script with PHP CLI, everything is okay and my device sends TCP Packet and My server receives TCP Packet and show that information. But when I run it By wamp with it local IP 127.0.0.1, it gave me that error.
till here:
I have a VPS Server
I have a Static IP (Public IP)
I have an Open TCP port with off firewall
I have a local IP
I have a wamp with IP:Port -> 127.0.0.1:80
Edit Two:
My Script Port: 41260
I google some keywords and I find this docs : (Bind) in Apache's official Site. And I decided to share the result to other users.

Multiple Socket Connection Using PHP

I have made a socket connection using php which is working perfectly below is the code for single socket but not working with multiple connections only accepts one connection at a time
error_reporting(E_ALL);
// sudo lsof -t -i:10000
// php -f server.php
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '192.168.5.155';
$port = 3490;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 1024) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_BINARY_READ))) {
/*echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;*/
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break 2;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
if($buf != '')
{
$req_get = $talkback;
$file = time();
$fp = fopen('data/'.$file.'.txt', 'a+');
fwrite($fp, $req_get);
fclose($fp);
socket_write($msgsock, $talkback, strlen($talkback));
}
//echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
I need a help to just make it work with multiple connection with clients.
After that i need to make entry into database from client sent data.
You can't listen many sockets using blocking socket connections at the same time. What you need is non-blocking socket connections with socket_select method instead of socket_accept.
My suggestion is to use some kind of PHP event-loop implementations (like reactphp).
Anyway, if you still want to implement it yourself, you can check the example of stream_select usage in reactphp/event-loop StreamSelectLoop implementation.

Socket communication between PHP server and ActionScript

I am studying about socket communication and succeeded to connect between PHP server and PHP client. Now I want to do same thing with PHP server and ActionScript client.
http://php.net/manual/en/sockets.examples.php
I read this and could communicate between server and client on 2 Windows command prompts both in PHP.
So now I changed the client to ActionScript but it seems connecting to server but the string or number is not sent.
Does socket communication matter only address and port regardless of language? Or I cannot use two different languages to communicate?
For server, I used example 1 code in the URL above, changed address and port.
AS client is here, I wrote the minimum code, this would just send number when the file is run.:
import flash.net.Socket;
import flash.events.*;
import flash.net.LocalConnection;
var s:Socket = new Socket;
s.connect("localhost",10000);
s.writeByte( 10 );
s.flush();
trace("sent");
And this is PHP server code.
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = 'localhost';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>

sending commands to tk103 gps tracker

I'm working on a realtime gps tracker web application using php.
The tracker reference is tk103, I can receive information from the tracker and store it into database.
GPRS mode of the device is enabled, my question is :
How can I send command from my server to the device using php.
Thanks in advance
it' depends on which protocol are you using to send data from tk103 device to server if you are using TCP/IP protocol once data came from device you should replay back with your command.
you show configure in device to ask or invoke to request for command.
we are configure like
1) imei:359710047059226,tracker,15/03/18 12:17,,F,121703.000,A,1255.2746,N,07736.8209,E,0.00,208.00,0.00,1,0;
id device send this command we are storing only to data base
2) imei:359710047059226,tracker,15/03/18 12:17,,F,121703.000,A,1255.2746,N,07736.8209,E,0.00,208.00,0.00,1,1;
last bit data we are testing
if 0 means only storing replay with :OK
if 1 means replay with :CMD GPRS 001 like some commands
[`#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
`]1

Socket not working PHP

Here is my code
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = 'localhost';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
do {
$out = socket_read($msgsock, 2048);
if (!empty($out)) {
if ($out == 'quit') {
break;
}
elseif ($out == 'shutdown') {
socket_write($msgsock, 'plc down', 8);
socket_close($msgsock);
break 2;
}
else {
switch ($out) {
case "KABBE": $response = "Kabbe te!"; break;
case "SZOPJ": $response = "Szopjal te!"; break;
default: $response = "Ismeretlen parancs";
}
socket_write($msgsock, $response, strlen($response));
break;
}
}
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
and now the errors
Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\wamp\www\socket\socket.php on line 18
socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: An invalid argument was supplied.
Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: An invalid argument was supplied.
I searched on google but nothing useful.
What is the problem?
PHP also offers stream_socket_server and other stream_socket_* functions.
I found these to be more developer friendly.
Example code from php.net:
$socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
'localhost' isn't a valid address as socket_bind doesn't accept DNS names, use the equivalent IP address '127.0.0.1'.
More info
That means you already have an open socket on your computer on that port.
Try switching to another unused port.
On Windows (which is what you seem to be working on), you can see the list of open sockets from the command line:
netstat -an
If you want to know which processes are listening to those ports, try this instead:
netstat -ban
use this code before binding:
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
for reference http://www.php.net/manual/en/function.socket-bind.php
You can also check http://www.php.net/manual/en/function.socket-set-option.php for details

Categories