PHP simulates socket connection and sends data multiple times

Source: Internet
Author: User

Introduction: This is a detailed page for PHP to simulate a socket connection and send data multiple times. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 340844 'rolling = 'no'>

<? PHP
// Post. php
Function post ($ host, $ port)
{
// $ Host = "127.0.0.1 ";
// Establish a connection
$ Conn = fsockopen ($ host, $ port );
If (! $ Conn)
{
Die ("con error ");
}
// Send data 5 times in a loop
//
For ($ I = 0; $ I <5; $ I ++)
{
$ DATA = "user_name = admin". $ I;
Writedata ($ Conn, $ host, $ data );
Echo $ I. "<br/> ";
}

Fclose ($ conn );
}

Function writedata ($ Conn, $ host, $ data)
{
$ Header = "post/test. php HTTP/1.1 \ r \ n ";
$ Header. = "Host: {$ Host} \ r \ n ";
$ Header. = "Content-Type: Application/X-WWW-form-urlencoded \ r \ n ";
$ Header. = "Content-Length:". strlen ($ data). "\ r \ n ";
// Keep-alive is the key
$ Header. = "connection: keep-alive \ r \ n ";
$ Header. = "{$ data} \ r \ n ";

Fwrite ($ Conn, $ header );

// Obtain the result
// $ Result = '';
// While (! Feof ($ conn ))
//{
// $ Result. = fgets ($ Conn, 128 );
//}
// Return $ result;
}

Post ('1970. 0.0.1 ', 80 );

?>
 
<? PHP
// Test. php
$ Fp = fopen('result.txt ', 'A ');
$ DATA = $ _ post ['user _ name']. "--". date ('Y-m-d h: I: s '). "\ r \ n ";
Fwrite ($ FP, $ data );
Fclose ($ FP );
?>

From: csdn

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/340844.html pageno: 6.

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.