Fsockopen detailed instance code in php

Source: Internet
Author: User

*/
$ Fp = fsockopen ("127.0.0.1", 80); // open the data stream
If (! $ Fp) // if an error occurs
{
Echo "unable to openn"; // output content
}
Else // if it is enabled successfully
{
Fwrite ($ fp, "get/http/1.0 rnrn"); // write content to the data stream
Stream_set_timeout ($ fp, 2); // set the timeout
$ Res = fread ($ fp, 2000); // read content
$ Info = stream_get_meta_data ($ fp); // get the data stream Header
Fclose ($ fp); // close the data stream
If ($ info ['timed _ out']) // if timeout
{
Echo 'Connection timed out! '; // Output content
}
Else
{
Echo $ res; // output read content
}
}

// Instance 2

// Create a server
$ Socket = stream_socket_server ("tcp: // 0.0.0.0.0: 8000", $ errno, $ errstr );
// If creation fails
If (! $ Socket)
{
Echo "$ errstr ($ errno) <br/> n ";
}
// If creation is successful
Else
{
// Accept the connection
While ($ conn = stream_socket_accept ($ socket ))
{
// Write data
Fwrite ($ conn, 'the local time is '. date ('n'/j/y g: I A'). "n ");
// Close the connection
Fclose ($ conn );
}
// Close the socket
Fclose ($ socket );
}

//

$ File = "test.txt"; // definition file
$ Fp = fopen ($ file, "w"); // open the data stream
If ($ fp) // if enabled successfully
{
Stream_set_write_buffer ($ fp, 0); // you can specify a buffer.
Fwrite ($ fp, $ output); // write content
Fclose ($ fp); // close the data stream
}


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.