What is TCP/IP, UDP?
TCP/IP (Transmission Control protocol/internet Protocol), the Transmission Control Protocol/Internetwork Protocol, is an industrial standard protocol set that is designed for wide area networks (WANs).
UDP (User data Protocol, Subscriber Datagram Protocol) is a protocol corresponding to TCP. It is a part of the TCP/IP protocol family.
Here is a diagram that shows the relationship of these protocols.
The TCP/IP protocol family includes transport layer, network layer and link layer. Now you know the relationship between TCP/IP and UDP.
where is the socket?
in Figure 1, we don't see the socket shadow, so where is it? Or use the diagram to speak, at a glance.
The original socket is here.
What is a socket?
the socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, which is the complex TCP/IP protocol family hidden behind the socket interface, for users, a group of simple interface is all, let the socket to organize data to meet the specified protocol.
will you use them?
predecessors have done a lot of things to us, the network communication between a lot of simple, but after all, there are quite a lot of work to do. Previously heard socket programming, think it is more advanced programming knowledge, but as long as the understanding of the work of socket programming, the veil of mystery will be uncovered.
A scene in life. You have to call a friend, dial first, the friend heard the phone call after the phone, then you and your friend set up a connection, you can talk. And so on, hang up the phone and end this conversation. The life of the scene explains how this works, perhaps the TCP/IP protocol family is born in life, this is not necessarily.
Overview of PHP Socket programming
php5.3 with the socket module, so that PHP has socket communication capabilities, the specific API can refer to the official manual: Http://php.net/manual/zh/function.socket-create.php, The implementation is very similar to C, but with less memory allocation and network byte-order conversion, this low-level operation
At the same time, PHP's Pcntl module and POSIX module can achieve basic process management, signal processing, such as operating system-level functions. Here are two very critical functions, pcntl_fork () and Posix_setsid (). Fork () a process, it means that a copy of the running process is created, that the replica is considered a child process, and that the original process is considered a parent process. When fork () is run, it can be detached from the process and terminal control that started it, and it means that the parent process is free to exit. Pcntl_fork () Returns the value,-1 indicates execution failure, 0 means that in the child process, greater than 0 is represented in the parent process. Setsit (), which first makes the new process a "leader" of the session, and finally stops the process from controlling the terminal. This is also the most critical step in becoming a daemon, which means that the process will not be forced out as the terminal closes. This is a critical step for a resident process that will not be interrupted. For the last time fork (), this step is not necessary, but it is usually done, it is the most important to prevent access to the control terminal
What is a daemon? A daemon is often considered a background task that does not control the terminal. It has three distinct features:
- Run in the background
- Detached from the process of initiating him
- No terminal control
The most common implementation methods are: Fork ()-> setsid ()-> fork (), and the Run_server () method in the code implements the daemon.
Server-side Socket listening code
<?php//Accept client requests, reply to Fixed response content function Server_listen_socket ($address, $port) {$buffer = "Msg from W"
Angzhengyi 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 () {$pid 1 = pcntl_fork (); if ($pid 1 = 0) {//The general process of the process/daemon of the fork ()->setsid ()->fork () Posix_se
TSID ();
if ($pid 2 = pcntl_fork ()) = = 0) {$address = "192.168.1.71";
$port = "8767";
Server_listen_socket ($address, $port);
else {//Prevent access to control terminal exit ();
} else {//wait-for-a-child process exit pcntl_wait ($status);
}//Server daemon run_server ();
Run effect
start the server-side socket process to see if it runs in the background, as shown in the following example:
Client access can be accessed through a browser or curl, which is accessed directly with Curl