还没有写完,明天继续

Socket函数

socket_accept

resource socket_accept ( resource $socket )

socket_accept — Accepts a connection on a socket

接受一个socket的连接

After the socket socket has been created using socket_create(), bound to a name with socket_bind(), and told to listen for connections with socket_listen(), this function will accept incoming connections on that socket. Once a successful connection is made, a new socket resource is returned, which may be used for communication. If there are multiple connections queued on the socket, the first will be used. If there are no pending connections, socket_accept() will block until a connection becomes present. If socket has been made non-blocking using socket_set_blocking() or socket_set_nonblock(), FALSE will be returned.

The socket resource returned by socket_accept() may not be used to accept new connections. The original listening socket socket, however, remains open and may be reused.

当一个Socket对象被socket_create()创建出来,并使用socket_bound()绑定一个名字,交由socket_listen()进行监听后,该函数会接受来自该socket进来的连接。

一旦成功连接,就会返回一个新的Socket对象来支持该连接。

如果在当前Socket上存在多个连接,则上述功能仅对第一个连接生效。

如果当前Socket上没有连接,则该函数会阻塞,直到一个连接出现。

如果该Socket已经通过socket_set_block()或者socket_set_nonblock()函数设置为不阻塞状态,则返回false。

该函数返回的socket对象可能不会拿来接受新的链接。而作为参数传递进来的Socket对象则继续保持开放状态,接受新的连接。

参数

  • socket

A valid socket resource created with socket_create().

socket_create()函数创建的有效socket实例。

返回值

Returns a new socket resource on success, or FALSE on error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error.

当该函数执行成功后,返回一个新的socket实例,执行失败后返回false。

可以通过socket_last_error()获取上次的错误信息。

可以通过socket_strerror()得到该错误信息的详细描述。

socket_bind()

bool socket_bind ( resource $socket , string $address [, int $port = 0 ] )

socket_bind — 给套接字绑定名字

socket_clear_error()

void socket_clear_error ([ resource $socket ] )

清除套接字或者最后的错误代码上的错误

socket_close()

void socket_close ( resource $socket )

关闭套接字资源

socket_cmsg_space

int socket_cmsg_space ( int $level , int $type )

socket_cmsg_space — Calculate message buffer size

统计数据所需的缓存大小。

Calculates the size of the buffer that should be allocated for receiving the ancillary data.

计算为接收辅助数据而分配的缓冲区的大小。

参数的解释信息等,文档上没有介绍。

socket_connect()

bool socket_connect ( resource $socket , string $address [, int $port = 0 ] )

socket_connect — 开启一个套接字连接

socket_create_listen()

resource socket_create_listen ( int $port [, int $backlog = 128 ] )

socket_create_listen — Opens a socket on port to accept connections

打开一个socket来接受连接。

socket_create_listen() creates a new socket resource of type AF_INET listening on all local interfaces on the given port waiting for new connections.

This function is meant to ease the task of creating a new socket which only listens to accept new connections.

socket_create_listen()创建一个新的AF_INET类型的socket对象,接收所有来自该端口的本地接口的连接。

这个函数是为了减轻创建一个新套接字的任务,这个套接字只监听接受新的连接。

参数

  • port

The port on which to listen on all interfaces.

服务器开设的端口。

  • backlog

The backlog parameter defines the maximum length the queue of pending connections may grow to. SOMAXCONN may be passed as backlog parameter, see socket_listen() for more information.

该backlog参数定义了该socket可挂起的连接队列可能增长到的最大长度。

返回值

socket_create_listen() returns a new socket resource on success or FALSE on error. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error.

当该函数执行成功后,返回一个新的socket实例,执行失败后返回false。

可以通过socket_last_error()获取上次的错误信息。

可以通过socket_strerror()得到该错误信息的详细描述。

注释

Note:

If you want to create a socket which only listens on a certain interface you need to use socket_create(), socket_bind() and socket_listen().

如果你想创建只监听来自特定接口的socket,你需要使用socket_create(),socket_bind()和socket_listen()

socket_create_pair()

socket_create_pair — Creates a pair of indistinguishable sockets and stores them in an array

创建一对相同的socket对象并且将他们保存在数组中。

bool socket_create_pair ( int $domain , int $type , int $protocol , array &$fd )

socket_create_pair() creates two connected and indistinguishable sockets, and stores them in fd. This function is commonly used in IPC (InterProcess Communication).

socket_create_pair()函数会创建两个相互连接的相似socket,将他们存储在$fd数组中。

该函数经常被用在IPC(进程间通信)上。

参数

  • domain

The domain parameter specifies the protocol family to be used by the socket. See socket_create() for the full list.

该参数指定了该socket采用的协议族,可以通过socket_create()函数中的信息查看全部协议族。

  • type

The type parameter selects the type of communication to be used by the socket. See socket_create() for the full list.

该参数指定了该socket采用的通信方式,可以通过socket_create()函数中的信息查看全部通信方式。

  • protocol

The protocol parameter sets the specific protocol within the specified domain to be used when communicating on the returned socket. The proper value can be retrieved by name by using getprotobyname(). If the desired protocol is TCP, or UDP the corresponding constants SOL_TCP, and SOL_UDP can also be used.

该protocol参数指定了在设置了domain的情况下,与通信另一方交流时采用的协议。

内置的值与协议名称之间可以通过使用getprotobyname()函数进行转换。

如果需要使用TCP或者UDP协议的话,可以使用相应的常数SQL_TCP以及SQL_UDP进行设置。

  • fd

保存最后产生的socket的对象。

返回值

true/false

socket_create()

socket_create — 创建一个套接字(通讯节点)

resource socket_create ( int $domain , int $type , int $protocol )

socket_get_option()/socket_getopt()

socket_get_option — Gets socket options for the socket

得到针对该socket的操作。

mixed socket_get_option ( resource $socket , int $level , int $optname )

The socket_get_option() function retrieves the value for the option specified by the optname parameter for the specified socket.

socket_get_option()函数会根据optname参数检索为该socket执行该操作所需的具体的参数。

参数

  • socket

A valid socket resource created with socket_create() or socket_accept().

socket_create()或者socket_accept()创建的有效socket函数

  • level

The level parameter specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function.

暂时不会翻译

socket_getpeername()

socket_getpeername — Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type

查询另一端的socket,该socket可能是是host/port形式的主机,或者是一个Unix的文件路径格式,取决于具体的类型。

bool socket_getpeername ( resource $socket , string &$address [, int &$port ] )

Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type.

查询另一端的socket,该socket可能是是host/port形式的主机,或者是一个Unix的文件路径格式,取决于具体的类型。

参数

  • socket

A valid socket resource created with socket_create() or socket_accept().

  • address

If the given socket is of type AF_INET or AF_INET6, socket_getpeername() will return the peers (remote) IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port.

If the given socket is of type AF_UNIX, socket_getpeername() will return the Unix filesystem path (e.g. /var/run/daemon.sock) in the address parameter.

如果该socket的类型是AF_INET或者AF_INET6,该函数会将远程的IP地址以相同的格式返回回来,例如(127.0.0.1或者fe80:1),并将结果保存在$address参数中。如果设置有port参数,则将端口号保存在$port参数中。

返回值

运行错误或者当socket的类型不是AF_INET,AF_INET6或者AF_UNIX时,也会返回错误。

socket_getsocketname()

socket_getsockname — Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type

与上面的socket_getpeername()对应,该函数是获取本地的socket信息。

bool socket_getsockname ( resource $socket , string &$addr [, int &$port ] )

Note: socket_getsockname() should not be used with AF_UNIX sockets created with socket_connect(). Only sockets created with socket_accept() or a primary server socket following a call to socket_bind() will return meaningful values.

注意:socket_getsockname()函数不能被用于属于AF_UNIX协议族,且是socket_connect()函数创建的socket。

只有socket_accept()创建的socket或者一个原先的socket被socket_bind()绑定才能返回有效值。

参数

  • socket

A valid socket resource created with socket_create() or socket_accept().

  • addr

If the given socket is of type AF_INET or AF_INET6, socket_getsockname() will return the local IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port.

If the given socket is of type AF_UNIX, socket_getsockname() will return the Unix filesystem path (e.g. /var/run/daemon.sock) in the address parameter.

  • port

If provided, this will hold the associated port.

socket_import_stream()

socket_import_stream — Import a stream

引入一个stream。

resource socket_import_stream ( resource $stream )

imports a stream that encapsulates a socket into a socket extension resource.

导入一个stram,将一个socket封装进另一个socket的扩展源中。

参数

  • stream

The stream resource to import.

返回值

true/false.

socket_last_error()

socket_last_error — Returns the last error on the socket

返回该socket的上一次错误的错误信息码。

int socket_last_error ([ resource $socket ] )

If a socket resource is passed to this function, the last error which occurred on this particular socket is returned. If the socket resource is omitted, the error code of the last failed socket function is returned. The latter is particularly helpful for functions like socket_create() which don’t return a socket on failure and socket_select() which can fail for reasons not directly tied to a particular socket. The error code is suitable to be fed to socket_strerror() which returns a string describing the given error code.

If no error had occurred, or the error had been cleared with socket_clear_error(), the function returns 0.

对于传入该函数的socket,该函数会返回该socket的上一次错误码。

如果不传入socket作为参数,会返回上一次执行失败的socket相关函数的信息。

这种不传入socket的方式对于像socket_create()、socket_select()类的函数检测错误原因很有帮助。

该错误信息码可以被传递进socket_strerror()函数中,来得到关于该错误信息的文字描述。

参数

  • socket

A valid socket resource created with socket_create().

返回值

This function returns a socket error code.

错误信息码。

socket_listen()

socket_listen — Listens for a connection on a socket

监听一个socket上的连接。

bool socket_listen ( resource $socket [, int $backlog = 0 ] )

After the socket socket has been created using socket_create() and bound to a name with socket_bind(), it may be told to listen for incoming connections on socket.

socket_listen() is applicable only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.

在socket对象被socket_create()函数创建出来,并且使用socket_bind()函数绑定了一个名字后,它可能需要使用该函数来监听一个来自该socket的连接。

该函数只对socket类型为SOCK_STREAM或者SOCK_SEQPACKET有效。

参数

  • sock

A valid socket resource created with socket_create().

  • backlog

A maximum of backlog incoming connections will be queued for processing. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed.

超过该参数设置的连接会被安排到队列中进行等待。

当客户端尝试连接,并且该socket仍有许多连接未处理时,可能会得到一个ECONNREFUSED的错误信息提示,或者两者采用的协议支持重传,则下次连接则可能成功。

返回值

true/false

socket_read()

socket_read — Reads a maximum of length bytes from a socket

从一个socket中读取最大长度的字节内容。

string socket_read ( resource $socket , int $length [, int $type = PHP_BINARY_READ ] )

The function socket_read() reads from the socket resource socket created by the socket_create() or socket_accept() functions.

参数

  • socket

A valid socket resource created with socket_create() or socket_accept().

  • length

The maximum number of bytes read is specified by the length parameter. Otherwise you can use \r, \n, or \0 to end reading (depending on the type parameter, see below).

读取的最大长度,否则你可以结合下面的type参数设置读取的结束值。

  • type

Optional type parameter is a named constant:

  • PHP_BINARY_READ (Default) - use the system recv() function. Safe for reading binary data.
  • PHP_NORMAL_READ - reading stops at \n or \r.
  • PHP_BINARY_READ (默认设置)
  • PHP_NORMAL_READ - 结束标志:\n 或者 \r

返回值

socket_read() returns the data as a string on success, or FALSE on error (including if the remote host has closed the connection). The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual representation of the error.

成功后将结果作为字符串返回,失败后则返回FALSE,如果远程主机关闭连接,则返回FALSE。

Note:
socket_read() returns a zero length string (“”) when there is no more data to read.

当无值时,则返回空字符串。

socket_recv()

socket_recv — 从已连接的socket接收数据

int socket_recv ( resource $socket , string &$buf , int $len , int $flags )

socket_recvfrom()

socket_recvfrom — Receives data from a socket whether or not it is connection-oriented

从一个socket中读取数据,无论该socket的连接状态是否维持。

int socket_recvfrom ( resource $socket , string &$buf , int $len , int $flags , string &$name [, int &$port ] )

The socket_recvfrom() function receives len bytes of data in buf from name on port port (if the socket is not of type AF_UNIX) using socket. socket_recvfrom() can be used to gather data from both connected and unconnected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.

The name and port must be passed by reference. If the socket is not connection-oriented, name will be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented, name is NULL. Additionally, the port will contain the port of the remote host in the case of an unconnected AF_INET or AF_INET6 socket.

该函数可以将指定长度的数据,从执行name和port的socket中读取出来,保存在buf中。

该函数可以从已连接或者连接失效的地方读取连接。

另外,可以设置flag来修改该函数功能。

name和port属性可以被设置为引用。

如果当前socket连接失败,这两个参数会作为host和port参数进行连接。

如果连接有效,则name参数没有什么用。

另外port参数会一直被有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值