Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a socket resource into a stream socket

PHP has two different APIs for interacting with sockets. There is the low level socket API which basically wraps the C socket API. And there is the high level stream sockets API, which implements the PHP stream interface.

Unfortunately, the stream socket API does not support setting low level socket options. However, this is something that I have to do. Likewise, the socket API does not support using standard function calls like fread, fwrite and fclose, making it incompatible with the rest of my code.

PHP 5.4 introduced the socket_import_stream function. This allows you to take a stream socket and get the underlying socket resource. My plan was to use this to create the stream socket, get the socket, set some options on it, and then continue using the original stream socket.

The reason this did not work for me is that I need to set the options before binding. The only way to bind a stream socket is to use stream_socket_server, which already performs the bind. That's why I could not use it.

I am now looking for the inverse of socket_import_stream, so that I can convert my socket resource into a stream socket. I have not been able to find such a function, but I'm hoping that some very smart people can help me. Or submit a patch to the PHP source that does it. Or give me hints on writing such a patch.

EDIT: I have some code that acts on a PHP stream to parse DNS packets from it. I want to re-use that code with a multicast-enabled socket. I cannot enable multicast on a stream socket, and I cannot use stream functions on a raw socket.

EDIT2: I want to use this stream with stream_select, so custom stream wrappers are not an option, unfortunately.

like image 363
igorw Avatar asked Sep 03 '12 00:09

igorw


People also ask

What is socket stream?

Stream sockets allow processes to communicate using TCP. A stream socket provides bidirectional, reliable, sequenced, and unduplicated flow of data with no record boundaries. After the connection has been established, data can be read from and written to these sockets as a byte stream. The socket type is SOCK_STREAM .

What is Stream_socket_client?

stream_socket_client — Open Internet or Unix domain socket connection.


1 Answers

You can use the stream_wrapper_register function in combination with a class which implements the streamWrapper template to create a multicast socket stream. This will allow you to leverage all of the built in stream functions, though it is not as convenient as socket_export_stream.

like image 162
james_t Avatar answered Oct 02 '22 07:10

james_t