PHP Socket network programming Getting Started Guide

Source: Internet
Author: User
This article mainly introduces the PHP Socket network programming Getting Started Guide, which is the basic knowledge of PHP getting started. If you need it, refer

This article mainly introduces the PHP Socket network programming Getting Started Guide, which is the basic knowledge of PHP getting started. If you need it, refer

What are TCP/IP and UDP?

TCP/IP (Transmission Control Protocol/Internet Protocol) is an industrial standard Protocol set designed for WANs.
User Data Protocol (UDP) is the Protocol corresponding to TCP. It belongs to the TCP/IP protocol family.
The following figure shows the relationship between these protocols.

TCP/IP protocol families include transport layer, network layer, and link layer. Now you know the relationship between TCP/IP and UDP.
Where is the Socket?
In the middle, we didn't see the Socket shadow, so where is it? You can still use graphs to speak clearly.

The original Socket is here.
What is Socket?
Socket is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. for users, a set of simple interfaces are all, let the Socket organize the data to conform to the specified protocol.
Will you use them?
Our predecessors have already done a lot for us, and the communication between networks is much simpler, but after all there is still a lot of work to do. I have heard about Socket programming before and think it is a relatively advanced programming knowledge. But as long as I understand the working principle of Socket programming, the mysterious veil will be uncovered.
A scenario in life. You need to call a friend to make a dial-up call. When a friend hears the ringtone and calls the phone, you and your friend can establish a connection to speak. After the communication is over, stop the call. The scenario in life explains this working principle. Maybe the TCP/IP protocol family is born in life, which is not necessarily true.

PHP Socket programming Overview
Php5.3 comes with the socket module, which enables php to have socket communication capabilities. For specific APIs, refer to the official manual. The specific implementation is very similar to that of c, only the underlying operations such as memory allocation and network byte sequence conversion are missing

In addition, the pcntl module and posix module of php can work together to implement basic process management, signal processing, and other operating system-level functions. There are two key functions, pcntl_fork () and posix_setsid (). Fork () is a process that creates a copy of the running process. The copy is considered as a sub-process, and the original process is considered as a parent process. After fork () runs, it can be detached from the process that starts it and the terminal control. This also means that the parent process can exit freely. Pcntl_fork () return value.-1 indicates execution failed, 0 indicates sub-process, and 0 indicates 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 for daemon, which means that the process will not be forcibly exited as the terminal is closed. This is a critical step for a resident process that will not be interrupted. This step is not necessary for the last fork (), but is usually done in this way. Its biggest significance is to prevent the acquisition of control terminals.

What is a daemon? A daemon is generally considered as a background task that does not control terminals. It has three distinct features:

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

Server socket listening code

<? Php // accept the client request and reply to the fixed response Content function server_listen_socket ($ address, $ port) {$ buffer = "Msg from wangzhengyi server, so kubi... "; $ len = strlen ($ buffer); // create, bind and listen to socket $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); if (! $ Socket) {echo "failed to create socket :". socket_strerror ($ socket ). "\ n"; exit () ;}$ bind_flag = socket_bind ($ socket, $ address, $ port); if (! $ Bind_flag) {echo "failed to bind socket :". socket_strerror ($ bind_flag ). "\ n"; exit () ;}$ backlog = 20; $ listen_flag = socket_listen ($ socket, $ backlog); if (! $ Listen_flag) {echo "failed to listen to socket :". socket_strerror ($ listen_flag ). "\ n"; exit ();} echo "waiting for clients to connect \ n"; while (1) {if ($ accept_socket = socket_accept ($ socket )) = FALSE) {continue;} else {socket_write ($ accept_socket, $ buffer, $ len); socket_close ($ accept_socket) ;}} function run_server () {$ pid1 = pcntl_fork (); if ($ pid1 = 0) {// first child process // the general process of the daemon process: fork ()-> setsid () -> fork () posix_setsid (); if ($ pid2 = pcntl_fork () = 0) {$ address = "192.168.1.71"; $ port = "8767 "; server_listen_socket ($ address, $ port);} else {// prevent obtaining control terminal exit ();}} else {// wait for first child process exitpcntl_wait ($ status) ;}// server daemon run_server ();

Running Effect
Start the socket process on the server to check whether it is running in the background.

Client Access, which can be accessed through a browser or curl. curl is used for access.

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.