Php Sockets vs Streams

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.php

Here some illustrating code lines:
Using sockets:app

...
$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:socket

...
$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.tcp

I know that Streams is part of PHP core and Sockets is an extension.ide

My question(s) is(are):.net

  • 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?

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.code

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.server

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

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

相關文章
相關標籤/搜索