使用方法:
cd?C:\phpStudy\php53
php C:\Users\Administrator\Desktop\chenhao\TcpServer.php
?
TcpServer收发十六进制和字符串?
<?php
//创建socket套接字
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//设置阻塞模式
socket_set_block($socket);
//为套接字绑定ip和端口
socket_bind($socket,'192.168.100.1',18899);
//监听socket
socket_listen($socket,4);
echo "tcp server listen port: 18899 \n";
while(true)
{
//接收客户端请求
if(($msgsocket = socket_accept($socket)) !== false)
{
//$str = "this is a service message";
//向连接的客户端发送数据
//socket_write($msgsocket, $str,strlen($str));
$sendStr = '01 03 00 20 00 02 C5 C1'; // 16进制数据
echo "TcpServer:".$sendStr."\n";
$sendStrArray = str_split(str_replace(' ', '', $sendStr), 2); // 将16进制数据转换成两个一组的数组
//向连接的客户端发送数据
for ($j = 0; $j < count($sendStrArray); $j++) {
socket_write($msgsocket, chr(hexdec($sendStrArray[$j]))); // 逐组数据发送
}
//读取请求内容
//$buf = socket_read($msgsocket, 8192,PHP_BINARY_READ);
//echo "Received msg: $buf \n";
$receiveStr = "";
$receiveStr = socket_read($msgsocket, 1024, PHP_BINARY_READ); // 采用2进制方式接收数据
$receiveStrHex = bin2hex($receiveStr); // 将2进制数据转换成16进制
echo "Client:" . $receiveStrHex ."\n";
//操作完之后需要关闭该连接否则 feof() 函数无法正确识别打开的句柄是否读取完成
socket_close($msgsocket);
}
}
Client 收发十六进制和字符串
<?php
$sendStr = '01 03 00 20 00 02 C5 C1'; // 16进制数据
$sendStrArray = str_split(str_replace(' ', '', $sendStr), 2); // 将16进制数据转换成两个一组的数组
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp")); // 创建Socket
echo "正在连接:\n"
if (socket_connect($socket, "192.168.100.1", 18899)) { //连接
for ($j = 0; $j < count($sendStrArray); $j++) {
socket_write($socket, chr(hexdec($sendStrArray[$j]))); // 逐组数据发送
}
//向打开的套集字写入数据(发送数据)
//$st="socket send message";
//$length = strlen($st);
//$s = socket_write($socket, $st, $length);
//从套接字中获取服务器发送来的数据
//$msg = socket_read($socket,8190);
//echo $msg;
$receiveStr = "";
$receiveStr = socket_read($socket, 1024, PHP_BINARY_READ); // 采用2进制方式接收数据
$receiveStrHex = bin2hex($receiveStr); // 将2进制数据转换成16进制
echo "client:" . $receiveStrHex;
}
socket_close($socket); // 关闭Socket
?>
超时及等待用户输入
<?php
//创建socket套接字
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//设置阻塞模式
socket_set_block($socket);
//为套接字绑定ip和端口
//socket_bind($socket,'192.168.254.191',18899);
socket_bind($socket,'192.168.100.1',18899);
//监听socket
socket_listen($socket,4);
echo "tcp server listen port: 18899 \n";
while(true)
{
//接收客户端请求
if(($msgsocket = socket_accept($socket)) !== false)
{
socket_set_option($msgsocket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>3, "usec"=>0 ) ); //发送超时
socket_set_option($msgsocket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) );//接收超时
$receiveStr = "";
$receiveStr = socket_read($msgsocket, 1024, PHP_BINARY_READ); // 采用2进制方式接收数据
$receiveStrHex = bin2hex($receiveStr); // 将2进制数据转换成16进制
echo "Client:" . $receiveStrHex ."\n";
while(true)
{
fgets(STDIN); //等待用户输入回车
$sendStr = '01 03 00 20 00 02 C5 C1'; // 16进制数据
echo "TcpServer:".$sendStr."\n";
$sendStrArray = str_split(str_replace(' ', '', $sendStr), 2); // 将16进制数据转换成两个一组的数组
//向连接的客户端发送数据
$str="";
for ($j = 0; $j < count($sendStrArray); $j++) {
$str=$str.chr(hexdec($sendStrArray[$j])); // 逐组数据拼接
}
socket_write($msgsocket,$str,strlen($str)); //一起发送
fgets(STDIN); //等待用户输入回车
$receiveStr = "";
$receiveStr = socket_read($msgsocket, 1024, PHP_BINARY_READ); // 采用2进制方式接收数据
$receiveStrHex = bin2hex($receiveStr); // 将2进制数据转换成16进制
if(strlen($receiveStrHex)>0)
{
echo "Client:" . $receiveStrHex ."\n";//010304 011d 0234 6b7e
$sendStrArray = str_split(str_replace(' ', '', $receiveStrHex), 2); // 将16进制数据转换成两个一组的数组
for ($j = 0; $j < count($sendStrArray); $j++) {
echo $sendStrArray[$j]." \n"; // 逐组数据发送
}
if(count($sendStrArray)==9)
{
echo "Client解析:\n温度:".( (hexdec($sendStrArray[3])*256+hexdec($sendStrArray[4]))/10 )."度,湿度:".(hexdec($sendStrArray[5])*256+hexdec($sendStrArray[6]))/10 ."%\n";
}
}
}
//操作完之后需要关闭该连接否则 feof() 函数无法正确识别打开的句柄是否读取完成
socket_close($msgsocket);
}
}
|