2015-04-08 03:52
浏览 69


I have this code:

$requestCount = 0;
$maxRequestCount = 10;

$ip = "";
$port = 10000;

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

    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>";



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.

    点赞 打赏 评论
  • dtnmuyoiw680512744 2015-04-08 05:23

    Probably the remote end closed the connection. Probably it's because you have to control what socket_write returns. You have no warranty that socket_write will write your whole buffer at once. If socket_write return 8142 (for example), you have to cut your buffer $getHTTP = substr($getHTTP, 8142); and try a socket_write again. If socket_write(...) === false there is an error and the connection is closed, you have to test it too.

    点赞 打赏 评论
  • duangu4943 2015-04-08 05:34

    From the packet capture it can be seen that you send 342 bytes to the peer (line 4) and then the peer responds with 1446 bytes (line 6) and after that closes the connection (FIN in line 7). From then on the server will not accept more data from the client and thus any attempts to send more data will be rejected with RST.

    I don't know what you are trying to achieve, but since the server closes the connection before the client is done sending the data there is probably some error. You might get more details from the servers response or it might simply be a protocol validation, i.e. the client does not speak the same protocol as the server or not in a proper way. For instance if you would try to use your code to speak with an HTTP server it would be simply wrong because you don't care about keep-alive, body length etc.

    点赞 打赏 评论

相关推荐 更多相似问题