What is Socket?
Socket is an intermediate software abstraction layer that communicates between the application layer and the TCP/IP protocol suite. It is a set 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 is all, allowing Socket to organize data to meet the specified requirements. protocol. It has been rare to see many people using PHP’s socket module to do some things. Probably everyone places it within the scope of scripting languages, but in fact, PHP’s socket module can do many things, including doing ftplist , http post submission, smtp submission, group package and interaction of special messages (such as smpp protocol), whois query. These are some of the more common queries. Especially what PHP's socket extension library can do is not much worse than C.
Socket Basics
php uses Berkley's socket library to create its connections. You can know that socket is just a data structure. Use this socket data structure to start a session between the client and the server. This server is always listening and preparing to generate a new session. When a client connects to the server, it opens a port on which the server is listening for a session. At this time, the server accepts the client's connection request and then performs a cycle. Now the client can send information to the server, and the server can send information to the client.
To generate a Socket, three variables are required: A protocol, a socket type and a public protocol type. There are three protocols to choose from when generating a socket. Continue reading below to get detailed protocol content. Defining a public protocol type is an essential element of connection. Let's take a look at these common protocol types.
1. Protocol
Description | |
This is the protocol most used to generate sockets, using TCP or UDP for transmission, and used in IPv4 addresses | |
Similar to the above, but used for IPv6 addresses | |
Local protocol, used on Unix and Linux systems, it Rarely used, usually used when the client and server are on the same machine |
Description | |
This protocol is a sequential, reliable, data-integrated byte stream-based connection. This is the most commonly used socket type. This socket uses TCP for transmission. | |
This protocol is a connectionless, fixed-length transfer call. This protocol is unreliable and uses UDP for its connections. | |
This protocol is a two-line, reliable connection that sends fixed-length data packets for transmission. This packet must be accepted completely before it can be read. | |
This socket type provides single network access. This socket type uses the ICMP public protocol. (ping and traceroute use this protocol) | |
This type is rarely used and is not implemented on most operating systems. It is provided for data Used by the link layer, the order of data packets is not guaranteed |
Description | |
Internet Control Message Protocol, mainly used on gateways and hosts, used To check network conditions and report error messages | |
User Datagram Protocol, it is a connectionless, unreliable transmission protocol | |
Transmission Control Protocol, this is the most commonly used reliable public protocol. It can ensure that the data packet can reach the recipient. If an error occurs during the transmission, then it will Resend the error packet. |
The above is the detailed content of What is socket in php? Detailed explanation of socket examples. For more information, please follow other related articles on the PHP Chinese website!