UDP写入套接字并同时从套接字读取(再次进行修改)

发布于 2024-07-15 10:35:13 字数 1004 浏览 6 评论 0原文

服务器:

<?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);
?>

服务器从客户端接收数据,但客户端没有从服务器收到任何数据,并且不停止运行。

Server:

<?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);
?>

Client:

<?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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2024-07-22 10:35:13

如果它是 UDP 套接字,那么为什么首先需要连接。 这还不够吗?

<?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);
?>

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);
?>
尤怨 2024-07-22 10:35:13

我假设 192.168.1.7:11104 是您的服务器。

在服务器中,您需要将数据包发送回接收它的位置:

//...
$msg="Sikerult";
//...
socket_sendto($socket, $msg, strlen($msg), 0, $from, $port);

在客户端中,您要连接到自身。 您应该将其连接到服务器:

$srvIP = '192.168.1.7';
$srvPort = 11104;
$result = socket_connect($sock, $srvIP, $srvPort);
$msg = "Sikerult";
$len = strlen($msg);
socket_send($sock, $msg, $len, 0);
socket_recv($sock, $buf, 12, 0);

I'm assuming that 192.168.1.7:11104 is your server.

In server you need to send packet back to where you've received it from:

//...
$msg="Sikerult";
//...
socket_sendto($socket, $msg, strlen($msg), 0, $from, $port);

In the client you're connecting to itself. You should connect it to server:

$srvIP = '192.168.1.7';
$srvPort = 11104;
$result = socket_connect($sock, $srvIP, $srvPort);
$msg = "Sikerult";
$len = strlen($msg);
socket_send($sock, $msg, $len, 0);
socket_recv($sock, $buf, 12, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文