471,603 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

How to check for a closed socket

How to check for a closed http socket without losing any data? (MSDN
says to send something to server).
Somewhere in my code (after sending the http header in plain text) I'm
doing a loop with:
Loop:----------------
Socket.Receive //receive http data chunks
Socket.Send //'/r/n' data to check if connection is active
------------------------
The problem is that when connection is closed by the http server, the
send socket function will throw an exception and it is impossible to
receive the last bytes if the data arrives between Socket.Receive and
Socket.Send functions.

How to check for a closed http socket without losing any data? This
must be easy to explain. Someone must have done this... what is the
solution?
(My solution wasn't sending nothing at all, but instead searching for
the "</html>" tag in the bytes received... but that is a slow procedure
and may give deadlock if the main page has no "</html>" tag.)

Thanks for any response,
Nuno Magalhaes.

Dec 2 '05 #1
2 2618
Generally it is not needed to check if connection is available. You can set
keepalive socket option, in order to maintain connection opened.

If server sent data and was forcibly closed you have no way to receive your
data, because only sender of the data can resend then :8-)

Hmm, if you may have deadlock I would recommend you to redesign the
algorithm.
For example you can search for a </html> on the other thread.
Generally it is recommended not to put time consuming work into the same
thread where network I/O is held...

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com
"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
How to check for a closed http socket without losing any data? (MSDN
says to send something to server).
Somewhere in my code (after sending the http header in plain text) I'm
doing a loop with:
Loop:----------------
Socket.Receive //receive http data chunks
Socket.Send //'/r/n' data to check if connection is active
------------------------
The problem is that when connection is closed by the http server, the
send socket function will throw an exception and it is impossible to
receive the last bytes if the data arrives between Socket.Receive and
Socket.Send functions.

How to check for a closed http socket without losing any data? This
must be easy to explain. Someone must have done this... what is the
solution?
(My solution wasn't sending nothing at all, but instead searching for
the "</html>" tag in the bytes received... but that is a slow procedure
and may give deadlock if the main page has no "</html>" tag.)

Thanks for any response,
Nuno Magalhaes.

Dec 2 '05 #2
But it is not me who closes the connection after sending the data, it
is the server! (as described in the HTTP RFC for "Connection: close"
parameter).

The server send all the data in chunks of 8192 bytes I guess but the
last bytes I can't get it because I make a "send" to the server to
check if it is alive and it isn't. After that "send" I can't receive
anymore... but only after a failed "send".

Vadym Stetsyak wrote:
Generally it is not needed to check if connection is available. You can set
keepalive socket option, in order to maintain connection opened.

If server sent data and was forcibly closed you have no way to receive your
data, because only sender of the data can resend then :8-)

Hmm, if you may have deadlock I would recommend you to redesign the
algorithm.
For example you can search for a </html> on the other thread.
Generally it is recommended not to put time consuming work into the same
thread where network I/O is held...

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com
"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
How to check for a closed http socket without losing any data? (MSDN
says to send something to server).
Somewhere in my code (after sending the http header in plain text) I'm
doing a loop with:
Loop:----------------
Socket.Receive //receive http data chunks
Socket.Send //'/r/n' data to check if connection is active
------------------------
The problem is that when connection is closed by the http server, the
send socket function will throw an exception and it is impossible to
receive the last bytes if the data arrives between Socket.Receive and
Socket.Send functions.

How to check for a closed http socket without losing any data? This
must be easy to explain. Someone must have done this... what is the
solution?
(My solution wasn't sending nothing at all, but instead searching for
the "</html>" tag in the bytes received... but that is a slow procedure
and may give deadlock if the main page has no "</html>" tag.)

Thanks for any response,
Nuno Magalhaes.


Dec 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by martinnitram | last post: by
6 posts views Thread by Michael Kennedy [UB] | last post: by
reply views Thread by Tomasz Judycki | last post: by
reply views Thread by =?Utf-8?B?YmJn?= | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.