Php Sockets vs Streams
Solution 1
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
Solution 2
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.
Related videos on Youtube
Comments
-
Catalin Enache almost 2 years
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 some illustrating code lines:
Using sockets:... $main_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Cannot create socket.\n"); @socket_bind($main_socket, $host, $port) or die("Could not bind to socket $host : $port.\n"); @socket_listen($main_socket, 5) or die("Could not set up socket listener\n"); ...
Using streams:
... $main_socket = @stream_socket_server ("tcp://$host:$port", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN) or die("Cannot create socket.\n"); $clients = array($main_socket); $clients_peername = array(array('port' => $port)); fputs(STDOUT, "Waiting for connections...\n"); ...
The point here is that a client and a server could be made using either sockets functions, either streams functions.
I know that Streams is part of PHP core and Sockets is an extension.
My question(s) is(are):
- What is the difference between sockets and streams when referring to sockets programming?
- Are there any capabilities, related to sockets programming, that one can have while the other one cannot?
-
Catalin Enache about 12 years@JamWaffles Thanks for editing and sorry for my english.
-
Bojangles about 12 yearsNo problem. Your English is nearly perfect, so there's nothing to worry about :)
-
Pacerier almost 11 yearsDoes more low-level mean faster?
-
serans over 10 yearsNot necessarily, it just means you have more control over what's going on. You might use that finer control to improve speed in some cases though.