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

Example Analysis of Socket Network programming in PHP

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

Share

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

This article mainly introduces the example analysis of Socket network programming in PHP, which is very detailed and has a certain reference value. Interested friends must read it!

What is TCP/IP, UDP?

TCP/IP (Transmission Control Protocol/Internet Protocol), the transmission control protocol / internetwork protocol, is an industry standard protocol set designed for the wide area network (WANs).

UDP (User Data Protocol, user Datagram Protocol) is the protocol corresponding to TCP. It belongs to one of the TCP/IP protocol families.

Here is a diagram that shows the relationship between these protocols.

The TCP/IP protocol family includes the transport layer, the network layer and the link layer. Now you know the relationship between TCP/IP and UDP.

Where is Socket?

In figure 1, we don't see the shadow of Socket, so where is it? Still use the picture to speak, it is clear at a glance.

So Socket is here.

What is Socket?

Socket is the middle software abstraction layer for the communication between the application layer and the TCP/IP protocol family, and it is a group of interfaces. In the design pattern, Socket is actually a facade pattern, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a simple set of interfaces is all, allowing Socket to organize the data to comply with the specified protocol.

Can you use them?

Our predecessors have done a lot for us, and the communication between networks is much easier, but after all, there is still a lot of work to be done. When I heard about Socket programming in the past, I thought it was a relatively advanced programming knowledge, but as long as I found out how Socket programming works, the mystery was lifted.

A scene in life. If you want to call a friend, dial first, and the friend will pick up the phone when he hears the ringtone, and then you and your friend will have a connection and can talk. When the exchange is over, hang up the phone and end the conversation. The scenes in life explain how this works. Maybe the TCP/IP protocol family was born in life, but not necessarily.

Overview of Socket programming in PHP

Php5.3 has its own socket module, which enables php to communicate with socket. For specific api, please refer to the official manual: http://php.net/manual/zh/function.socket-create.php. The implementation is very similar to c, except that the underlying operations such as memory allocation and network byte order conversion are missing.

At the same time, php's pcntl module and posix module work together to achieve basic process management, signal processing and other operating system-level functions. There are two very critical functions, pcntl_fork () and posix_setsid (). Fork () a process means that a copy of the running process is created, which is considered to be a child process, while the original process is considered to be the parent process. When fork () runs, it can break away from the process and terminal control that started it, which also means that the parent process is free to exit. Pcntl_fork () returns a value,-1 indicates execution failed, 0 indicates that it is in the child process, and greater than 0 indicates that it is in the parent process. Setsit (), which first makes the new process the "leader" of a new session, and finally makes the process no longer control the terminal. This is also the most critical step in becoming a daemon, which means that the process is not forced to exit as the terminal shuts down. This is a critical step for a resident process that will not be interrupted. To make the last fork (), this step is not necessary, but it is usually done, and its greatest significance is to prevent the acquisition of the control terminal.

What is a daemon? A daemon is usually thought of as a background task that does not control the terminal. It has three obvious features:

Run in the background

From the process that started him.

No terminal control is required

The most common implementation method is fork ()-> setsid ()-> fork (). The run_server () method in the code implements the daemon.

Server socket listening code

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