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);
Related
I am running a client-server code in php for chatting. I am getting this error:
socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it
I have gone through many questions and answers on this problem but still unable to solve this.
Here is my code :
for user-1:
$host = "127.0.0.1";
$port_1 = 5001;
$port_2 = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");
socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
$result = socket_read ($socket_1, 1024);
if (isset($_POST['submit']))
{
$message = $_POST['client_message'];
socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
}
// or die("Could not read server response\n");
if(!empty($result))
{
echo "Reply From Server :".$result;
}
socket_close($socket_1);
socket_close($socket_2);
socket_close($new_socket);
?
This is for user-2
$host = "127.0.0.1";
$port_1 = 5001;
$port_2 = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");
socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
$result = socket_read ($socket_1, 1024);
if (isset($_POST['submit']))
{
$message = $_POST['client_message'];
socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
}
// or die("Could not read server response\n");
if(!empty($result))
{
echo "Reply From Server :".$result;
}
socket_close($socket_1);
socket_close($socket_2);
socket_close($new_socket);
?>
please help me figuring out the errors.
Thank you
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
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);
}
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);
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