473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2772
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: martinnitram | last post by:
Dear all, following are some piece of my code (mainly create a socket connection to server and loop to receive data): # function to create and return socket def connect(): server_config =...
6
by: Michael Kennedy [UB] | last post by:
Hi, I have a project using the TcpClient and its associated NetworkStream. Everything works well except for one condition which I haven't found any information about dealing with: How do I...
0
by: Tomasz Judycki | last post by:
Hi all! I've got a problem - probably easy to solve but I practically don't know Perl nor modules so it's hard for me. The problem concerns two processes communicating via sockets. I need to...
4
by: prashantkumar1982 | last post by:
I am using the select call to read from many sockets. I don't want to call read on every socket to check if it is closed, as it defeats the purpose of using the select call. Is there any way to...
2
by: White Spirit | last post by:
I have a function within an application where a client connected to a server continuously sends data. The code on the server side is of the following form: Socket socket = receiveSocket;...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
2
by: jlparise | last post by:
Hey everyone, I am really rusty on my Java socket programming and am having some confusing problems. I don't need the code debugged(even though it doesn't work). I'm not going to make you dig...
0
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, here might not be best place to ask this but, Does anyone know how to rollback to particular check point not back to start of transaction? We are handling a socket connection as a...
11
Plater
by: Plater | last post by:
I'm having a bit of trouble figuring out how to detect if a socket is closed or not. Outside of using read()/write(). I also don't want to set the socket to non-blocking if at all possible. I wrote...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.