Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of PHP Socket programming process

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the detailed introduction of PHP Socket programming process", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the detailed introduction of PHP Socket programming process"!

Introduction

Socket is used for interprocess communication. Inter-process communication is usually based on the client-server model. At this point, the client-server is an application that can interact with each other. The interaction between the client and the server requires a connection. Socket programming is responsible for establishing interactive connections between applications.

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

Use the code

Objective: to develop a client to send string messages to the server, which reverses the same information and returns it to the client.

PHP server

Step 1: 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 ("Could not create socket\ n")

Step 3: bind socket to port and host

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

$result = socket_bind ($socket, $host, $port) or die ("Could not bind to socket\ n")

Step 4: start socket snooping

After binding to the IP and port, the server starts waiting for the client to connect. It's been waiting until it's not connected.

$result = socket_listen ($socket, 3) or die ("Could not set up socket listener\ n")

Step 5: accept the connection

This function accepts incoming connection requests from the created 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. 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 have prepared the server-side socket, but in fact this script doesn't do anything. So in order to continue to accomplish the above goal, we will read the client socket message, then reverse the received message and send it back to the client socket.

Step 6: read the message from the client socket

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

Step 7: reverse the message

$output = strrev ($input). "\ n"

Step 8: 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 server. Now, let's learn how to create a PHP client.

PHP client

The first two steps are the same as the server.

Step 1: set variables such as "host" and "port"

$host = "127.0.0.1"

$port = 5353

/ / No Timeout

Set_time_limit (0)

Note: the port and host here should be the same as the definition on the server side.

Step 2: create a socket

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

Step 3: connect to the server

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

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

Step 4: write to the server socket

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

In this step, the client-side socket's data is sent to the server-side socket.

Step 5: read the response from the server

$result = socket_read ($socket, 1024) or die ("Could not read server response\ n")

Echo "Reply From Server:". $result

Step 6: close socket

Socket_close ($socket)

Complete code

Server 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 establishing the above files (server.php and client.php), do the following:

Copy these files in the www directory (assuming WAMP) and place them in C:\ wamp.

Open a Web browser and type localhost in the address bar.

Browse server.php first and then client.php.

At this point, I believe that you have a deeper understanding of the "detailed introduction of the PHP Socket programming process". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report