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):
Socket programming is responsible for establishing that connection between applications to interact. Client application sends message($message) to server($host) and the server application receives it from the client through a port($port). The client. php runs and sends the message from a client machine.
stream_socket_client — Open Internet or Unix domain socket connection.
PHP Stream Introduction Streams are the way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior.
Socket programming can be defined as the programming approach that has the server and the client as the application where a connection has to be established between both of them to facilitate the communication between them. In terms of PHP, it also lets us implement the concept of socket programming.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With