• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php cocket的使用方法

php 搞代码 3年前 (2022-01-21) 17次浏览 已收录 0个评论

这篇文章主要介绍了关于php cocket的使用方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

window下,打开命令窗口,分别运行以下两行命令:

# php.exe e:/wwwroot/server.php# php.exe e:/wwwroot/server.php

以下代码来自php官方手册

server.php:

<?php //The Server error_reporting(E_ALL); $address = "127.0.0.1"; $port = "10000";   /* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */ $mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($mysock, $address, $port); socket_listen($mysock, 5); $client = socket_accept($mysock); echo "Server started, accepting connections...\n";   $i = 0; while (true == true) {     $i++;     echo "Sending $i to client.\n";     socket_write($client, $i, strlen($i));         $input = socket_read($client, 2048);     echo "Response from client is: $input\n";     sleep(5); } echo "Closing sockets..."; socket_close($client); socket_close($mysock);

client.php

<?php //The Client error_reporting(E_ALL); $address = "127.0.0.1"; $port = 10000; /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) {     echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else {     echo "socket successfully created.\n"; } echo "Attempting to connect to '$address' on port '$port'..."; $result = socket_connect($socket, $address, $port); if ($result === false) {     echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else {     echo "successfully connected to $address.\n"; } $i = 0; wh<strong>*本文来@源gao@daima#com搞(%代@#码@网2</strong><pre>搞代gaodaima码

ile (true == true) { $i++; echo "Sending $i to server.\n"; socket_write($socket, $i, strlen($i)); $input = socket_read($socket, 2048); echo "Response from server is: $input\n"; sleep(5); } echo "Closing socket…"; socket_close($socket);

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请随时关注我们网站!

相关推荐:

PHP源码php-beast加密

yii2 模板twig中使用GridView::widget

以上就是php cocket的使用方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php cocket的使用方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址