Which of these two so开发者_如何学Ccket connectivity options are more efficient to use, in terms of having better performance and less overhead?
socket_connect family or fsockopen?
As far as I know the performance is very much the same, but the result of the connection is different after initialization. For example fsockopen()
will open the connection and immediately go to LISTENING
-state, where socket_create()
will just create the connection and socket_open()
put it in LISTENING
-state.
You could say: fsockopen() === socket_open(socket_create(), ADDRESS)
Furthermore, after you do socket_shutdown()
and socket_close()
, you will leave the connection in TIME_WAIT
-state, which will will make the socket wait until the receiving end of the socket disconnects, which can take a while.
All in all, there quite the same, choose the one you like the best and suits your intention best. If you want to pre-create all sockets but only connect in a certain time, go with socket_create()
, else use fsockopen()
as it's easier.
If you have your own webserver and no intention of distributing your code, you can also take a look into cURL, which is essentially the same as both other options but has more failsaves built-in.
精彩评论