how to connect more than one client from one server - php

how to connect more than one client to a single server ,, for a simple chat application between 2 clients.
I didn't get a proper answer on net,
should i have to create multiple sockets or ports ,
any reference or example code or guidance..
here is my server code
$host = "127.0.0.1";
$port = 25003;
// No Timeout
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//echo $socket;
$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");
$output = strrev($input);
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);

If there are multiple connections queued on the socket, the first will be used. If there are no pending connections, socket_accept() will block until a connection becomes present.
If you want to have multiple clients on a server you will have to use non blocking.
socket_set_nonblock($socket);

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);
?>`

Creating a client and server side socket connection and keeping it connected in php

If we create a socket in server side then it runs in a infinite loop, cant we do something like this for the client side? Can we create a listening mood in a infinite loop? Do I need to create a new socket every for this every time?
Here is my code, it writes only once, when I try to write after socket_read it doesn't work.
Server side code
<?php
$host = "192.168.56.1";
$port = 8080;
$message = "Hello Client";
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) or die("Could not set up socket listener\n");
echo 'listining ip '.$host." at port ".$port;
while(true){
$com = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($com, 1024) or die("Could not read input\n");
$input = trim($input);
echo '
Client says: '.$input;
socket_write($com, $message , strlen ($message)) or die("Could not write output\n");
}
echo '
server closed';
socket_close($com);
socket_close($socket);
?>
client side code
<?php
$host = "192.168.56.1";
$port = 8080;
$message = "Hello Server side";
$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");
socket_write($socket,$message, strlen($message)) or die("Could not send data to server\n");
echo "Server says :";//.$result;
socket_close($socket);
?>
If we create a socket in server side then it runs in a infinite loop,
cant we do something like this for the client side? Can we create a
listening mood in a infinite loop?
Of course - if your server and client would send and receive strictly in turn, you could simply change the socket_read line in the client to
while ($result = socket_read($socket, 1024))
Do I need to create a new socket every for this every time?
No, you must not, since it wouldn't be the same connection.
But for a more complete server example which handles multiple connections and disconnections, cf. this answer to How to write server for chat program in php by using socket.

how to implement socket connection on server?

I am initiator of socket programming. I am trying to implement the socket connection.
But I don't know which IP and port should I use for server.
The code I took from a link :
server.php:
<?php
// set some variables
$host = "<?>";
$port = 5555;
// don't timeout!
set_time_limit(0);
// create socket
//echo 'hello';
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
// 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");
echo 'hello';
// 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 = $_REQUEST['server_input'];
$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);
?>
- client.php
<?php
//include_once('server.php');
$host = "<?>";
$port = 5555;
$message = "hi from client";
//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 "Reply From Server :".$result;
// close socket
socket_close($socket);
?>
Description :
This function accepts incoming connection request on the created socket. After accepting the connection from client socket, this function returns another socket resource that is actually responsible for communication with the corresponding client socket. Here “$spawn” is that socket resource which is responsible for communication with client socket.
But I don't know which IP and port should I use for server.
The IP for use is same static IP set on your server's network, If you try to local test, Can use 127.0.0.1 for IP.
And also, for port, you can use one desired number between 1024 to 65535.
Note that, the IP and Port used on the server and client must be same.

keep socket server running on CentOS to listen to client request in php

I have a client and server program i do not have an idea of how to start the server automatically on my CENTOS server and the server should keep on running irrespective of my browser is closed, and it keeps on listening to client request. Please Help
Server.php
<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// 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";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Client.php
<?php
// where is the socket server?
$host = "XX.XX.XXX.XX";
$port = 25763;
$message = "Hello Server This is the first message to the server";
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 "Reply From Server :".$result;
// close socket
socket_close($socket);
// print result to browser
?>
Generally, you should start .php file with exec(), for example:
exec('/usr/bin/php /var/www/directory/server/event_server.php');
Next thing, put your socket_accept() (althoug, I used different scheme, with socket_select()) in infinite loop, and dont forget about delay - this is very important, as you don't want to hang your CPU:
$broadcast_start = microtime(true);
while(TRUE) {
socket_accept($socket);
//Do stuff...
if( round(microtime(true) - $broadcast_start, 1) < 0.5 ) {
usleep(500000);
}
}
But actually, your subbjects is really complex. You should first search the web for solutions, which are plenty.

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