Keep-Alive socket with HTTP server under C# (.net) how to send several queries?

孤者浪人 提交于 2019-12-06 08:11:10

You are reading the stream until it is closed by the remote side. It makes sense that after that point you won't get any data.

You need to make the server keep the connection alive. This is done by setting a keep alive HTTP header. You seem to be doing that.

With HTTP keep alive the server will set the Content-Length header to tell you how many bytes to read. Read exactly that many bytes. After that, send the next request.

TCP keep alives (SocketOptionName.KeepAlive) have nothing to do with HTTP keep alives. Remove that code.

In addition the mistakes in my other answer you are not instructing the server to keep the connection alive. The proper header value is keep-alive. That's why the server closes the connection.

Thx. Now I have found solution:

1) Don't forget check socket.Available to decide is stream read to end, checking of stream.Read(...) == 0 or Recive(...)==0 is not real thing

2) Don't trust Internet - some servers return Keep-Alive but it's not true - connection is opened, but all subrequests fails

3) Snippet in question is working if reading logic will looks like

while (socket.Available != 0)
{
    socket.Receive(bigbuff);
}

4) It's not 100% always good (u need check some other socket things to controll reading), but on sites with real Keep-Alive support it works well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!