php-websocket icon indicating copy to clipboard operation
php-websocket copied to clipboard

socket_create(): Unable to create socket [10047]: An address incompatible with the requested protocol was used

Open suyovj opened this issue 4 years ago • 9 comments

Hey,

I am trying to replicate the example given in Readme.

But I am stuck up with this error.

Type: ErrorException Message: socket_create(): Unable to create socket [10047]: An address incompatible with the requested protocol was used

I haven't changed anything in the library.

Using PHP 8.0.1 OS: Windows 10

Help is appreciated.

suyovj avatar Jan 21 '21 16:01 suyovj

Hello,

I do not use any windows machines so I can not test this but it seems to be a problem with your hosts configuration. Please see: https://stackoverflow.com/questions/2370388/socketexception-address-incompatible-with-requested-protocol

nekudo avatar Jan 21 '21 18:01 nekudo

Just found out that SOCK_DGRAM is not supported in Windows. The code has to be modified to enable support for windows.

Reference: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/

suyovj avatar Jan 23 '21 02:01 suyovj

Could you share your changes? Maybe I can include them in the next version.

nekudo avatar Jan 24 '21 07:01 nekudo

This is my problem and I could not fix any action I had to do and I had to use the old versions

Screenshot (450)

sirj3x avatar Jan 27 '21 10:01 sirj3x

Can you please try to change $this->icpSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0); to $this->icpSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);

and check if that works on a windows machine?

nekudo avatar Jan 27 '21 10:01 nekudo

Can you please try to change $this->icpSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0); to $this->icpSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);

and check if that works on a windows machine?

No

cochmedia avatar Apr 14 '21 14:04 cochmedia

Hello,

By changing a couple of lines I solved it. in Server.php

private string $host;
private int $port;

then,

$this->host = $host;
    $this->port = $port;

then,

if (substr(php_uname(), 0, 7) == "Windows"){
        $this->icpSocket = socket_create(AF_INET, SOCK_DGRAM, 0);
        $ipcSocketPath = $this->host;
    } else {
        if (file_exists($ipcSocketPath)) {
            unlink($ipcSocketPath);
        }
        $this->icpSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
    }

then,

if (substr(php_uname(), 0, 7) == "Windows") {
        $from = '';
        $port = 0;
        $bytesReceived = socket_recvfrom($this->icpSocket, $buffer, 65536, 0, $from, $port);
    } else {
        $bytesReceived = socket_recvfrom($this->icpSocket, $buffer, 65536, 0, $this->ipcSocketPath);
    }

in Application.php, removed final at line 19

Thanks a lot.

MasumNishat avatar Sep 03 '21 21:09 MasumNishat

Hello! @MasumNishat solution works. Could you add it in the new version?

PPhucker avatar Sep 07 '21 08:09 PPhucker

@MasumNishat If you create a pull-request I'm happy to merge these changes into a new release.

nekudo avatar Sep 15 '21 12:09 nekudo