What is the preferred method for reading/writing to TCP/IP sockets in PHP? There are many, many ways of doing it, including:
fread()andfwrite()fgets()andfputs()file_get_contents()andfile_put_contents()stream_get_contents()stream_socket_recvfrom()andstream_socket_sendto()- probably several others that I’ve not run across…
I realize that fgets() and fputs() operate a bit differently (by lines instead of arbitrary chunks of data), but I really don’t understand the differences in the others and which method would be best for what purposes.
Edit: Also socket_recv() and socket_send().
Edit #2: Also socket_read() and socket_write().
With many of these functions, one can specify offsets and max lengths, providing much overlap (and confusion) between them. Generally, however, they are used as described here:
fread, fwrite, stream_socket_recvfrom, stream_socket_sendto read/write a specific number of bytes at a time.
fgets, fputs read/write a line at a time.
file_get_contents, file_put_contents, stream_get_contents read/write entire streams of content at a time.