0

我需要将一些数据发布到监听服务器并使用 php 中的套接字获取答案。

把工作做好(接收者得到他需要的东西),但是当我试图得到任何 serwer answear 时,我就是不能。它只是一遍又一遍……看起来while永远不会结束,但为什么呢?(在服务器端团队检查并说响应是提交给我的)。

我使用 Php 但 serwer 是 c++(我听说这可能是问题所在)

编码:

ini_set('max_execution_time', 36000); // to test the shile for a longer time, but aparentry responce goes right away

$fp = fsockopen("192.168.x.x", 9999, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {

    echo "Connected<br />\n";

$out = chr(11)."MSH|....some fields there....|".chr(28).chr(13); //chr(x) are ruquired by serwer

if ( fputs($fp, $out, strlen($out)) )
{

    echo "<br />\nwriting message<br />\n";//if I eomment the while below this will show without problem

    while (!feof($fp)) {
       echo fgets($fp, 1024);
    }//just goes and goes... never finding end of file (I gues)
}

    fclose($fp);
}

我在 php 中使用了 3 种不同的方式来打开套接字,它们似乎都有类似的问题。我如何测试出了什么问题?

4

1 回答 1

1

我会尝试使用 fwrite 而不是 fputs,但是您的代码看起来不错,您应该描述一下您使用它的目的、哪个服务器等等,以获取有关它的一些文档。

并选择 TCP/UDP。

$fp = fsockopen('udp://192.168.X.X', '9999', $errno, $errstr, 10);
fwrite($fp, '\\xff\\xff\\xff\\xff\\'); 

试试这个。

 do {
      $contents .= fgetc($fp);
      $socketstatus = socket_get_status($fp);    
    } while($socketstatus["unread_bytes"]); 

 echo $contents;
于 2013-03-29T09:14:23.160 回答