PHPSocket programming started

Source: Internet
Author: User
PHPSocket programming started, read PHPSocket programming started, let's start with a simple example-a TCP service that receives the input string, processes and returns the string to the client. below is the corresponding code: PHP code :? // Set some basic variables $ host = "> <LINKhref =" http://www.php100.com//statics/

Let's start with a simple example: a TCP service that receives the input string, processes it, and returns it to the client. The corresponding code is as follows:

PHP code:

// Set some basic variables
$ Host="192.168.1.99";
$ Port=1234;
// Set the timeout value
Set_time_limit(0);
// Create a Socket
$ Socket=Socket_create(AF_INET,SOCK_STREAM,0) Or die ("Cocould not create
Socket \ n");
// Bind the Socket to the port
$ Result=Socket_bind($ Socket,$ Host,$ Port) Or die ("Cocould not bind
Socket \ n");
// Start listening link
$ Result=Socket_listen($ Socket,3) Or die ("Cocould not set up socket
Listener \ n");
// Accept incoming connections
// Another Socket for communication
$ Spawn=Socket_accept($ Socket) Or die ("Cocould not accept incoming
Connection \ n");
// Obtain client input
$ Input=Socket_read($ Spawn,1024) Or die ("Cocould not read input \ n");
// Clear the input string
$ Input=Trim($ Input);
// Process client input and return results
$ Output=Strrev($ Input)."\ N";
Socket_write($ Spawn,$ Output,Strlen($ Output) Or die ("Cocould not write
Output \ n");
// Disable sockets
Socket_close($ Spawn);
Socket_close($ Socket);
?>
The following is a detailed description of each step:

1. the first step is to create two variables to save the IP address and port of the server running the Socket. you can set your own server and port (this port can be a number between 1 and 65535), provided that this port is not used.

PHP code: // Set two variables
$ Host="192.168.1.99";
$ Port=1234;
?>
2. You can use the set_time_out () function on the server side to ensure that PHP will not time out while waiting for client connection.

PHP code: // Timeout
Set_time_limit(0);
?>
3. On the basis of the above, we should use the socket_creat () function to create a Socket. --- this function returns a Socket handle, which will be used in all future functions.

PHP code: // Create a Socket
$ Socket=Socket_create(AF_INET,SOCK_STREAM,0) Or die ("Cocould not create socket \ n");
?>
The first parameter "AF_INET" is used to specify the domain name;
The second parameter "SOCK_STREM" tells the function what type of Socket will be created (TCP type in this example)

Therefore, if you want to create a UDP Socket, you can use the following code:

PHP code: // Create a socket
$ Socket=Socket_create(AF_INET,SOCK_DGRAM,0) Or die ("Cocould not create socket \ n");
?>
4. Once a Socket handle is created, the next step is to specify or bind it to the specified address and port. This can be done through the socket_bind () function.

PHP code: // Bind a socket to the specified address and port
$ Result=Socket_bind($ Socket,$ Host,$ Port) Or die ("Cocould not bind to socket \ n");
?>
5. after the Socket is created and bound to a port, you can start listening for external connections. PHP allows you to start a listener using the socket_listen () function, and you can specify a number (in this example, the second parameter is 3)

PHP code: // Start listening for connection
$ Result=Socket_listen($ Socket,3) Or die ("Cocould not set up socket listener \ n");
?>
6. up to now, your server has not done anything except waiting for connection requests from clients. once a client connection is received, the socket_assept () function starts to take effect. It receives connection requests and calls another sub-Socket to process client-server information.

PHP code: // Accept the request link
// Call the sub-socket to process the information
$ Spawn=Socket_accept($ Socket) Or die ("Cocould not accept incoming connection \ n");
?>
This sub-socket can now be used by the subsequent client-server communication.

7. after a connection is established, the server will wait for the client to send some input information, which can be obtained by The socket_read () function and assigned to the $ input variable of PHP.

PHP code: // Read client input
$ Input=Socket_read($ Spawn,1024) Or die ("Cocould not read input \ n");
?>
The second parameter of socker_read is used to specify the number of bytes to read. You can use it to limit the size of data obtained from the client.

Note: The socket_read function reads the shell data until \ n, \ t, or \ 0 characters are met. The PHP script regards this write character as an input Terminator.

8. now the server must process the data sent from the client (in this example, only the data is input and returned to the client ). this part can be done by the socket_write () function (making it possible to send a data stream back to the client by the communication socket)

PHP code: // Process client input and return data
$ Output=Strrev($ Input)."\ N";
Socket_write($ Spawn,$ Output,Strlen($ Output) Or die ("Cocould not write output \ n");
?>
9. Once the output is returned to the client, the parent/child socket should be terminated through the socket_close () function.

PHP code: // Disable sockets
Socket_close($ Spawn);
Socket_close($ Socket);
?>

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.