PHP socket (fsockopen) functions use code

Source: Internet
Author: User
Application Example of PHP socket (fsockopen) function

Using the Fsockopen function, the first thing to do is to open the allow_url_open=on in php.ini;

Fsockopen is the encapsulation of the socket client code, which encapsulates the socket_create (), Socket_connect ().

Server-side code: server.php

 "; Socket_write ($msgsock, $msg, strlen ($msg)); Returns the message to the client echo "read client message\n"; $buf = Socket_read ($msgsock, 8192); Gets the message sent by the client $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

 
  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);}? >
  • 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.