douzhan8395 2012-04-23 05:51
浏览 316
已采纳

TCP连接只发送一种不接收的数据

I have fought enough with this: I am trying to write a TCP client code and this code has to send a data to a server and and get a response. The client code is as follows:

<?php
$str3 = "Test Data";
echo $str3;
$fp = stream_socket_client("tcp://192.168.1.26:12000", $errno, $errstr, 30);
if (!$fp) 
{
    echo "$errstr ($errno)<br />
";
    echo "Some problem! </br>";
} 

else 
{   
    fwrite($fp,$str3);
    $str = "";

    while ($str == "")
    {
       $str = fgets($fp, 1024);
    }

    fclose($fp);
    echo $str;
}
?> 

The remote host receives the data and responds back too. Unfortunately, I dont get the data in $str. I do not control the remote host and I can only see that it has received data and generated response. Can you suggest, where exactly am I going wrong?

  • 写回答

1条回答 默认 最新

  • doouzlrvb01417498 2012-04-23 05:57
    关注

    You may wish to try a more idiomatic read loop; I've stolen this code from http://www.php.net/manual/en/function.fgets.php:

    while (($buffer = fgets($fp, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($fp)) {
        echo "Error: unexpected fgets() fail
    ";
    }
    fclose($fp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料