473,729 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2805
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.goo glegroups.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.goo glegroups.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
3814
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 = ('192.168.1.50', 3333); sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect()
6
18498
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 detected when the socket connection was closed on the server-side? That is, if my client connects to the server and everything is initialized
0
1577
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 know that one of them died already and there is no point in sending anything to it. This is a script iris_s.pl acting as server : ===========================================
4
1808
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 know whether one of the sockets have been closed, without actually testing each socket.
2
2763
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; NetworkStream networkStream = new NetworkStream (socket); System.IO.StreamReader streamReader = new System.IO.StreamReader (networkStream); while (socket.Connected)
6
15604
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 0 but it seems a little poor to me. :-\ Is there a reliable way to test such socket 'state'?
2
10512
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 through all the abstraction layers and such. Anyways here is my issue. The application sends a request for data to the server and the server responds. However, the server then sends a TCP Fin message to close the connection which I can see through...
0
1391
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 single transaction and during a socket communication, many packets can be received and contents of them will be written to database many times. At the start of socket communication, oracle database connection is opened and at the end, all writes to...
11
18040
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 a function that reads a single char(or byte, whatever) and wanted to use it in a simulated blocking and nonblocking way. It works fine for waiting for the correct timeouts, but since I use ioctl() to detect if there is data to be read, I won't...
0
8923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9203
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9151
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8155
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.