Application example analysis 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

. The code is as follows:
<?php
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.<font color= ' red ' >server send:welcome</font><br/>";
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

. The code is as follows:


<?php
$fp = Fsockopen ("127.0.0.1", 10008, & $errno, & $errstr, 10);
if (! $fp) {
Echo $errstr. " (". $errno. ") <br>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);
}

Application example analysis based on PHP socket (fsockopen)

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.