Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?
like image 750
Catalin Enache Avatar asked Mar 18 '12 17:03

Catalin Enache


People also ask

What are PHP sockets?

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.

What is Stream_socket_client?

stream_socket_client — Open Internet or Unix domain socket connection.

What is a stream PHP?

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.

Can we use socket in PHP?

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.


1 Answers

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

like image 67
serans Avatar answered Oct 06 '22 00:10

serans