5

What is the preferred method for reading/writing to TCP/IP sockets in PHP? There are many, many ways of doing it, including:

  • fread() and fwrite()
  • fgets() and fputs()
  • file_get_contents() and file_put_contents()
  • stream_get_contents()
  • stream_socket_recvfrom() and stream_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().

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

2 Comments

I've studied the manual pages for all these functions before posting here. I guess I'm looking for more of an explanation why there are 5+ sets of methods that can all do roughly the same things. Even the functions which accept offsets/lengths
@drrcknlsn because that's what PHP is known for. To quote our own Jeff Atwood PHP "is like an virtual explosion at the keyword and function factory." (codinghorror.com/blog/2008/05/…) There are ton of methods because people wrote a ton of different ways to do the same thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.