PHP Socket Programming Process detailed

Source: Internet
Author: User
Tags bind connect variables php server php and socket string strlen

Introduced

The socket is used for interprocess communication. Interprocess communication is typically based on the client-server side model. At this point, the client-server side is an application that can interact with one another. The interaction between the client and the server needs to be connected. Socket programming is responsible for establishing an interactive connection between applications.

In this article, we'll learn how to create a simple client-server with PHP. We will also learn how client applications send messages to the server, and how to accept messages from the server side.

Using code

Objective: To develop a client to send a string message to the server, and the server will reverse the same information and return it to the client.

PHP Server

1th Step: Set variables, such as host and port

$host = "127.0.0.1";
$port = 5353;
No Timeout
Set_time_limit (0);

The port number can be any positive integer between 1024-65535.

Step 2nd: Create a socket

$socket = Socket_create (af_inet, sock_stream, 0) or die ("could not create socket\n");

3rd step: Bind socket to port and host

The socket resource created is bound to the IP address and port number.

$result = Socket_bind ($socket, $host, $port) or die ("could not bind to socket\n");

4th step: Start Socket monitoring

After binding to the IP and port, the server begins to wait for the client to connect. It waits until there is no connection.

$result = Socket_listen ($socket, 3) or die ("could not set up socket listener\n");

5th step: Accept the connection

This function accepts the incoming connection request from the socket being built. After accepting a connection from a client socket, the function returns another socket resource, which is actually responsible for communicating with the corresponding client socket. The "$spawn" here is the socket resource responsible for communicating with the client socket.

$spawn = socket_accept ($socket) or die ("could not accept incoming connection\n");

So far, we've got the socket for the service, but in fact the script doesn't do anything. So in order to continue with the above goal, we will read the client socket message and then reverse the received message and send it back to the client socket.

Step 6th: Read the message from the client socket

$input = Socket_read ($spawn, 1024) or die ("could not read input\n");

Step 7th: Reverse Message

$output = Strrev ($input). "\ n";

8th step: Send a message to the client socket

Socket_write ($spawn, $output, strlen ($output)) or die ("could not write output\n");

Close socket

Socket_close ($spawn);
Socket_close ($socket);

This completes the service side. Now, let's learn how to create a PHP client.

PHP Client

The first two steps are the same as the service side.

1th Step: Set variables, such as host and port

$host = "127.0.0.1";
$port = 5353;
No Timeout
Set_time_limit (0);

Note: The ports and hosts here should be the same as defined in the server.

Step 2nd: Create a socket

$socket = Socket_create (af_inet, sock_stream, 0) or die ("could not create socket\n");

3rd step: Connect to the service side

$result = Socket_connect ($socket, $host, $port) or die ("Could not connect toserver\n");

At this point, unlike the server, the client socket does not bind ports and hosts. Instead, it connects to the service socket and waits for the connection to be accepted from the client socket. This step establishes a connection between the client socket and the server socket.

4th step: Write to the service port socket

Socket_write ($socket, $message, strlen ($message)) or die ("could not send data to server\n");

In this step, the data for the client socket is sent to the server socket.

Step 5th: Read the response from the server side

$result = Socket_read ($socket, 1024) or die ("could not read server response\n");
echo "Reply from Server:". $result;

Step 6th: Close the socket

Socket_close ($socket);

The complete code

Service side (server.php)

Set some variables
$host = "127.0.0.1";
$port = 25003;
Don ' t timeout!
Set_time_limit (0);
Create socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("could not create socket\n");
Bind socket to Port
$result = Socket_bind ($socket, $host, $port) or die ("could not bind to socket\n");
Start listening for connections
$result = Socket_listen ($socket, 3) or die ("could not set up socket listener\n");

Accept Incoming Connections
Spawn another socket to handle communication
$spawn = socket_accept ($socket) or die ("could not accept incoming connection\n");
Read Client input
$input = Socket_read ($spawn, 1024) or die ("could not read input\n");
Clean up input string
$input = Trim ($input);
echo "Client message:". $input;
Reverse client input and send back
$output = Strrev ($input). "\ n";
Socket_write ($spawn, $output, strlen ($output)) or die ("could not write output\n");
Close sockets
Socket_close ($spawn);
Socket_close ($socket);

Client (client.php)

$host = "127.0.0.1";
$port = 25003;
$message = "Hello Server";
echo "Message to Server:". $message;
Create socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("could not create socket\n");
Connect to Server
$result = Socket_connect ($socket, $host, $port) or die ("could not connect to server\n");
Send string to Server
Socket_write ($socket, $message, strlen ($message)) or die ("could not send data to server\n");
Get server response
$result = Socket_read ($socket, 1024) or die ("could not read server response\n");
echo "Reply from Server:". $result;
Close socket
Socket_close ($socket);

After you create the above files (server.php and client.php), do the following:

    1. Copy these files from the WWW directory (assuming wamp) and place them in C:\wamp.

    2. Open your Web browser and type localhost in the address bar.

    3. First browse server.php and then client.php.

License

This article, along with any related source code and files, is licensed by the Code Project Open License (Cpol).

"Responsible Editor: Wangxueyan TEL: (010) 68476606"


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.