How to keep a php web socket connection open after first connection - php

I'm using the following code as the server for sockets while i'm trying to learn sockets, but the client code will work once, i have to run the server script every time before i run the clint code.
And so when do i ran this server.php to keep listening for client requests?
SERVER.PHP
$host = "127.0.0.1";
$port = 25003;
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");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if ($input == "Hey"){
$input = "Hey you don't shout me. Talk properly...";
} else if ($input == "Vetra"){
$input = "Hello how are you there, whats your name?";
} else { $input = "Well, what can i say, you must be a human being.";
}
echo $input;
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
//socket_close($spawn);
//socket_close($socket);
CLIENT.PHP
$host = "127.0.0.1";
$port = 25003;
$message = $_POST['data'];
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo $result;
//socket_close($socket);

You should waiting for new connections in infinite loop:
$host = "127.0.0.1";
$port = 25003;
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");
while(true) {
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if ($input == "Hey"){
$input = "Hey you don't shout me. Talk properly...";
} else if ($input == "Vetra"){
$input = "Hello how are you there, whats your name?";
} else {
$input = "Well, what can i say, you must be a human being.";
}
echo $input.PHP_EOL;
$output = $input . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
}

Related

How to run this PHP websocket as a background service in linux server

I got this two files chat.php and chat2.php, it works fine but i want to run it as a background service in linux server or something like that
chat.php
`<?php
$host = $_SERVER['SERVER_ADDR'];// set some variables
$port = 25003;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?> `
*
i run this code and i could transfer from my input form as long as the page is reloading the chat.php file get the POST string so i want to run it as a server service or something like that. Thanks.*
`<?php
// set some variables
$host = $_SERVER['SERVER_ADDR'];
$port = 25003;
$message = $_POST['message'];
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo '';
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
?>`

PHP Socket Client only sends and receives one message

I recently started learning php socket.
I want to create a Permanent TCP connection Between server and client! But my PHP Socket Client only sends and receives one message. I want to send and receive messages indefinitely, Through one connection.
Server.php:
<?php
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, '127.0.0.1', '8088') or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
while(true) {
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 2048) or die("Could not read input\n");
echo base64_encode($input)."\r\n";
$output="OK";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
//socket_close($spawn);
//socket_close($socket);
}
?>
Client.php:
<?php
set_time_limit(0);
$input="Hi";
$socket2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result2 = socket_connect($socket2, '127.0.0.1', '8088') or die("Could not connect to server\n");
while(true) {
socket_write($socket2, $input, strlen($input)) or die("Could not send data to server\n");
$result2 = socket_read ($socket2, 2048) or die("Could not read server response\n");
$output=$result2;
echo $output;
sleep(2);
}
//socket_close($spawn);
//socket_close($socket);
?>
In server.php, you need to add another while(trur) {} for the code after socket_accept, cause if you everytime you run socket_accept, the last connection made would not work anymore.
<?php
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, '127.0.0.1', '8088') or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
while(true) {
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
while(true) {
$input = socket_read($spawn, 2048) or die("Could not read input\n");
echo base64_encode($input)."\r\n";
$output="OK";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
}
}
//socket_close($spawn);
//socket_close($socket);

Sending a XML request to a socket

I am trying to send a XML to a server in a TCP\IP (socket) connection.
My connection is okay. The sending part is the issue.
See below;
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = "xx.xxx.xx.xxx";
$port = xxxx;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
if($result === true){
echo 'connected';
}
$sendVendRequest='
<ipayMsg client="SAFEPAY" term="00001" seqNum="0" time=" '.date('Y-m-d H: i: s').' +0200">
<elecMsg ver="2.44">
<vendReq>
<ref>319155500001</ref>
<amt cur="KSh">1000</amt>
<numTokens>1</numTokens>
<meter>A12C3456789</meter>
<payType>cash</payType>
</vendReq >
</elecMsg>
</ipayMsg>';
$vendRequestXml=simplexml_load_string($sendVendRequest) or die("Error: could not create an object");
// print_r($vendRequestXml);
socket_write($socket, $sendVendRequest, strlen($sendVendRequest)) or die("Could not send data to server\n");
The sending part fails. It loads until it times out. I suspect I am sending the request wrongly.. Someone please direct me on how to achieve this.
This code works for me:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = "example.com";
$port = 80;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
if($result === true){
echo 'connected';
}ยท
$st='
<ipayMsg client="SAFEPAY" term="00001" seqNum="0" time=" '.date('Y-m-d H: i: s').' +0200">
<elecMsg ver="2.44">
<vendReq>
<ref>319155500001</ref>
<amt cur="KSh">1000</amt>
<numTokens>1</numTokens>
<meter>A12C3456789</meter>
<payType>cash</payType>
</vendReq >
</elecMsg>
</ipayMsg>';
$length = strlen($st);
while(true) {
$sent = socket_write($socket, $st, $length);
if($sent === false) {
break;
}
// Was the entire msg sent?
if($sent < $length) {
// If not, handle the leftover data.
$st = substr($st, $sent);
$length -= $sent;
} else {
break;
}
}
echo socket_read($socket, 8192);
Can you telenet to that port?

How to test PHP Socket Programming in localhost + ubuntu

I have two files:
client.php
<?php
$host = "127.0.0.1";
$port = 25003;
$message = "Hello Server";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
socket_close($socket);
?>
and server.php
<?php
$host = "127.0.0.1";
$port = 25003;
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");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$input = trim($input);
echo "Client Message : ".$input;
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);
?>
I am running http://www.domain.com/php-socket/client.php from browser. But I don't know how to run server.php on server side using terminal. My php-socket root directory is /var/www/php-socket/
please open terminal and type bellow command
cd /var/www/php-socket
php server.php

Error :Could not bind to socket in php programming

these is an example showing TCP server that accepts a string as input, reverses it and returns it to the client.
Here is Code:
<?php
$host = "127.0.0.1";
$port = 1234;
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");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$input = trim($input);
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);
?>
and got error like this:
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:\xampp\htdocs\socket.php on line 5
Could not bind to socket
What could I do to fix this ?
Add this option to the program 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