Application Example Analysis _php tutorial based on PHP socket (fsockopen)

Source: Internet
Author: User
Fsockopen function can be used, first of all to open the php.ini in the Allow_url_open=on;
Fsockopen is the encapsulation of the socket client code, which encapsulates the socket_create,socket_connect.
Server-side code: server.php
Copy CodeThe code is as follows:
Error_reporting (E_all);
Set_time_limit (0);
$address = ' 127.0.0.1 ';
$port = 10008;
Creating ports
if ($sock = Socket_create (Af_inet, sock_stream, sol_tcp) = = = = False) {
echo "Socket_create () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n";
}
Binding
if (Socket_bind ($sock, $address, $port) = = = = False) {
echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}
Listening
if (Socket_listen ($sock, 5) = = = = False) {
echo "Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). "\ n";
}
while (true) {
Get a link
if ($msgsock = socket_accept ($sock)) = = = = False) {
echo "Socket_accepty () Failed:reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
Break
}
Welcome Send to Client
$msg = "1.server send:welcome
";
Socket_write ($msgsock, $msg, strlen ($msg)); Return information to the client
Echo ' read client message\n ';
$buf = Socket_read ($msgsock, 8192); Get the information that the client sent over
$talkback = "2.received message: $buf \ n";
Echo $talkback;
if (false = = = Socket_write ($msgsock, $talkback, strlen ($talkback))) {//return information to the client
echo "Socket_write () failed reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";
} else {
Echo ' send success ';
}
Socket_close ($msgsock);
}
Socket_close ($sock);

Client code: fsocket.php
Copy CodeThe code is as follows:
$fp = Fsockopen ("127.0.0.1", 10008, & $errno, & $errstr, 10);
if (! $fp) {
Echo $errstr. " (". $errno. ")
n ";
} else {
$in = "head/http/1.1\r\n";
$in. = "host:localhost \ r \ n";
$in. = "connection:close\r\n\r\n";
Fputs ($fp, $in);
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}
Fclose ($FP);
}

http://www.bkjia.com/PHPjc/327077.html www.bkjia.com true http://www.bkjia.com/PHPjc/327077.html techarticle fsockopen function can be used, first to open the php.ini in the Allow_url_open=on, Fsockopen is the socket client code encapsulation, the function encapsulates the socket_create,socket_ Connect Service ...

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