php socket通信的基础知识php socket create
转自:http://www.byz5.com/html/234/6/6521/1.htm
迷人而又让人容易懵懂的套接字(Sockets)。Sockets在PHP中是没有充实哄骗的功能。今天你将瞥见产生1个能使用客户端连接的办事器,并在 客户端使用socket进行连接,办事器端将具体的处置惩罚信息发送给客户端。
当你瞥见完备的socket过程,那么你将会在往后的程序研发中使用它。这个办事器是1个能让你连接的HTTP办事器,客户端是1个Web浏览器,这是一 个单一的 客户端/办事器 的关系。
Socket 基础
PHP使用Berkley的socket库来创建它的连接。你可以懂得socket只不过是1个数值布局。你使用这个socket数值布局去开始1个客户端和办事器之间的会话。这个办事器是一直在监听准备产生1个新的会话。当1个客户端连接办事器,它就打开办事器正在进行监听的1个端口进行会话。这时,办事器端接受客户端的连接请求,那么就进行一次循环。此刻这个客户端就可以兴许发送信息到办事器,办事器也能发送信息给客户端。
产生1个Socket,你需要3个变量:1个协议、1个socket类型和1个大众协议类型 。产生1个socket有三种协议供选择,接续看底下的内容来 获取具体的协议内容。
定义1个大众的协议类型是进行连接1个必不可少的元素。底下的表咱们看看有那些大众的协议类型。
表一:协议
名儿/常量描述
AF_INET这是大多数用来产生socket的协议,使用TCP或UDP来传输 ,用在IPv4的地址
AF_INET6与上面类似,不过是来用在IPv6的地址
AF_UNIX本地协议,使用在Unix和Linux 系 统上,它很少使用,一般都是当客户端和办事器在同一台及其上的时辰使用
表二:Socket类型
名儿/常量描述
SOCK_STREAM这个协议是按照顺序的、可靠的、数值完备的基于字节流的连接 。这是1个使用最多的socket类型,这个socket是使用 TCP 来进行传输。
SOCK_DGRAM这个协议是无连接的、固定长度的传输调用。该协议是不可靠的,使用UDP 来进行它的连接。
SOCK_SEQPACKET这个协议是双线路的、可靠的连接,发送固定长度的数值包进行传输。必须把这个包完备的接受才气进行读取。
SOCK_RAW这个socket类型提供单一的收集访问,这个socket类型使用ICMP大众协议。(ping、traceroute使用该协 议)
SOCK_RDM这个类型是很少使用的,在大部分的操作系统上没有实现,它是提供给数值链路层使用,不保证数值包的顺序
表三:大众协议
名儿/常量描述
ICMP互联网控制消息协议,首要使用在网关和主机上,用来检查收集状态和报告错误信息
UDP 用户数值报文协议,它是1个无连接,不可靠的传输协议
TCP 传输控制协议,这是1个使用最多的可靠的大众协议,它能保证数值包可以兴许到达接受者那儿,如果在传输过程中发生错误,那么它将重新发送出错数值包。
此刻你懂得了产生1个socket的3个元素,那么咱们就在php中使用socket_create()函数来产生1个socket。这个 socket_create()函数需要3个参数:1个协议、1个socket类型、1个大众协议。socket_create()函数运行成功归回1个 包含socket的资源类型,如果没有成功则归回false。
Resourece socket_create(int protocol, int socketType, int commonProtocol);
Socket函数
函数名描述
socket_accept()接受1个Socket连接
socket_bind()把socket绑定在1个IP地址和端口上
socket_clear_error()清除socket的错误或者最后的错误代码
socket_关上()关闭1个socket资源
socket_connect()开始1个socket连接
socket_create_listen()在指定端口打开1个socket监听
socket_create_pair()产生一对没有区另外socket到1个数组里
socket_create()产生1个socket,至关于产生1个socket的数值布局
socket_get_option()获取socket选项
socket_getpeername()获取长途类似主机的ip地址
socket_getsockname()获取本地socket的ip地址
socket_iovec_add()添加1个新的矢量到1个分离/聚合的数组
socket_iovec_alloc()这个函数创建1个可以兴许发送接收读写的iovec数值布局
socket_iovec_delete()删除1个已分配的iovec
socket_iovec_fetch()归回指定的iovec资源的数值
socket_iovec_free()释放1个iovec资源
socket_iovec_set()配备布置iovec的数值新值
socket_last_error()获取时下socket的最后错误代码
socket_listen()监听由指定socket的所有连接
socket_read()读取指定长度的数值
socket_readv()读取从分离/聚合数组过来的数值
socket_recv()从socket里结束数值到缓存
socket_recvfrom()接受数值从指定的socket,如果没有指定则默认时下socket
socket_recvmsg()从iovec里接受消息
socket_select()多路选择
socket_send()这个函数发送数值到已连接的socket
socket_sendmsg()发送消息到socket
socket_sendto()发送消息到指定地址的socket
socket_set_block()在socket里配备布置为块标准样式
socket_set_nonblock()socket里配备布置为非块标准样式
socket_set_option()配备布置socket选项
socket_shutdown()这个函数许可你关闭读、写、或者指定的socket
socket_strerror()归回指定错误号的具体错误
socket_write()写数值到socket缓存
socket_writev()写数值到分离/聚合数组
以上所有的函数都是PHP中关于socket的,使用这些函数,你必须把你的socket打开,如果你没有打开,请编辑你的php.ini文件,去失底下这行前面的注释:
extension=php_sockets.dll
如果你没有办法去失注释,那么请使用底下的代码来加载扩展库: if(!extension_loaded(‘sockets’)) { if(strtoupper(substr(PHP_OS, 3)) == “WIN”) { dl(‘php_sockets.dll’); } else { dl(‘sockets.so’); } }
*如果你不懂得你的socket是不是打开,那么你可以使用phpinfo()函数来确定socket是不是打开。你经由过程查看phpinfo信息了解 socket是不是打开。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
