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.