Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets fread挂起—;PHP_Sockets - Fatal编程技术网

Sockets fread挂起—;PHP

Sockets fread挂起—;PHP,sockets,Sockets,我使用fsockopen($host,$port)创建了一个套接字,使用fwrite发送数据,并尝试使用fread读取数据。看起来fsockopen成功地创建了套接字,fwrite成功地向套接字发送了数据(返回正数-4),但fread挂起。看起来它正在等待数据读取,但我不明白为什么。。弗里特成功了。我试着用socket\u create、socket\u connect、socket\u send和socket\u recv代替fwrite和fread,但同样的情况也发生了 下面是我的代码摘要:

我使用fsockopen($host,$port)创建了一个套接字,使用fwrite发送数据,并尝试使用fread读取数据。看起来fsockopen成功地创建了套接字,fwrite成功地向套接字发送了数据(返回正数-4),但fread挂起。看起来它正在等待数据读取,但我不明白为什么。。弗里特成功了。我试着用socket\u create、socket\u connect、socket\u send和socket\u recv代替fwrite和fread,但同样的情况也发生了

下面是我的代码摘要:

$socket = fsockopen($host,$port,$errno,$errstr);
if(!$socket){
     echo "$errstr ($errno)<br>";
}

...
$example; //string to send
fwrite($socket,pack("N",strlen($example)),4);
$result = fread($socket,4); <-- Hangs and eventually shuts down the server.
...
这将输出以下数组:

array(7) { 
["stream_type"]=> string(14) "tcp_socket/ssl" 
["mode"]=> string(2) "r+" 
["unread_bytes"]=> int(0) 
["seekable"]=> bool(false) 
["timed_out"]=> bool(false) 
["blocked"]=> bool(true) 
["eof"]=> bool(false) } 

这困扰了我很长时间。任何帮助都将不胜感激。

如果您正在使用流函数进行套接字通信,您可能希望使用流函数来确保实际在线路上发送数据,否则此项的短缺可能只是位于流输出缓冲区中

但这不应该是一个问题。既然你说的结果是一样的,我建议你重新检查一下你使用的是哪种应用程序协议——另一方可能会期望超过4个字节来生成响应


最后的办法总是使用类似or的数据包嗅探器。

非常感谢您的回复。我终于解决了我的问题,这是由于我愚蠢的错误。。。我不得不向套接字发送另一个数据,但我没有。你的回答仍然帮助我尝试了不同的东西,我学到了新的东西,比如fflush、tcpdump和wireshark。再次感谢你!!这就是我在回答的第二部分所说的。你也可以接受答案,你知道……:)对不起,我不知道如何接受你的回答。我刚想出来!非常感谢你的帮助!:)
array(7) { 
["stream_type"]=> string(14) "tcp_socket/ssl" 
["mode"]=> string(2) "r+" 
["unread_bytes"]=> int(0) 
["seekable"]=> bool(false) 
["timed_out"]=> bool(false) 
["blocked"]=> bool(true) 
["eof"]=> bool(false) }