Doubt about Socket php

Asked

Viewed 290 times

-1

I’m starting to study about Sockets, and I took as a basis the code of the following video: https://www.youtube.com/watch?v=qqDdHjJBZVw

The question is: Why does it only work when called through cls in the CMD? When I start the apache service in Xampp, and try to run the server through the browser, it does not work.

I have already made the command netstat -a, -an to catch a port that is not being used.

I’m using the following codes: Client:

<?php
echo "\n Type your username:  ";
$user = trim(fgets(STDIN));
if(strlen($user) <= 2) { exit; }
else {
while(1)
{   
    echo "\n Please say something or enter 'q' to quit:  ";
    $ticker = trim(fgets(STDIN));
    if($ticker=='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",1234);
    if($result===false)
    {
        echo "Socket connection failed!";
    }
        else { 
        socket_write($socket,"$user says --> $ticker",1024);
         }
    }
}
?>

Server:

<?php
error_reporting(0);
set_time_limit(0);
$host = "127.0.0.1";
$port = 1234;
$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");
echo "Waiting for connections... \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);
?>

Ps: Even through CMD, if I start apache in Xampp it doesn’t work.

If someone could send a client/server code just for me to study, as it works in the browser, it would help me a lot. Thank you!

1 answer

0

You should test if the extenxão is compiled before using the sockets:

if (!extension_loaded('sockets')) {
    die('Sockets não disponível.');
}

It is probably present only in the CLI version.

  • I did a test and returned true, the extension is loaded. I did a Debug, and the problem occurs in the socket_bind method.

  • Right, check and put here the socket_last_error value()

  • The error occurs in the socket_bind method line, put the socket_last_error() after the die, the result was this: Could not bind to socket - socket last error = 10048

  • It seems that this in use, try to use the socket_set_option($socket, SOL_SOCKET, SO_REUSEADDDR, 1) before the bind. Also check the string version of the message with socket_strerror(socket_last_error()).. should be more informative about the error that is occurring.

  • Without using the socket_set_option, the following msg occurred: Could not bind to socket - Normally only one uses the one of each socket Address (protocol/Endere the network/port).

  • Putting the socket_set_option, as you asked. The message was this: Could not bind to socket - An attempt was made to access a socket in a way prohibited by the access permissions.

  • As far as I understand, before the server.php file can access the port, the xamp apache access the port first, hence the conflict occurs.

  • It seems that the user who is running on the web server does not have enough permission for this, which makes me wonder why create a server to receive connections within a page. It makes more sense in the same console. It would be nice to exclaim this purpose in the question.

  • Actually, I was just wondering why it doesn’t work when the Apache is on. I think the socket doesn’t work with web server on, because the web server accesses the port before the socket, I think this is it.

  • Looks like port conflict, puts the server socket on another port that should solve.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.