PHP Fsockopen Detailed Instance Code _php tutorial

Source: Internet
Author: User
*/
$FP =fsockopen ("127.0.0.1", 80); Open Data Flow
if (! $fp)//If an error is turned on
{
echo "Unable to Openn"; Output content
}
else//If open successfully
{
Fwrite ($fp, "get/http/1.0rnrn"); Writing content to a data stream
Stream_set_timeout ($FP, 2); To make a timeout setting
$res =fread ($FP, 2000); Read content
$info =stream_get_meta_data ($FP); Get the data Flow header
Fclose ($FP); Turn off Data flow
if ($info [' timed_out '])//If timed out
{
Echo ' connection timed out! '; Output content
}
Else
{
Echo $res; Output Read content
}
}

Example Two

Create a service-side
$socket =stream_socket_server ("tcp://0.0.0.0:8000", $errno, $ERRSTR);
If creation fails
if (! $socket)
{
echo "$errstr ($errno)
n ";
}
If the creation succeeds
Else
{
Accept Connection
while ($conn =stream_socket_accept ($socket))
{
Write Data
Fwrite ($conn, ' The local time is '. Date (' n/j/y g:i a '). " n ");
Close connection
Fclose ($conn);
}
Close socket
Fclose ($socket);
}

//

$file = "Test.txt"; Definition file
$FP =fopen ($file, "w"); Open Data Flow
if ($FP)//If Open successfully
{
Stream_set_write_buffer ($fp, 0); Set buffer
Fwrite ($fp, $output); Write content
Fclose ($FP); Turn off Data flow
}


http://www.bkjia.com/PHPjc/444820.html www.bkjia.com true http://www.bkjia.com/PHPjc/444820.html techarticle */$fp =fsockopen (127.0.0.1,80);//Open Data stream if (! $fp)//If open error {echo unable to openn;//output} else//if successfully opened {fwrite ($fp, get /HTTP/1.0RNRN);//To number ...

  • 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.