PHP Socket,server,cient, Analog post

Source: Internet
Author: User
Tags local time socket

Fsockopen

to Socket Way to open a connection

my most common use is simulation. Post

this is from Xiao Li's excavation. ... Good

as to the simulation Get Way .... Direct file_get_content it's okay. .

Code See back

Stream_socket_client, code See back

this and Fsockopenfsockopen same . to Socket Way to open a connection , just different parameters .

Stream_socket_server

Build a Socket Server End , code See back

if it was built to be TCP of the Server will use stream_socket_accept for communication

if it was built to beUDPof theServerwill useStream_socket_recvfromandStream_socket_sendtofor communication,andStream_socket_serveryou need to add a parameter to theStream_server_bind

  p>

There is also a < Font face= "Times New Roman" >socket extended This is the most basic building of the socket, but from lang=" "en-us" > 5.3.0 pecl

we're not going to introduce you here. .

----------------------------Stream_socket Client---------------------------

<?php

/**

* @name test.php

* @date Sun 00:49:00 CST 2008

* @copyright Mayong (myz)

* @author Mayong (myz)

* @link http://blog.111cn.net/mayongzhan/

 */

$xport = "TCP";

$port = "8001";

$ip = "127.0.0.1";

$address = "{$xport}://{$ip}:{$port}";

$fp = Stream_socket_client ($address, $errno, $ERRSTR, 1);

if (! $fp) {

echo "$errstr ($errno) <br/>";

} else {

fwrite ($fp, "");

Echo fread ($FP, 1024);

fclose ($FP);

}

?>

----------------------------Streamsocketserver---------------------------

<?php

/**

* @name test2.php

* @date Sun 00:45:57 CST 2008

* @copyright Mayong (myz)

* @author Mayong (myz)

* @link http://blog.111cn.net/mayongzhan/

 */

Header ("Content-type:text/html;charset=utf-8");

// Set No timeout . The service side certainly cannot timeout

Set_time_limit (0);

// Get available Socket

$xportlist = Stream_get_transports ();

echo "Transports:";

foreach ($xportlist as $value) {

echo "{$value}";

}

// define something

$xport = "TCP";

$port = "8001";

$address = "{$xport}:// 0.0.0 . 0:{$port}";

// Establish Socketserver

if ($xport = = ' TCP ') {

$socket = Stream_socket_server ($address, $errno, $ERRSTR);

}

ElseIf ($xport = = ' udp ') {

$socket = Stream_socket_server ($address, $errno, $errstr, Stream_server_bind);

}

if (! $socket) {

echo ' {$errstr} ({$errno}) ';

}

else {

echo "Listening {$xport}:{$port} ... ";

if ($xport = = ' TCP ') {

/ / license one socket connection , -1 timeout

while ($conn = Stream_socket_accept ($socket,-1)) {

                     // ports that are accessed

$peer = Stream_socket_get_name ($conn, true);

echo "$peer";

fwrite ($conn, ' ' The local time '. Date ("y-m-d h:i:s") );

fclose ($conn);

              }

       }

elseif ($xport = = ' udp ') {

do {

                     // ports that are accessed

$pkt = Stream_socket_recvfrom ($socket, 1, 0, $peer );

echo "$peer ";

stream_socket_sendto ($socket, Date ("Y-m-d h:i:s"), 0, $peer );

while ($pkt!== false);

       }

/ / close socket

fclose ($socket);

}

?>

----------------------------Fsockopen (POST)---------------------------

<?php

/**

* @name test.php

* @date Sat 23:01:23 CST 2008

* @copyright Mayong (myz)

* @author Mayong (myz)

* @link http://blog.111cn.net/mayongzhan/

 */

/**

* php send post request

 *

* @param string $url address submitted to

* @param array $data The parameter array to be submitted (' A ' => ', ', ' => ');

* @return string

 */

function Virtualpost ($url, $data) {

$url = Parse_url ($url);

If (! $url) return "URL cannot parse ";

if (!isset ($url [' Port ']) $url [' port '] = ";

if (!isset ($url [' query ']) $url [' query '] = ";  

$encoded = "";

while (the list ($k, $v) = each ($data)) {

$encoded. = ($encoded? "&": "");

$encoded. = Rawurlencode ($k). = ". Rawurlencode ($v);

       }

//$fp = Stream_socket_client ($url [' Host ']. ":". ( $url [' Port ']? $url [' Port ']: 80));

$fp = fsockopen ($url [' Host '], $url [' Port ']? $url [' Port ']: 80);

If (! $fp) return " cannot open to $url [host] connection ";

       // Send

fputs ($fp, sprintf ("POST%s%s%s http/1.0", $url [' Path '], $url [' query ']? "?": ", $url [' query ']));

fputs ($fp, "Host: $url [Host]");

fputs ($fp, "content-type:application/x-www-form-urlencoded");

fputs ($fp, "Content-length:". Strlen ($encoded). " ");

fputs ($fp, "connection:close");

fputs ($fp, "$encoded");

       // Accept

$line = fgets ($fp, 1024);

if (!eregi ("^HTTP/1. ", $line) " return "error returned " ;

       // filter out empty lines

$results = "";

$inheader = 1;

while (!feof ($fp)) {

$line = fgets ($fp, 1024);

              // filter out the rest of the head information

if ($inheader && ($line = = "" | | $line = = "")) {

$inheader = 0;

}elseif (! $inheader) {

$results. = $line;

              }

       }

fclose ($FP);

return $results;

}

echo virtualpost (' http://127.0.0.1/test/test2.php ', Array (myz=> ' Mayong account ' ));

?>

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.