PHP套接字工作不成功 [英] PHP Sockets is half-working

查看:76
本文介绍了PHP套接字工作不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我让mIRC在端口1235上进行监听(与我同在),我试图制作一个php脚本以连接到我的本地主机服务器上的该端口。

So, i have mIRC making a listen (bear with me on this one) on port 1235 and i tried to make a php script to connect to such port on my localhost server.

我的本​​地主机是一个干净的Apache + PHP,其中mIRC正在另一台计算机上运行(在局域网内)。

My localhost is a clean Apache+PHP with mIRC being run on another computer (inside lan).

该脚本的右半部分为:
1)它连接到1235端口
2)它发送$ i
2.1),但另一方面却没有收到消息(也就是我收到了袜子读取事件,但没有弹出文本)
3)它读取所有传入消息的正确性
4)当'end'是消息
drumroll
时,它关闭5)仅当while函数不存在。
5.1)又名Inifi加载。它仅在通过'end'关闭套接字时显示回显。

The script works half-right as: 1) it connects to the 1235 port 2) it sends the $i 2.1) but on the other side no msg is recieved (aka I get a sock read event but no text pops up) 3) it reads all the incoming messages correclty 4) it closes when 'end' is the message drumroll 5) it only works IF the while function isn't present. 5.1) aka Inifi-loading. it only shows echo when socket is closed via 'end'

这是代码,这是我在php.net上找到的一个简单示例,在此先感谢:)

here is the code, which is a simple example i found on php.net, thanks in advance :)

    <?php
//The Client
error_reporting(E_ALL);

$address = "192.168.1.101";
$port = 1235;

/* 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<br>";
}
$allow = true;
$i = 0;
while ($allow == true)
{
    $i++;
    echo "Sending $i to server.\n<br>";
    socket_write($socket, $i, strlen($i));

    $input = socket_read($socket, 2048);
    if ($input == 'end') {
        $allow = false; 
    }
    echo "Response from server is: $input\n";
    sleep(5);
}

echo "Closing socket...";
socket_close($socket);
?>

这是我在浏览器中看到结束后的意思

this is what i get in browser, after saying 'end'


套接字已成功创建。
尝试在
的端口 1235上连接到
的 192.168.1.101 ...已成功连接到
192.168.1.101。向服务器发送1。服务器的响应是:ok
服务器的响应是:end

socket successfully created. Attempting to connect to '192.168.1.101' on port '1235'...successfully connected to 192.168.1.101. Sending 1 to server. Response from server is: ok Response from server is: end

这是在mIRC中:


测试:89.152.172.21已加入!
阅读
关闭
Blockquote

test: 89.152.172.21 is in! read close Blockquote

如果我将其工作了一个小时,请乘以读为60,然后将N + 1发送到服务器
,但这仅在袜子关闭后(或通过停止一会儿)显示出来

if i had left it working for an hour, multiply "read" for 60 and "sending N+1 to server" but this only shows up AFTER sock close (or by stoping the while)

推荐答案

您有普通的 socket_write() socket_read()调用。不幸的是,套接字功能在设计上不可靠(这种行为是从BSD套接字继承的)。您不能只调用 socket_write()并期望它实际写入字节(听起来很奇怪……)

You have "plain" socket_write() and socket_read() calls. Unfortunately socket functions are unreliable by design (that's behavior inherited from BSD sockets). You can't just call socket_write() and expect it to actually write bytes (as odd as it sounds…)

PHP手动报价:


socket_write()不一定会写入给定缓冲区中的所有字节。根据网络缓冲区等,虽然缓冲区更大,但仅写入一定数量的数据(即使一个字节)也是有效的。您必须当心,这样您就不会无意间忘记传输其余数据。

socket_write() does not necessarily write all bytes from the given buffer. It's valid that, depending on the network buffers etc., only a certain amount of data, even one byte, is written though your buffer is greater. You have to watch out so you don't unintentionally forget to transmit the rest of your data.

您必须检查多少字节已被写入并继续重试,直到发送了所有想要的内容。

You have to check how many bytes have been written and keep retrying until all you wanted has been sent.

阅读时也一样。读呼叫可以读取的内容与您请求的内容相同或更少,并且您希望重试该呼叫。

The same goes for reading. Read call can read as much as you requested or less and you're expected to retry the call.

请确保将套接字设置为阻塞然后循环调用读/写,或者使用(同样糟糕) select()调用来查找哪些套接字可能包含一些数据或可能已完成发送。

Make sure to set sockets to blocking and then call read/write in a loop, or use (equally awful) select() call to find which sockets may have some data or may have finished sending.

如果您想要的API不是PITA,请使用流具有可靠的读/写。

If you want API that's not such PITA, then use streams instead, which have reliable reads/writes.

如果您不一定需要PHP,请查看 Node JS ,它是专门为程序设计的服务器/环境。网络连接寿命长。

If you don't necessarily need PHP, then have a look at Node JS, which is specially designed server/environment for programs with long-lived network connections.

这篇关于PHP套接字工作不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆