What is the socket in PHP? Socket instance Detailed

Source: Internet
Author: User
Tags php programming
What is a socket?

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol. It has been very rare to see how many people use the PHP socket module to do something, probably everyone put it in the context of scripting language, but in fact, PHP socket module can do a lot of things, including do ftplist,http post submission, SMTP commit, Group packages and make special message interactions (such as the SMPP protocol), WHOIS queries. These are the more common queries. in particular, the PHP socket extension library can do things that are not much worse than c.

Socket Basics

PHP uses the Berkley socket library to create its connection. You can tell that the socket is just a data structure. Use this socket data structure to start a session between a client and a server. This server is always listening ready to produce a new session. When a client connects to a server, it opens a session on a port that the server is listening on. At this point, the server accepts the client's connection request, then it loops. Now the client is able to send messages to the server, and the server can send messages to the client.
Generating a socket requires three variables: a protocol, a socket type, and a common protocol type . And there are three protocols for a socket to choose from, and continue to look at the content below to get a detailed agreement. Defining a common protocol type is an essential element for connecting. Let's take a look at these common protocol types.

1. Agreement

Name/constant Describe
Af_inet This is most of the protocol used to generate the socket, using TCP or UDP to transmit, using the address of the IPV4
Af_inet6 Similar to the above, but to use the address of the IPV6
Af_unix Local protocols, used on UNIX and Linux systems, are rarely used, and are typically used when the client and server are on the same platform and on

2. Socket type

Name/constant Describe
Sock_stream This protocol is a sequential, reliable, data-complete byte-stream-based connection. This is the most used socket type, which is transmitted using TCP.
Sock_dgram This protocol is a non-connected, fixed-length transfer call. The protocol is unreliable and uses UDP to connect to it.
Sock_seqpacket This protocol is a two-line, reliable connection that sends a fixed-length packet for transmission. This package must be fully accepted to be read.
Sock_raw This socket type provides a single network access, and this socket type uses the ICMP public protocol. (Ping, traceroute Use this Protocol)
Sock_rdm This type is rarely used and is not implemented on most operating systems, it is provided to the data link layer for use, and does not guarantee the order of packets

3. Public agreement

Name/constant Describe
Icmp Internet Control Message Protocol, primarily used on gateways and hosts, to check network conditions and report error messages
Udp User data message protocol, which is a non-connected, unreliable transport protocol
Tcp Transmission Control Protocol, which is a reliable public protocol with the most use, ensures that packets can reach the recipient, and if an error occurs during transmission, it will resend the error packet.

Knowing the three elements that produce a socket, we use the socket_create () function in PHP to generate a socket. This socket_create () function requires three parameters: one protocol, one socket type, and one public protocol. The Socket_create () function successfully returns a resource type that contains the socket and returns false if it does not succeed.

Resourece socket_create (int protocol, int sockettype, int commonprotocol);

PHP provides several functions to manipulate the socket, be able to bind a socket to an IP, listen to a socket for communication, accept a socket; Now let's look at an example of how a function generates, receives, and listens to a socket.

Its code is as follows:

<?php$commonprotocol = Getprotobyname ("tcp"), $socket = Socket_create (Af_inet, Sock_stream, $commonProtocol); Socket_bind ($socket, ' localhost ', 1337); Socket_listen ($socket);? >

Description: The above example produces a server side of your own.

In the first line of the example, a common protocol name is used to obtain a protocol type. Here is the TCP public protocol, if you want to use UDP or ICMP protocol, then you should change the Getprotobyname () function parameter to "UDP" or "ICMP". An alternative option is to specify SOL_TCP or SOL_UDP in the socket_create () function instead of using the Getprotobyname () function.
The second line of the example is to produce a socket and return an instance of the socket resource. After you have an instance of a socket resource, you must bind the socket to an IP address and a port.
The third line here binds the socket to the local computer (127.0.0.1) and binds the socket to your 1337 port. Then you need to listen for all incoming socket connections. After line fourth, you need to know all the socket functions and their use. The socket function is described in detail in a later section.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of tutorials

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.