3

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

1 Answer 1

8

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok proper, and when do i ran server.php? and isn't it gonna exploit resources by constantly running?
no. as you see in last line of loop you destroy client socket ($spawn), and waiting for dispatch new one in first line of loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.