Timeout of socket

Source: Internet
Author: User
Fsockopen is often used in the timeout problem of socket recently. therefore, pay attention to its timeout settings and find that multiple settings can be configured, but google cannot open it, and baidu won't work, I had to turn to my colleagues for this basic question. Check the code. if & nbsp ;(! $ Fp & nbsp; @ fsockopen ($ host, & nbsp; $ this-& gt; port? $ This-& gt; timeout of po socket
Fsockopen is often used recently, so pay attention to its timeout settings. it is found that there are multiple settings available, but google cannot open it, and baidu won't work, I had to turn to my colleagues for this basic question.
Check the code,
If (! $ Fp = @ fsockopen ($ host, $ this-> port? $ This-> port: 80, $ errno, $ errstr, $ timeout1 )){
Fclose ($ fp );
Return false;
}
Socket_set_timeout ($ fp, $ timeout2 );

While (! Feof ($ fp )){
$ Line = fgets ($ fp, 4096 );
Fclose ($ fp );
Break;
}
Question: 1. there are two timeouts: $ timeout1 and $ timeout2. what is the difference between the two timeouts? Is the former only used to establish connections, and the latter only used to read data?
2. $ timeout2 is used to control the time limit from fsockopen to fclose, or to control the data limit for each fgets operation?
3. if the second parameter of fgets ($ fp, 4096) is 4096, I set it very large, for example, 0.1 billion seconds, do you need to set $ timeout2 to a greater value?
4. I know that fread is used to read binary data. In addition, there is a file_get_contents in php. Does the file_get_contents kernel use fread or fgets? When reading binary data, I tried to use both fgets and fread. in this case, can any binary data be read or written in the text format?
There are a lot of problems, but they are all very essential. Which of my colleagues has a high literacy level.
------ Solution --------------------
Socket_set_timeout is the alias of stream_set_timeout.
1. yes
2. waiting time before each read
That is, if no data is read within $ timeout2, it is regarded as a failure.
3. the default value is unlimited waiting. since you want to limit the waiting time, you are naturally responsible for your actions.
4. fread is used to read data in binary mode. the length is the same except the last one.
Fgets is used to read data in text mode and stops when \ n is encountered. If \ x1a is encountered, the file ends.
In terms of data, they are both binary. The so-called text is subject to some constraints.
File_get_contents and file_put_contents both read and write files in binary mode.

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.