PHP Socket server code _ PHP Tutorial

Source: Internet
Author: User
This article mainly introduces the php simple socket server client code instance. This article provides two versions of server ports, and describes the code problems and improvements, the client code for using this socket server is provided. if you need it, you can refer to the following article to introduce the php simple socket server client code instance. This article provides two versions of server ports, describes the code problems and improvements, and provides the client code for using this socket server. For more information, see

This article shares a simple socket example using php. Implement a TCP service that receives the input string, processes and returns the string to the client.

Generate a socket server

 

Tip: you should use your command prompt to run the above code. The reason is that a server is generated, not a Web page. If you try to use a Web browser to run this script, it is likely to exceed 30 seconds. You can use the following code to set an unlimited running time, but we recommend that you use a command prompt to run it.

The code is as follows:

set_time_limit(0);


Perform a simple test on the script in your command prompt:

The code is as follows:

Php.exe socket_server.php


If you have not set the PHP interpreter in the system environment variable, you can specify the detailed path for php.exe. When you run this server, you can connect to port 1337 through remote login (telnet) to test this server.

The preceding server has three problems:

1. it cannot accept multiple connections.

2. it only completes a unique command.

3. you cannot connect to this server through a Web browser.

This first problem is easy to solve. you can use an application to connect to the server every time. However, the problem is that you need to use a Web page to connect to the server, which is more difficult. You can make your server accept the connection and then send some data to the client (if it must be written), close the connection and wait for the next connection.

Make improvements based on the previous code and generate the following code for your new server:

  


What does this server do? It initializes a socket and opens a cache to send and receive data. It waits for a connection. Once a connection is generated, it prints "Socket connected" on the server screen. This server checks the buffer zone. if there is data in the buffer zone, it sends the data to the connected computer. Then, it sends the receiving information of the data. once it accepts the information, it stores the information in the data, allows the connected computer to know the information, and closes the connection. When the connection is closed, the server starts to process the next connection.

Generate a socket client

It is easy to handle the second problem. You need to generate a php page to connect to a socket, send some data to its cache, and process it. Then you have a processed data record, and you can send your data to the server. Connect to another client and it will process the data.

The following example demonstrates the use of socket:

   NO DATA

"); break; }else{ // Do something with the data in the buffer echo("

Buffer Data: " . $buffer . "

"); }}echo("

Writing to Socket

");// Write some test data to our socketif(!socket_write($socket, "SOME DATA\r\n")){ echo("

Write failed

");}// Read any response from the socket phpernote.comwhile($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)){ echo("

Data sent was: SOME DATA
Response was:" . $buffer . "

");}echo("

Done Reading from Socket

");

The code in this example demonstrates how to connect the client to the server. The client reads data. If this is the first connection to reach this loop, the server will send "no data" to the client. If this happens, the client is connected. The client sends its data to the server, and the data is sent to the server. the client waits for a response. Once a response is received, it writes the response to the screen.

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.