Basic knowledge of PHP socket communication PHP Socket create

Source: Internet
Author: User
Tags get ip socket error
Transferred from: http://www.byz5.com/html/234/6/6521/1.htm

A fascinating and confusing socket (Sockets). Sockets is not a full-featured feature in PHP. Today you will have a glimpse of the 1 server that can use the client connection, and the client uses the socket to connect, and the server side sends the specific disposition penalty information to the client.

When you catch a glimpse of the complete socket process, you will use it later in the development of the program. This is a browser that allows you to connect to the HTTP office, the client is 1 Web browsers, which is a single client/manager relationship.

Socket Basics

PHP uses the Berkley socket library to create its connection. You can understand that a socket is just a 1 numeric layout. You use this socket value layout to start a session between the client and the manager. This manager is always listening ready to generate 1 new sessions. When 1 client connections are made, it is the session that opens the 1 ports on which the timer is listening. At this point, the server will accept the client's connection request, then a loop. At this point the client can send messages to the office, and the agent can send messages to the client.

To generate 1 sockets, you need 3 variables: a protocol, 1 socket types, and a VW protocol type. Generate 1 Sockets There are three kinds of protocols to choose from, continue to look at the bottom of the content to obtain specific protocol content.

Defining the 1-VW protocol type is an essential element for connecting 1. The table below let's look at the types of public agreements.

Table I: Protocol

Name/Constant Description

Af_inet This is most of the protocol used to generate sockets, using TCP or UDP for transmission, used in IPV4 address

Af_inet6 similar to the above, but to use the address of IPV6

Af_unix local protocols, which are used on UNIX and Linux systems, are rarely used, and are typically used when the client and the server are on the same station and on the same

Table II: Socket type

Name/Constant Description

Sock_stream This protocol is a sequential, reliable, and well-valued byte-stream-based connection. This is the 1 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 value packet for transmission. This package must be read in a fully-fledged acceptable talent.

Sock_raw This socket type provides a single collection of access, this socket type uses the ICMP VW 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 value of the link layer and does not guarantee the order of the value packets

Table III: Volkswagen Agreement

Name/Constant Description

ICMP Internet Control Message Protocol, primarily used on gateways and hosts, to check the collection status and report error messages

UDP user Value Message Protocol, which is 1 non-connected, unreliable transmission protocol

TCP Transmission Control Protocol, which is the 1 most reliable mass protocol used, guarantees that a value packet may reach the recipient, and if an error occurs during transmission, it will resend the error packet.

Now that you understand the 3 elements that produce 1 sockets, let's use the Socket_create () function in PHP to generate 1 sockets. This socket_create () function requires 3 parameters: one protocol, 1 socket type, and one VW protocol. The Socket_create () function successfully returned to the 1 resource type containing the socket, or false if unsuccessful.

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

Socket function

Function Name Description

Socket_accept () accepts 1 socket connections

Socket_bind () binds a socket to 1 IP addresses and ports

Socket_clear_error () Clears the socket error or the last error code

Socket_ shut Down () Close 1 socket resources

Socket_connect () Start 1 socket connections

Socket_create_listen () Open 1 socket listener on the specified port

Socket_create_pair () produces a pair without a region in addition to a socket into 1 arrays

Socket_create () generates 1 sockets to a numeric layout that produces 1 sockets

Socket_get_option () Get socket options

Socket_getpeername () Get IP addresses for long-distance similar hosts

Socket_getsockname () Gets the IP address of the local socket

Socket_iovec_add () Add 1 new vectors to 1 separate/aggregated arrays

Socket_iovec_alloc () This function creates 1 Iovec numeric layouts that may be sent to receive read and write

Socket_iovec_delete () Delete 1 assigned Iovec

Socket_iovec_fetch () Return the value of the specified Iovec resource

Socket_iovec_free () Frees 1 Iovec resources

Socket_iovec_set () is equipped with a new value for the assigned Iovec

Socket_last_error () Gets the last error code for the current socket

Socket_listen () listens for all connections by the specified socket

Socket_read () reads a value of the specified length

SOCKET_READV () reads the values from the Detach/aggregate array.

SOCKET_RECV () end value from socket to cache

Socket_recvfrom () accepts a numeric value from the specified socket if not specified by default when the socket

Socket_recvmsg () receive messages from Iovec

Socket_select () multi-channel selection

Socket_send () This function sends a value to the connected socket

SOCKET_SENDMSG () Send message to socket

Socket_sendto () sends a message to the socket at the specified address

Socket_set_block () is equipped with a block standard style in the socket.

Socket_set_nonblock () socket is equipped with non-block standard style

Socket_set_option () equipped with socket options

Socket_shutdown () This function permits you to close a read, write, or specified socket

Socket_strerror () return specific error for specified error number

Socket_write () write values to the socket cache

Socket_writev () write values to separate/aggregate arrays

All of the above functions are in PHP for the socket, using these functions, you must open your socket, if you do not open, please edit your php.ini file, to lose the previous line of comments:

Extension=php_sockets.dll

If you have no way to lose the comment, then use the code below to load the extension library: if (!extension_loaded (' sockets ')) {if (Strtoupper (substr (Php_os, 3)) = = "WIN") {DL (' Php_ Sockets.dll '); } else {dl (' sockets.so ');}}

* If you don't know if your socket is open, then you can use the Phpinfo () function to determine whether the socket is open or not. You check the Phpinfo information through the process to see if the socket is open.

  • 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.