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