1

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>

1 Answer 1

1

You are continually creating, connecting and closing sockets, inside the while(1) with minimal delay between them. It's likely that you are consuming all the ports available as as the originating endpoint, as it would take time to close the socket.

Did you mean to only create a connection whenever the page is POSTed to with a new message?

Sign up to request clarification or add additional context in comments.

3 Comments

Once client is connected to server, the server need to know who is the client. After that i need to send message to all clients which connected to the server. Is this means that i need to keep connecting between server and client?
If you want to run this via a browser, then you could look at something like WebSockets (developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) which maintains a connection between Javascript code running on the browser and the server. Otherwise you could look at using long polling (check out stackoverflow.com/questions/333664/…).
I had tried with port bigger than 65530 and it works for sending to server. Now I need to let the server send a reply to all client that connected to the server. Is there any solution on this? It will be very helpful if there is any. By the way, Thanks a lot. =)

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.