donglun7151 2015-04-08 03:52
浏览 77
已采纳

socket_write:如何在不关闭连接的情况下发送多个数据包?

I have this code:

$requestCount = 0;
$maxRequestCount = 10;

$ip = "192.168.0.100";
$port = 10000;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connect = socket_connect($socket, $ip, $port);

while(true){
    if($requestCount == $maxRequestCount){break;}

    $write = socket_write($socket, $getHTTP, strlen($getHTTP));
    echo "Sending TCP message... OK (lenght = $write).<br>";

    $out = '';
    while($out = socket_read($socket, 65536)){echo "Reading response... OK (lenght = ". strlen($out).")<br>";}

    echo "<br>";

    usleep(100);
    $requestCount++;
}

socket_close($socket);

When the first request is made the connection is already closed (FYN, ACK). How do i send 10 packets and then the connection is closed?

enter image description here

  • 写回答

3条回答 默认 最新

  • dongshi2836 2015-04-08 05:30
    关注

    You can't. Just like a real-world conversation, there is no way to force somebody who isn't interested to keep listening. In the same way, you can't stop the computer on the other end of your socket from closing it.

    Judging from variable names in your code, it looks like you're sending HTTP requests (just on a different port). HTTP servers have the option of closing the connection after they respond to the first request they get in that connection. That's what appears to be happening here. You will have to create a new socket and reconnect to send each request.

    Another note: TCP doesn't have "packets". It is a stream oriented connection. I know that sounds like a pedantic difference, but it doesn't make sense to ask how you would "send multiple packets without closing the connection", because you don't get to control how TCP sends your messages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算