PHP socket link. can I set the time? How to solve

Source: Internet
Author: User
PHP socket link. can I set the time? PHPcode $ socketsocket_create (AF_INET, SOCK_STREAM, SOL_TCP); $ IP $ row [IP]; $ connectionsocket_connect ($ socket, $ IP, 2012); the default value is 30 seconds, if it cannot be connected, it will take too long for the PHP socket link to be connected. can you set the time?
PHP code
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);  $IP=$row['IP']; $connection = socket_connect($socket, $IP, 2012);  

It seems that the default value is 30 seconds. if it cannot be connected, it will take too long. can I set it to only 10 seconds?

------ Solution --------------------
There are actually a lot of useful references in the user comment area of the online manual, you can search for timeout in the http://php.net/manual/en/function.socket-get-option.php, because this problem is very common, so there will certainly be.
If you are too lazy, extract a piece of code for you.

SO_SNDTIMEO Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. array. the array will contain in two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value.
To set a socket timeout value (assuming you 've set it blocking) use:

PHP code
Socket_set_option ($ socket, SOL_SOCKET, // socket level SO_SNDTIMEO, // timeout option array ("sec" => 10, // Timeout in seconds "usec" => 0 // I assume timeout in microseconds ));
------ Solution --------------------
Discussion

Reference:

There are actually a lot of useful references in the user comment area of the online manual, you can search for timeout in the http://php.net/manual/en/function.socket-get-option.php, because this problem is very common, so there will certainly be.
If you are too lazy, extract a piece of code for you.

SO_SNDTIMEO Reports the timeout value specifying ......

------ Solution --------------------
In this way, the connected php code is put into a separate file, accessed using AJAX, and the result is put into a p

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.