Php socket-based basic communication and phpsocket Communication

Source: Internet
Author: User

Php socket-based basic communication and phpsocket Communication
Php socket-based basic communication 1. Preface

Socket is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. for users, a set of simple interfaces are all, let the Socket organize the data to conform to the specified protocol.

There are many such introductions. I hope to learn more about Baidu. If you look down, remember to enable socket extension first;

2. Create a socke Server

Basic steps:

  • Initialize 86 socket
  • Port binding
  • Port for listening
  • Call accept to block
  • Waiting for client connection
3. server code implementation
<? Phpecho "***************** server ****************** \ r \ n "; // set the IP address and port (the port must not be occupied and allow external access) $ ip = "127.0.0.1"; $ port = 1935; // timeout design set_time_limit (0); // create a socket $ socket = socket_create (AF_INET, SOCK_STREAM, 0) or die ("failed to create a socket because: {socket_strerror ($ socket)} \ r \ n "); // bind the socket to the specified ip address and port $ ret = socket_bind ($ socket, $ ip, $ port) or die ("failed to bind socket, cause: {socket_strerror ($ ret)} \ r \ n"); // listen to socket, maximum number of equal values is 4 $ ret = socket_l Isten ($ socket, 4) or die ("failed to listen to socket because: {socket_strerror ($ ret)} \ r \ n"); // count $ count = 0; echo "waiting for connection !!! \ R \ n "; do {// receive request connection // call the sub-socket processing information $ msgsock = socket_accept ($ socket); if (! $ Msgsock) {echo "socket blocking failed because: {socket_strerror ($ msgsock)} \ r \ n" ;}$ msg = "<p style = 'color: red'> connection successful </p> "; // process client input and return data socket_write ($ msgsock, $ msg, strlen ($ msg )); $ buf = socket_read ($ msgsock, 8192); $ talkback = "received message: {$ buf} \ r \ n"; echo $ talkback; if (++ $ count> 5) {break;} socket_close ($ msgsock);} while (true); socket_close ($ socket );

 

4. Create a slient Client

Basic steps:

  • Use TCP to create a socket Resource
  • Connect to the socket server
  • Socket_write
  • Socket_read receives data
  • Disable socket Resources
5. client code implementation
<? Phpecho '***************** client ****************** <br/> '; // set IP address and port $ port = 1935; $ ip = '2017. 0.0.1 '; // timeout design set_time_limit (0); // create a TCP socket resource $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die ('socket _ create failed: '. socket_strerror ($ socket); echo 'created successfully <br/> '; $ restult = socket_connect ($ socket, $ ip, $ port ); echo 'Connection successful <br/> '; $ in =' A sokcet client is created successfully, with a random number = '. rand (); if (socket_write ($ socket, $ in, strlen ($ in) {echo 'sent successfully, sent information is '. $ in. '<br/>';} else {echo 'failed to send because '. $ socket_strerror ($ socket ). '<br/>';} while ($ out = socket_read ($ socket, 8192) {echo 'message received successfully, information is '. $ out. '<br/>';} echo 'socket closed <br/> '; socket_close ($ socket); echo' closed successfully <br/> ';

 

6. Enable Customer Service 7. Use the client to connect to the server

Server Status:

The basic socket connection is almost the same here.

8. Related lines

8.1. socket_create

This interface is used to create a socket resource. If a socket is successfully returned, false is returned for failure;

Http://www.php.net/manual/zh/function.socket-create.php for details

8.2. socket_bind

If a name is bound to the socket, true is returned. If a failure is returned, false is returned;

Http://php.net/manual/en/function.socket-bind.php for details

8.3. socket_listen

Listen to a socket resource. If yes, return true. If no, return false;

Http://php.net/manual/zh/function.socket-listen.php for details

,

8.4. socket_accept

If a sub-socket is created, a new socket resource is returned successfully. If the socket resource fails, false is returned;

Http://www.php.net/manual/en/function.socket-accept.php for details

8.5. socket_read

Read the information sent by the client. The read information is returned successfully. Otherwise, an empty string is returned.

(Socket_read () returns a zero length string ("") when there is no more data to read .)

Http://www.php.net/manual/en/function.socket-read.php for details

8.6. socket_write

Want to write data to the cache through socket

Http://php.net/manual/en/function.socket-write.php for details

8.7. socket_close

Disable socket Resources

Http://php.net/manual/zh/function.socket-close.php for details

8.8. socket_strerror

Get error information

Http://php.net/manual/zh/function.socket-strerror.php for details

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.