socket_create_listen()

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Opens a socket on port to accept connections

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

socket_create_listen() creates a new socket resource of type AF_INET listening onalllocal 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.

参数$portThe port on which to listen on all interfaces.$backlogThe$backlogparameter defines the maximum length the queue of pending connections may grow to.SOMAXCONN may be passed as$backlogparameter, see socket_listen() for more information.

返回值

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.

注释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().

参见If you specify no port number, or 0, a random free port will be chosen.

To use ports for ipc between client/server on the same machine you can use (minus error checking)

server.php:

$sock = socket_create_listen(0);

socket_getsockname($sock, $addr, $port);

print "Server Listening on$addr:$port\n";

$fp = fopen($port_file, 'w');

fwrite($fp, $port);

fclose($fp);

while($c = socket_accept($sock)) {

/* do something useful */

socket_getpeername($c, $raddr, $rport);

print "Received Connection from$raddr:$rport\n";

}

socket_close($sock);

?>

client.php:

$fp = fopen($port_file, 'r');

$port = fgets($fp, 1024);

fclose($fp);

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_connect($sock, '127.0.0.1', $port);

socket_close($sock);

?>Please note that port 1 to and with 1024 on linux and bsd system require root privileges. So it is recommended to choose a higher port for your own application.Remember that ports are only valid from 1 - 65535

[editor's note: typo fixed, thanks abryant at apple dot com]I believe that on some systems this may not bind to some or all public interfaces.

On my Windows system, I could not connect on the public interface using this, but could when I made the individual calls to create, bind, and listen.

Dustin Oprea

php socket_create_listen(),socket_create_listen()相关推荐

  1. [转]php socket编程通信

    FROM : http://blog.csdn.net/baixiaoshi/article/details/9399083 今天终于测试成功了php中的socket通信,先看原理图 这里可以清晰的看 ...

  2. PHP查看PECL模块包含的函数

    为什么80%的码农都做不了架构师?>>>    http://php.net/manual/zh/function.get-extension-funcs.php 查看PECL扩展 ...

  3. PHP SOCKET编程详解

    这篇文章主要介绍了PHP SOCKET编程详解,需要的朋友可以参考下 1. 预备知识 一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实 ...

  4. 深入浅出讲解:php的socket通信

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1.         什么是TCP/IP.UDP? 2.         Soc ...

  5. php查询socket数据包头,php 查询数组值php中关于socket的系列函数总结

    本文列举了所有关于PHP语言中使用socket相关服务的一些函数.注意使用如下函数之前,你需要确保你的socket已打开,如果你没有打开,请编辑你的php.ini文件,去掉下面这行前面的注释(分号): ...

  6. php sorcket_PHP: Sockets - Manual

    socket_accept - Accepts a connection on a socket socket_addrinfo_bind - Create and bind to a socket ...

  7. webso员ket php,客戶端和PHP後端通信:Sokets,Stream,TCP/UDP?

    簡短版本:我想將客戶端連接到PHP服務器,但是我同時運行10個PHP腳本的服務器的限制.客戶端和PHP後端通信:Sokets,Stream,TCP/UDP? 的問題是:什麼是連接與PHP腳本客戶的最佳 ...

  8. php socket 基础知识

    ◆ Socket 基础 PHP使用Berkley的 socket库来创建它的连接.socket只不过是一个数据结构.你使用这个socket数据结构去开始一个客户端和服务器之间的会话.这个服务器是一 直 ...

  9. php socket 多次发送,php模拟socket 多次发送数据的实现方法

    php模拟socket 多次发送数据的实现 方法 表四:Socket函数 函数名描述 socket_accept() 接受一个Socket连接 socket_bind() 把socket绑定在一个IP ...

最新文章

  1. 基于Matlab的声波信号处理,基于声卡和Matlab平台的语音信号增强处理系统
  2. rocketmq怎么保证消息一致性_从入门到入土(三)RocketMQ 怎么保证的消息不丢失?...
  3. yii required 字段去除*号
  4. python有趣代码-一个有意思的 Python 训练项目集
  5. java.sql.SQLException: ORA-00923: FROM keyword not
  6. CRM reference product的UI界面
  7. 一个项目有两个pom_Python Selenium设计模式之POM设计模式
  8. Python:使用ctypes库调用外部DLL
  9. [RubyOnRails]一些网址
  10. html怎样让页面居中显示,HTML怎么让页面居中
  11. C#第九天面向对象的学习
  12. 粘性定位 sticky
  13. 中点画椭圆算法_中点圆算法
  14. 运维工程师必备的认证【红帽liunx-RHCE 8】
  15. 如何将pip更新到最新版本
  16. 360安全卫士“隐身僵尸木马”专杀工具,木马乎?
  17. shell脚本配置运行python程序,小技巧之 Linux 软连接的使用
  18. Flash 特效原理:震动效果
  19. 北上广深杭大厂+独角兽职位机会推荐
  20. IDEA debug下调试Evaluate

热门文章

  1. phpstorm编辑器乱码问题解决
  2. Django错误解决: CSRF verification failed. Request abor
  3. 子页面带到父页面提交
  4. 更改 SQL Server 2000 端口号
  5. Catlyst 6509告警信息--把trunk口配置成access
  6. JDK源码(21)-Unsafe
  7. http 几种请求方法的差别
  8. Git忽略项目中的指定的文件
  9. 思科集成管理控制器IMC爆出任意代码执行漏洞CVE-2017-6616 绿盟科技发布安全威胁通告...
  10. (43) 讨论和通知