Fgets is slow. Is there any other way?

Source: Internet
Author: User
Fgets is slow. Is there any other method:
$fp = fsockopen($server, $port, $errno, $errstr ,30);fputs($fp, $send_all);stream_set_timeout($fp, 30);$result = '';while(!feof($fp)) {   $result.= fgets($fp,1024);}print_r($result);


Although the data can be retrieved, it takes more than 20 seconds.

Is there any other way to read data? Do not use fread. use fread to retrieve complete data.


Reply to discussion (solution)

What about curl?

Slow because of network connection, unless the hardware environment is changed, how to change the code will not help

Slow because of network connection, unless the hardware environment is changed, how to change the code will not help

I read the documents and do not quite understand the principles of fread and fgets.
Fread can read all data at a time very quickly, but cannot obtain the complete data. it can only take more than one thousand bytes.
While fgets can get all of the 1024 values each time. It is very slow.

Does fgets need to be re-linked every time, so if there are more than a dozen cycles, it will slow down every time it is superimposed?

Fgets and fread are not directly involved in the connection. When packets arrive, they are stored in the buffer zone. they read the local buffer zone, so the speed is not affected.

Fread reads data blocks of the specified size until the end of the file. If the end of the file cannot be determined, a wait will occur, resulting in failure.
When fgets reads data blocks of the specified size, it will stop reading the data blocks with a line break until the end of the file.

Attackers can obtain remote data by using file_get_contents.

If your entire file is no larger than 8 kB, remove 1024.

The cause is found.

The MTU package on the server is limited.

Http://blog.csdn.net/jjjfew/article/details/6413460

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.