PHP Socket programming process details

Source: Internet
Author: User
Tags php server

PHP Socket programming process details

Introduction

Socket is used for inter-process communication. Inter-process communication is generally based on the client-server model. In this case, the client-server is an application that can interact with each other. The interaction between the client and the server needs to be connected. Socket programming is responsible for establishing interactive connections between applications.

In this article, we will learn how to use PHP to create a simple client-server. We will also learn how client applications send messages to the server and how to receive messages from the server.

Use Code

Objective: To develop a client that sends a string message to the server. The server reverts the same information and returns it to the client.

PHP server

Step 2: 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 and 65535.

Step 2: Create a socket

$ Socket = socket_create (AF_INET, SOCK_STREAM, 0) or die ("cocould not create socket \ n ");

Step 2: bind the socket to the port and host

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

$ Result = socket_bind ($ socket, $ host, $ port) or die ("cocould not bind to socket \ n ");

Step 2: Start socket listening

After being bound to an IP address and port, the server waits for the connection from the client. Wait until the connection is established.

$ Result = socket_listen ($ socket, 3) or die ("cocould not set up socket listener \ n ");

Step 2: accept connections

This function will accept the connection request passed in by the socket. After accepting the connection from the client socket, this function returns another socket resource, which is actually responsible for communicating with the corresponding client socket. $ Spawn is the socket resource responsible for communicating with the client socket.

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

So far, we have prepared the server socket, but in fact this script has not done anything. Therefore, in order to continue to achieve 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 2: read messages from client socket

$ Input = socket_read ($ spawn, 1024) or die ("cocould not read input \ n ");

Step 2: reverse messages

$ Output = strrev ($ input). "\ n ";

Step 2: send a message to the client socket

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

Disable socket

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

This completes the server. Now, we learn how to create a PHP client.

PHP Client

The first two steps are the same as those on the server.

Step 2: 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 the definitions in the server.

Step 2: Create a socket

$ Socket = socket_create (AF_INET, SOCK_STREAM, 0) or die ("cocould not create socket \ n ");

Step 2: connect to the server

$ Result = socket_connect ($ socket, $ host, $ port) or die ("cocould not connect toserver \ n ");

In this case, the client socket is not bound to the port and host. Instead, it connects to the server socket and waits to accept the connection from the client socket. This step establishes a connection from the client socket to the server socket.

Step 2: write data to the server socket

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

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

Step 2: read responses from the server

$ Result = socket_read ($ socket, 1024) or die ("cocould not read server response \ n ");
Echo "Reply From Server:". $ result;

Step 2: Disable socket

Socket_close ($ socket );

Complete code

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 ("cocould not create socket \ n ");
// Bind socket to port
$ Result = socket_bind ($ socket, $ host, $ port) or die ("cocould not bind to socket \ n ");
// Start listening for connections
$ Result = socket_listen ($ socket, 3) or die ("cocould not set up socket listener \ n ");

// Accept incoming connections
// Spawn another socket to handle communication
$ Spawn = socket_accept ($ socket) or die ("cocould not accept incoming connection \ n ");
// Read client input
$ Input = socket_read ($ spawn, 1024) or die ("cocould 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 ("cocould not write output \ n ");
// Close sockets
Socket_close ($ spawn );
Socket_close ($ socket );

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 ("cocould not create socket \ n ");
// Connect to server
$ Result = socket_connect ($ socket, $ host, $ port) or die ("cocould not connect to server \ n ");
// Send string to server
Socket_write ($ socket, $ message, strlen ($ message) or die ("cocould not send data to server \ n ");
// Get server response
$ Result = socket_read ($ socket, 1024) or die ("cocould not read server response \ n ");
Echo "Reply From Server:". $ result;
// Close socket
Socket_close ($ socket );

After setting up the above files server. php and client. php), perform the following operations:

License

This article and any related source Code and files are licensed by The Code Project Open License (CPOL.

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.