Php Sockets vs Streams

后端 未结 2 1777
别那么骄傲
别那么骄傲 2021-01-30 09:36

I think php sockets and php streams are overlapping each other.
I\'ve managed to make a CLI PHP chat client and a server, using either sockets or streams.

Here som

相关标签:
2条回答
  • 2021-01-30 09:47

    According to the manual, the sockets extension is more low-level. For instance, whith sockets you have finer-grained control when creating one, and can choose SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, etc.

    The socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client.

    For a more generic client-side socket interface, see stream_socket_client(), stream_socket_server(), fsockopen(), and pfsockopen().

    source: http://www.php.net/manual/en/intro.sockets.php

    0 讨论(0)
  • 2021-01-30 09:57

    As you pointed out, 'streams' are in PHP core (built-in, always available) while 'sockets' are part of a rarely included extension. Other than that, they are nearly identical. You can use both TCP and UDP with streams with both as well as blocking and non-blocking modes, which covers 99% of all use-cases.

    The only common exception I can think of is ICMP. For example, 'ping'. However, it looks like there currently isn't a safe way to do ICMP from PHP. Such calls require SOCK_RAW via the socket extension, which requires 'root' privileges to execute. Also, not all routers will route other packet types outside of TCP, UDP, and ICMP. This limits the usefulness of the socket extension.

    0 讨论(0)
提交回复
热议问题