Basic principles of phpsocket | server. php | client. php

Source: Internet
Author: User
Basic principles of phpsocket | server. php | client. php
Basic principles of php socket:
Server: Create a socket = bind the socket to the specified IP address and port = listen to all connection requests of the socket = respond or return data
Client: create a socket = connection request socket = send data to socket = read socket response data

(~ _~) If you do not understand the problem, please correct it ~~~

  1. Server. php
  2. $ Host = '1970. 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. // ================================== Send to the client ==================== =========================================== //
  19. // ================================================ ================== //
  20. $ Read = socket_read ($ connect, 1024 );
  21. Echo $ read;
  22. // ===================Accept messages sent from the client =========================== ========== //
  23. // While ($ connect = socket_accept ($ socket )){
  24. // $ Read = socket_read ($ connect, 1024 );
  25. // Echo $ read;
  26. //}
  27. // Cyclically accept

  1. Client. php
  2. $ Host = '1970. 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. // ============================== Send a request to the server ==================== ========================================= //
  11. // ================================================ ============================== //
  12. $ Read = socket_read ($ socket, 1024 );
  13. Echo $ read;
  14. // =========================Accept the returned data from the server ======== //

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.