Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of socket for php

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the socket usage of php", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's make up a small series to take you to learn "what is the socket usage of php"!

About socket programming involved in the network protocol, what TCP ah, UDP ah, what socket three-way handshake, etc., these network protocols have a very detailed explanation on the Internet, not here, just cut a socket to establish the socket process diagram Let you see:

How does socket connect? As mentioned above, the process of establishing a connection is essentially the same as the connection between mysql client and server. And it is different from mysql is that mysql server and client have been edited for us, we just need to apply it. However, the critical moment has come, socket has nothing to offer us, the only thing to offer us is: dozens of socket functions.

This implies that socket programming is to create our own server and client, that is to say,``socket programming ``--is to build our own application similar to mysql server and client.

Before you get lost in socket programming, let me show you a few key socket functions and explain what they do. Otherwise, if you have no foundation for socket programming, I am afraid that after you read it, you will decisively skip this article and have a phobia of socket from now on.

Key socket function 1:

socket_create($net parameter 1,$stream parameter 2,$protocol parameter 3)

Role: Create a socket socket, which is a network data stream.

Return value: a socket, or false, parameter error E_WARNING warning

PHP's online manual makes it even clearer:

socket_create creates and returns a socket, also known as a communication node. A typical network connection consists of two sockets, one running on the client side and one on the server side.

Parameter 1 is: network protocol,

What are network protocols? Its options are the following three:

AF_INET: IPv4 network protocol. Both TCP and UDP can use this protocol. It's usually this, you know.

AF_INET6: IPv6 network protocol. Both TCP and UDP can use this protocol.

AF_UNIX: Local communication protocol. IPC (Interprocess Communication) with high performance and low cost.

Parameter 2: Socket stream, options are:

SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET SOCK_RAW SOCK_RDM。

Only the first two are explained here:

SOCK_STREAM TCP protocol socket.

SOCK_DGRAM UDP protocol socket.

For more information please click here: php.net/manual/zh/function.socket-create.php

Parameter 3: protocol, options include:

SOL_TCP: TCP protocol.

SOL_UDP: UDP protocol.

As you can see here, the second and third arguments of the socket_create function are actually related.

For example, suppose your first parameter applies IPv4 protocol: AF_INET, and then your second parameter applies TCP socket: SOCK_STREAM,

Then the third parameter must be SOL_TCP, which should not be difficult to understand.

TCP socket, of course, can only use TCP protocol, right? If you use UDP socket, then the third parameter how to choose I will not say, ha ha, you know.

Key Function 2:

socket_connect($socket parameter 1,$ip parameter 2,$port parameter 3)

Function: Connect a socket, return true or false

Parameter 1: socket_create function return value

Parameter 2: IP address

Parameter 3: Port number

Key Function 3:

socket_bind($socket parameter 1,$ip parameter 2,$port parameter 3)

Function: bind a socket, return true or false

Parameter 1: socket_create function return value

Parameter 2: IP address

Parameter 3: Port number

Key Function 4:

socket_listen($socket parameter1,$backlog parameter2)

Listens on a socket and returns true or false.

Parameter 1: socket_create function return value

Parameter 2: Maximum number of listening sockets

Key Function 5:

socket_accept($socket)

Function: Receives socket resource information, returns socket resource information successfully, fails as false

Parameter: socket_create function return value

Key Function 6:

socket_read($socket parameter 1,$length parameter 2)

Role: Read socket resource information,

Return value: socket resource successfully converted to string information, failure is false

Parameter 1: socket_create or socket_accept function return value

Parameter 2: Length of string read

Key Function 7:

socket_write($socket1,$msg2,$strlen3)

Role: Write data to socket

Return value: successfully returns the byte length of the string, failure is false

Parameter 1: socket_create or socket_accept function return value

Parameter 2: String

Parameter 3: Length of string

Key Function 8:

socket_close($socket)

Function: Close socket

Return value: true for success, false for failure

Parameter: socket_create or socket_accept function return value

These eight functions are the core functions of socket. Here are two more important functions.

socket_last_error($socket), Parameter is the return value of socket_create, Function is to get the last error code number of the socket, Return value socket code

socket_streror ($code), parameter is the return value of socket_last_error function, get the string information of code, the return value is the error information of socket

These two functions are still very important in socket programming. When writing socket programming, I think you still have to use them, especially novices. They can be used as debugging.

Here's the code, pay attention, please read my comments carefully, comments are very important,

Server script, D:\vhost\test\socket\server_socket.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report