socket_getsockname
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
socket_getsockname — 获取套接字本地端的名字,返回主机名和端口号或是 Unix 文件系统路径,具体取决于套接字类型
说明
注意: socket_getsockname() 不应该用于 socket_connect() 创建的
AF_UNIX
类型套接字。只有使用 socket_accept() 创建的套接字或调用过 socket_bind() 的服务端套接字会返回有意义的值。
参数
socket
-
由 socket_create() 或 socket_accept() 创建的 Socket 实例。
address
-
如果给定套接字的类型是
AF_INET
或AF_INET6
,socket_getsockname() 将在参数address
上返回本地 IP 地址 (例如:127.0.0.1
或fe80::1
),如果存在端口号,也将关联到port
参数。如果给定套接字的类型是
AF_UNIX
,socket_getsockname() 将在address
参数中返回 Unix 文件系统路径(例如:/var/run/daemon.sock
)。 port
-
如果提供此参数,它将保存关联的端口号。
返回值
成功时返回 true
, 或者在失败时返回 false
。 如果套接字类型不是 AF_INET
、AF_INET6
或 AF_UNIX
中的任意一个,socket_getsockname()
也可能返回 false
,在此情况下,套接字最后的错误码不会更新。
参见
- socket_getpeername() - 获取套接字远端名字
- socket_last_error() - 返回套接字上的最后一个错误
- socket_strerror() - 返回描述套接字错误的字符串
用户贡献的备注 1 note
Curiously, getsockname() works for socket_create() and socket_create_pair() Unix-domain (AF_UNIX) sockets if one calls socket_bind() after creation to name the formerly anonymous socket(s).
Using a socket_bind() call also results in a file system "file" (socket, first character 's' in an "ls -l" listing) being created with the given name. Such a "file" will need to be removed explicitly, as closing the socket will not remove it.
备份地址:http://www.lvesu.com/blog/php/function.socket-getsockname.php