php接收tcp数据格式的简单介绍

php 如何解析通过tcp协议发过来的数据//创建socket监听端口
$socket = socket_create_listen("55555");
//连接失败给出错误信息
if(!$socket){
exit("Failed to create socket!\n");
}
while(true){
$client = socket_accept($socket); //接受一个Socket连接php接收tcp数据格式!
php编程语言可以解析tcp数据包(包括标志位,序列号,窗口长度)吗?TCP协议头最少20个字节 , 包括以下的区域
TCP源端口(Source Port):16位的源端口其中包含初始化通信的端口 。源端口和源IP地址的作用是
标示报问的返回地址 。
TCP目的端口(Destination port):16位的目的端口域定义传输的目的 。这个端口指明报文接收计算
机上的应用程序地址接口 。
TCP序列号(序列码,Sequence Number):32位
TCP应答号(Acknowledgment Number):32位的序列号由接收端计算机使用,重组分段的报文成最初形式 。,如果设置了ACK控制位,这个值表示一个准备接收的包的序列码 。
怎样使用php实现tcp/udp通讯1.在socket_bindphp接收tcp数据格式的时候ip地址不能真回环地址如127.0.0.1
2.server.php后台跑起来php接收tcp数据格式的时候nohup php server.php/var/tmp/a.log 21
一php接收tcp数据格式: udp 方式
1) server.php
?php//error_reporting( E_ALL );set_time_limit( 0 );ob_implicit_flush();$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );if ( $socket === false ) {echo "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n";}$ok = socket_bind( $socket, '202.85.218.133', 11109 );if ( $ok === false ) {echo "socket_bind() failed:reason:" . socket_strerror( socket_last_error( $socket ) );}while ( true ) {$from = "";$port = 0;socket_recvfrom( $socket, $buf,1024, 0, $from, $port );echo $buf;usleep( 1000 );}?
2) client.php
?php$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);$msg = 'hello';$len = strlen($msg);socket_sendto($sock, $msg, $len, 0, '202.85.218.133', 11109);socket_close($sock);?
二php接收tcp数据格式: TCP 方式
1)server.php
?php//error_reporting( E_ALL );set_time_limit( 0 );ob_implicit_flush();$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );socket_bind( $socket, '192.168.2.143', 11109 );socket_listen($socket);$acpt=socket_accept($socket);echo "Acpt!\n";while ( $acpt ) {$words=fgets(STDIN);socket_write($acpt,$words);$hear=socket_read($acpt,1024);echo $hear;if("bye\r\n"==$hear){socket_shutdown($acpt);break;}usleep( 1000 );}socket_close($socket)?
2) client.php
?php$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);$con=socket_connect($socket,'192.168.2.143',11109);if(!$con){socket_close($socket);exit;}echo "Link\n";while($con){$hear=socket_read($socket,1024);echo $hear;$words=fgets(STDIN);socket_write($socket,$words);if($words=="bye\r\n"){break;}}socket_shutdown($socket);socket_close($sock);?
问一下,PHP中的Socket怎么被动接收数据首先是服务端的代码:
?php
$host = 'localhost';
$port = 4888;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not blind to port\n");
$result = socket_listen($socket, 3) or die("Could not set LIstener\n");
while($spawn = socket_accept($socket) or die("Could not readinput\n")){
$input = socket_read($spawn, 1024);
echo $input,"\n";
$input = '7636553:'.trim($input);
//client
$output = $input."\n";
socket_write($spawn, $output, strlen($output));
}
//kill
socket_close($spawn);
socket_close($socket);
echo "close\n";
?
客户端的代码:
?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'localhost',4888) or die("Could not connect\n");
echo 'Write to Socket',"\n";
if(!socket_write($socket, "some data")){
echo 'write failed',"\n";
}
//read respose from socket
while($buffer = socket_read($socket, 1024)){
echo 'Response: ',$buffer,"\n";
}
?
先启动服务器端 , 再执行客户端 。然后,你就可以看见这个效果了 。
PHP如何接收动态数据保存并实时显示到网页上?头部加上超时控制,但对于很多服务器无效 , 因为服务器输出超时很多在服务器控制,所以建议用cmd脚本方式运行此程序:
?php
set_time_limit(0); //禁用脚本超时
// Create the socket and connect
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,'116 。236 。128 。220', 14580);
// Write some test data to our socket
if(!socket_write($socket, "user NoCall pass -1 vers test 1.0 filter b/B* \r\n"))
{
echo("pWrite failed/p");
}
if(!file_exists('socket_log.html')){
file_put_contents('socket_log.html', 'script
var xx = setInterval(function(){ //每5秒刷新一次页面
window.location.reload();
}, 5000);
/script');
}
// Read any response from the socket
while($buffer = socket_read($socket, 64, PHP_NORMAL_READ))
{
echo json_encode($buffer); //转换为json数据输出
//记入文件
file_put_contents('socket_log.html', json_encode($buffer), FILE_APPEND);
}
echo("pDone Reading from Socket/p");
使用方法:用命令行方式运行此脚本
php script.php
脚本会一直运行到接收数据结束,并持续将收到的数据写入socket_log.html文件 。
在浏览器打开socket_log.html页面,此页面会自动每5秒刷新一次,来显示最新的数据 。
确保程序有权限创建及写入socket_log.html文件
【php接收tcp数据格式的简单介绍】关于php接收tcp数据格式和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读