I run the server.php then run client.php and this warning show on the client.php screen after several try to send from client to server.
Warning: socket_connect(): unable to connect [10048]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\xampp\htdocs\client.php on line 32 Socket connection failed!
After that xampp stopped working.
Here is server.php
<!DOCTYPE html>
<html>
<?php
error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
$host = "127.0.0.1";
$port = 65535;
echo "Waiting for connections... \n";
$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");
while(1){
$spawn[++$i] = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "\n";
$input = socket_read($spawn[$i],1024);
$client = $input;
echo $client ."\n";
socket_close($spawn[$i]);
echo "\n";
}
socket_close($socket);
?>
</html>
Here is my client.php
<!DOCTYPE html>
<html>
<body>
<form method="post" action="client.php">
<p><h4><label>Type Your Message Here:<input name = "message" size = "25" maxlength = "30" required></label></h4></p>
<input type="submit" name="sendmsg" class="btn btn-primary" value="send message"/>
</form>
<?php
$user="abc";
if(empty($_POST)){
}
elseif(isset($_POST['sendmsg'])) {
$message =$_POST["message"];
while(1){
if($message=='q') { exit; }
$socket= socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($socket===false)
{
echo "Socket creation failed!";
}
$result = socket_connect($socket,"127.0.0.1",65535);
if($result===false)
{
echo "Socket connection failed!";
}
else {
if($message !='0'){
socket_write($socket,"$user says --> $message",1024);
$message='0';
}
}
socket_close($socket);
}
}
?>
</body>
</html>