PHP Socket Basics | server-side server.php| client client.php

Source: Internet
Author: User
PHP Socket basic principle, the basic use method:
Server: Create a socket socket = "bind this socket to the specified IP and port =" Listen for all link requests for this socket = "Response or return data
Client: Create a socket socket = "Connection needs to request socket =" Send data to socket = "Read the socket response data

(~_~) understand the wrong place also ask classmate Correction ~ ~ ~
  1. server.php
  2. $host = ' 192.168.0.10 ';
  3. $port = 88888;
  4. if ($socket = Socket_create (Af_inet, Sock_stream, sol_tcp)) {
  5. echo "Socket create success.\n";
  6. }
  7. if ($bind = Socket_bind ($socket, $host, $port)) {
  8. echo "socket bind success.\n";
  9. }
  10. if ($listen = Socket_listen ($socket)) {
  11. echo "Socket listening...\n";
  12. }
  13. if ($connect = socket_accept ($socket)) {
  14. echo "Socket accept a connect success.\n";
  15. }
  16. //===================================================//
  17. Socket_write ($connect, ' I am from server.php at DateTime: '. Date (' y-m-d h:i:s '). " \ n ");
  18. ==================== sent to client ==================================================//
  19. //==================================================//
  20. $read = Socket_read ($connect, 1024);
  21. Echo $read;
  22. ============ receives the message from the client ============================//
  23. while ($connect = Socket_accept ($socket)) {
  24. $read = Socket_read ($connect, 1024);
  25. Echo $read;
  26. //}
  27. Loop Accept
Copy Code
    1. client.php
    2. $host = ' 192.168.0.10 ';
    3. $port = 88888;
    4. if ($socket = Socket_create (Af_inet, Sock_stream, sol_tcp)) {
    5. echo "Socket create success.\n";
    6. }
    7. Socket_connect ($socket, $host, $port);
    8. //=======================================================//
    9. Socket_write ($socket, ' I am from client.php at DateTime: '. Date (' y-m-d h:i:s '). " \ n ");
    10. ================== sends a request to the server ================================================//
    11. //=======================================================//
    12. $read = Socket_read ($socket, 1024);
    13. Echo $read;
    14. =============== accept the return data of the server ========================//
Copy Code
  • Related Article

    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.