UDP同时写入socket和从socket读取(再次修改) [英] UDP write to socket and read from socket at the same time(again with modification)
本文介绍了UDP同时写入socket和从socket读取(再次修改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
服务器:
<?php
error_reporting(E_ALL | E_STRICT);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '192.168.1.7', 11104);
$from = "";
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);
//$buf=socket_read($socket, 2048);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
$msg="Sikerult";
//socket_write($socket, $msg, strlen($msg));
socket_sendto($socket, $msg, strlen($msg), 0, '192.168.1.6', 11105);
//socket_close($socket);
?>
客户:
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$result = socket_connect($sock, '192.168.1.6', 11105);
$msg = "Sikerult";
$len = strlen($msg);
//socket_write($sock, $msg, strlen($msg));
socket_sendto($sock, $msg, $len, 0, '192.168.1.7', 11104);
//$buf=socket_read($sock, 2048);
socket_recvfrom($sock, $buf, 12, 0, $from, $port);
echo $buf;
socket_close($sock);
?>
服务器从客户端接收数据,但客户端从服务器没有得到任何数据并且没有停止运行.
The server receives the data from the client but the client got nothing from the server and not stop running.
推荐答案
如果是 UDP 套接字,那么首先为什么需要连接.这还不够吗?
If it is a UDP socket then why do you need to connect in the first place. Doesn't this suffice?
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = "Sikerult";
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '192.168.1.7', 11104);
socket_recvfrom($sock, $buf, 12, 0, '192.168.1.7', 11104);
echo $buf;
socket_close($sock);
?>
这篇关于UDP同时写入socket和从socket读取(再次修改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文