Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference betweens php socket I/O functions

Tags:

php

sockets

Long story short, there're so many socket I/O functions in php seems doing the same thing.

So I'm wondering is there any difference between fread() and stream_get_contents() and stream_socket_recvfrom() ?

Also fwrite() and stream_socket_sendto(), they're all look same, which should I choice ?

Thank you for viewing my question, and I will appreciate for any advice~

like image 534
NotUser9123 Avatar asked Aug 17 '26 22:08

NotUser9123


1 Answers

Inconsistency and redundancy seem to be hallmarks of PHP unfortunately: you do have a lot from which to choose. And streams are just plain confusing frankly.

I think what's important to keep in mind in this case is that some functions that operate on sockets are generic and others are more specific. The PHP streams API attempts to provide a way to generalize file/network operations and therefore provides generic functions for common operations such as fwrite and fread. That's why you can fopen a Web page, local file or compressed archive all with the same function. However the stream_socket_*() family of functions are more specific and provide extra functionality that pertain only to sockets. For example, the stream_socket_recvfrom() and stream_socket_sendto() calls allow for out-of-band data channels (essentially a way to multiplex another data stream using a single connection).

I'll try to compare/contrast some of the generic versus specific PHP functions as they apply to sockets.

  • fopen() vs stream_socket_client(): You can fopen a TCP connection for example but suppose you wanted to get more information about the connection status or handle timeouts. The stream_socket_client() function allows you to set connection timeout status and get more advanced error feedback when a connection is unsuccessful. It also allows you to connect asynchronously (i.e. in non-blocking mode).

  • fread()/fwrite() vs stream_socket_sendto()/stream_socket_recvfrom(): As already stated you can receive/send out-of-band data using the later set of functions; you can also encapsulate the connection and read/write operations into a single call.

  • fclose() vs stream_socket_shutdown(): These aren't exactly analogous but the names imply something similar. You still want to call fclose to free the socket and any associated memory. The function of stream_socket_shutdown is to shutdown a channel (either send or receive) in the underlying TCP connection. For example, you can stop sending on a full-duplex socket but still keep reading.

Note: this next comparison isn't really a comparison between a generic, stream function and a socket-specific function. It is a comparison of two generic stream functions that happen to do different things. I included it since you mentioned it in the question.

  • fread() vs stream_get_contents(): fread reads up to a pre-determined amount of bytes; stream_get_contents reads the remaining data on the stream. You can see this is pretty much a convenience function that can improve performance. However there are times when you need to stream the data in using fread such as when you expect a lot of data that can't fit into main memory.

Hopefully you get the idea. A lot of this parallels the structure of the lower-level programming interfaces. In Linux, for example, the interfaces for working with I/O devices are polymorphic. You can read() on a file, domain socket, stream socket, datagram socket, pipe, fifo, ETC. However there are functions for specifically operating on a certain type of I/O device (e.g. send() is called only on sockets).

I would recommend choosing what best fits your needs and not predispose yourself to any general rules regarding what to use. For example, if you need to GET a Web page over HTTP, just use file_get_contents. If you know the response is going to be huge you might want to fopen it and stream the result to disk. If you need to implement a client for a custom protocol then I would use the more specific stream socket family of functions that give you more fine tuned control.

like image 178
Roger Gee Avatar answered Aug 20 '26 12:08

Roger Gee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!