socket_create vs. fsockopen php

2019-01-25 05:48发布

问题:

The hosting service that I use currently does not let me use sockets, probably for good reason on their part. They do, however, let me use fsockopen. I was wondering what the difference is, because some scripts that worked with socket_create and even stream_socket_server, do not work with fsockopen. That said, if fsockopen should work, my code is listed below. What it does is it listens on its own ip address for incoming udp packets and reads them.

Thanks

$sock = fsockopen("udp://x.x.x.x", $port);
while(1)
{
    $buf = fread($sock, 200);
    flush();
    ob_flush();
}

回答1:

fsockopen creates a connection to a host, not a listening socket.

fsockopen($address) ~== socket_connect(socket_create(), $address)

Your hosting provider doesn't want you listening on alternate ports/protocols.

If what you have works, I wouldn't count on it always working as it would be a bug.



标签: php sockets udp