Hi,
lets say I have a connected SOCKET s. At some point in time, I want to know
if the "s" is still valid, that it is still connected. Is there any API that
I can give me this information?
And can I register some callback like thing, that would inform me when "s"
disconnection happens? What I usually do is while I call "send" or "recv", I
get the socket_error and through that I know whats the status. But in this
situation actually I cant wait to call send or recv to know that the socket
is still valid or not.
regards,
Ab. 6 2033
Abubakar wrote: lets say I have a connected SOCKET s. At some point in time, I want to know if the "s" is still valid, that it is still connected. Is there any API that I can give me this information?
And can I register some callback like thing, that would inform me when "s" disconnection happens? What I usually do is while I call "send" or "recv", I get the socket_error and through that I know whats the status. But in this situation actually I cant wait to call send or recv to know that the socket is still valid or not.
You can call recv with the zero byte buffer. I'd like to point out that
the programmer should keep track of this sort of things, just as much as
with pointers. There's no function that will tell you whether some
pointer points to a valid object, if you deleted the object then the
pointer is invalid. In the case of sockets, if you called closesocket
then the socket handle is invalid.
The major problem is what if a socket is closed and a new one is opened
with the same value. Any type of check will tell you that "s" is valid,
which is not true.
I deal with this by having a struct that holds a socket, validity flag,
and a reference counter. If the flag is false then the socket is not to
be used/referenced anymore, and when ref_cnt drops to zero destroy the
struct and close the socket.
Thanks for the reply.
I will try to explain my design and try to explain why I cant call recv or
send in this case. Whats happening is that I have a seperate thread that has
to send data (to the socket by calling "send" function) *whenever* the data
becomes available, and this thread does nothing else. So naturally I cannot
keep the thread's infinite loop running all the time. So I use
WaitForSingleOb ject along with a handle created using CreateEvent. The
thread proc is actually a public method of a class that has the infinite
loop. So I have another public method, in the same class lets call it
setMessage(). Let me explain in steps what happens:
1 - something calls setMessage and passes the data to it in its parameter.
2 - setMessage copies this data to some local variable of this class.
3 - setMessage calls the SetEvent
4 - due to SetEvent call, the WaitForSingleOb ject returns and the next lines
of code takes the copied data and passes it to the "send" socket function.
It then returns and goes again in the waitforsingleob ject call and so blocks
again.
Now for client disconnection notifications purposes I need to be informed at
any time when the socket connection breaks so that I can do some gui updates
based on this. But as you have read above, since I'm in a blocking call to
waitforsingleob ject, I cant call "send" or recv. Should I have another
thread calling "send" with zero buffer length after equal interval just to
check if this socket connection is going fine or not? This doesnt seems nice
idea to me.
I need suggestion on this design, how could I make it better? How are others
doing it?
Regards,
Ab.
"Mihajlo Cvetanović" <ma*@RnEeMtOsVe Et.co.yu> wrote in message
news:#H******** *****@TK2MSFTNG P09.phx.gbl... Abubakar wrote: lets say I have a connected SOCKET s. At some point in time, I want to
know if the "s" is still valid, that it is still connected. Is there any API
that I can give me this information?
And can I register some callback like thing, that would inform me when
"s" disconnection happens? What I usually do is while I call "send" or
"recv", I get the socket_error and through that I know whats the status. But in
this situation actually I cant wait to call send or recv to know that the
socket is still valid or not.
You can call recv with the zero byte buffer. I'd like to point out that the programmer should keep track of this sort of things, just as much as with pointers. There's no function that will tell you whether some pointer points to a valid object, if you deleted the object then the pointer is invalid. In the case of sockets, if you called closesocket then the socket handle is invalid.
The major problem is what if a socket is closed and a new one is opened with the same value. Any type of check will tell you that "s" is valid, which is not true.
I deal with this by having a struct that holds a socket, validity flag, and a reference counter. If the flag is false then the socket is not to be used/referenced anymore, and when ref_cnt drops to zero destroy the struct and close the socket.
Abubakar wrote: Now for client disconnection notifications purposes I need to be informed at any time when the socket connection breaks so that I can do some gui updates based on this. But as you have read above, since I'm in a blocking call to waitforsingleob ject, I cant call "send" or recv. Should I have another thread calling "send" with zero buffer length after equal interval just to check if this socket connection is going fine or not? This doesnt seems nice idea to me.
Do you ever call recv? If the communication protocol is such that you
only send messages, but don't receive confirmation to them, then there's
a chance that a message that you think is received on the other end
actually isn't received at all.
There are situations when even the TCP/IP stack (in the operating
system) "thinks" that the connection is alive, while it is really dead
(I call them zombie connections). If TCP/IP stack believes this, so
shall you. Your first an last line of defense of these situations is a
communication protocol that facilitates request-response mechanism. If a
response doesn't arrive in a timely fashion (you get to choose the
time-out period) then you declare a time out on a connection and declare
the connection dead for all practical purposes.
The use of such protocol can't be "workaround ed" if you need to know
whether the message is *processed* on the other end (and *that* is what
you actually want to know, and not whether the message arrived, or even
whether the connection is broken).
> Do you ever call recv? If the communication protocol is such that you only send messages, but don't receive confirmation to them, then there's a chance that a message that you think is received on the other end
yes I do exactly that. I send a message called "hello" and than the other
end responds in "hello_ack" (while I'm still in recv) after which I indicate
in the gui that the message was successfuly sent. But after gui notification
I have nothing more to do but wait for someone to click on the gui's one
button to send the message again and after which i call recv again to get
the acknowledgment. This cycle repeats. And the way I'm stopping the thread
is through waitforsingleob ject as i explained in my previous reply.
Ab.
"Mihajlo Cvetanović" <ma*@RnEeMtOsVe Et.co.yu> wrote in message
news:#2******** ******@TK2MSFTN GP12.phx.gbl... Abubakar wrote: Now for client disconnection notifications purposes I need to be
informed at any time when the socket connection breaks so that I can do some gui
updates based on this. But as you have read above, since I'm in a blocking call
to waitforsingleob ject, I cant call "send" or recv. Should I have another thread calling "send" with zero buffer length after equal interval just
to check if this socket connection is going fine or not? This doesnt seems
nice idea to me.
Do you ever call recv? If the communication protocol is such that you only send messages, but don't receive confirmation to them, then there's a chance that a message that you think is received on the other end actually isn't received at all.
There are situations when even the TCP/IP stack (in the operating system) "thinks" that the connection is alive, while it is really dead (I call them zombie connections). If TCP/IP stack believes this, so shall you. Your first an last line of defense of these situations is a communication protocol that facilitates request-response mechanism. If a response doesn't arrive in a timely fashion (you get to choose the time-out period) then you declare a time out on a connection and declare the connection dead for all practical purposes.
The use of such protocol can't be "workaround ed" if you need to know whether the message is *processed* on the other end (and *that* is what you actually want to know, and not whether the message arrived, or even whether the connection is broken).
Abubakar wrote: yes I do exactly that. I send a message called "hello" and than the other end responds in "hello_ack" (while I'm still in recv) after which I indicate in the gui that the message was successfuly sent. But after gui notification I have nothing more to do but wait for someone to click on the gui's one button to send the message again and after which i call recv again to get the acknowledgment. This cycle repeats. And the way I'm stopping the thread is through waitforsingleob ject as i explained in my previous reply.
Why do you need an indication of broken connection? If there is a
response then the connection is valid, if there's no response after a
time-out value then the connection is invalid. You still must use a
time-out functionality, even if you check for connection validity.
Anyway, you could call recv(0) in GUI in timer event handler. Also, you
should call WaitForMultiple Objects (instead of WFSO) with an extra event
that indicates that you should stop waiting, possibly reconnect or
whatever. You set this event if recv(0) returns an error.
Whenever you have secondary threads that may wait on something you
*must* add at least one more event to every waiting list. This event
indicates only that the thread should stop waiting. Every Sleep then
becomes WaitForSingleTh read, every WFSO becomes WaitForMultiple Threads,
and every WFMO gets an additional handle in its list. When main thread
should finish it should set that event, wait until all secondary threads
finish, and then finish itself. Anyway, you could call recv(0) in GUI in timer event handler. Also, you should call WaitForMultiple Objects (instead of WFSO) with an extra event that indicates that you should stop waiting, possibly reconnect or whatever. You set this event if recv(0) returns an error.
Sounds like a pretty good idea. I think this technique will solve the
problem.
Thanks.
Ab.
"Mihajlo Cvetanović" <ma*@RnEeMtOsVe Et.co.yu> wrote in message
news:uM******** ******@TK2MSFTN GP10.phx.gbl... Abubakar wrote: yes I do exactly that. I send a message called "hello" and than the
other end responds in "hello_ack" (while I'm still in recv) after which I
indicate in the gui that the message was successfuly sent. But after gui
notification I have nothing more to do but wait for someone to click on the gui's one button to send the message again and after which i call recv again to
get the acknowledgment. This cycle repeats. And the way I'm stopping the
thread is through waitforsingleob ject as i explained in my previous reply.
Why do you need an indication of broken connection? If there is a response then the connection is valid, if there's no response after a time-out value then the connection is invalid. You still must use a time-out functionality, even if you check for connection validity.
Anyway, you could call recv(0) in GUI in timer event handler. Also, you should call WaitForMultiple Objects (instead of WFSO) with an extra event that indicates that you should stop waiting, possibly reconnect or whatever. You set this event if recv(0) returns an error.
Whenever you have secondary threads that may wait on something you *must* add at least one more event to every waiting list. This event indicates only that the thread should stop waiting. Every Sleep then becomes WaitForSingleTh read, every WFSO becomes WaitForMultiple Threads, and every WFMO gets an additional handle in its list. When main thread should finish it should set that event, wait until all secondary threads finish, and then finish itself. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jane Austine |
last post by:
Running Python 2.3 on Win XP
It seems like socket is working interdependently with subprocesses of
the process which created socket.
------------------------------------
#the server side
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.bind(('localhost',9000))
|
by: Jim |
last post by:
Need some comments from anyone willing to help, please. See the code
included below. This compiles with GCC on FreeBSD 4.7. The only
point of it is to accept a socket connection. Nothing else has been
implemented.
It does not work. Everything matches how it should be (according to
the man pages on the relevant functions). It also matches line by
line with code from another application used as a known to be working
reference. ...
|
by: Frank Meng |
last post by:
Hi.
I am trying a csharp sample from
http://www.codeproject.com/csharp/socketsincs.asp .
(Sorry I didn't post all the source codes here, please get the codes
from above link if you want to try).
I had some troubles when I started 6 threads (each thread made a
separate connection) and sent messages to same server simultaneously.
Sometimes, not always, the socket looks like ok, but really it is
dead.
I don't why it happens.
|
by: Khadim |
last post by:
I am using the following code, don't know what am i missing. in BYTE2 I
am not getting any information from the remote application.
any guidance would be highly appreciated, thanking you in advance
// PatentRequestPaket is a structure defined above in the class
//public struct PatentRequestPacket
//{
// public char Key;// = new char;
// public char PatentNumber;// = new char;
// public int NumPages;
|
by: giangiammy |
last post by:
hi all,
I'd like to implement a server socket in java: something linke the
following example.
The problem is that the HTML has not the permission to execute
instruction
serverSocket =
Components.classes.
createInstance(Components.interfaces.nsIServerSocket);
| |
by: zxo102 |
last post by:
Hi everyone,
I am using a python socket server to collect data from a socket
client and then control a image location ( wxpython) with the data,
i.e. moving the image around in the wxpython frame.
But the "app.MainLoop()" in wxpython looks like conflicting with
the "while 1:" in socket server. After I commented the
"app.MainLoop()", everything is working except two things:
1. if I click anywhere on the screen with the mouse, the image is...
|
by: Johan Tibell |
last post by:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socket, args) == -1) {
perror("function");
exit(EXIT_FAILURE);
}
I feel that the ifs destroy the readability of my code. Would it be
|
by: Arno |
last post by:
Hi,
I've written a class for client-socket connection, but I get a lot of times
the error message "Unable to read data from the transport connection" when
restart reading the stream with socket.BeginRead in the Sub
SocketIncomingMsg. I'm debugging now for weeks, but I can't detect where it's
going wrong.
the part of code where it fails:
|
by: Markgoldin |
last post by:
I am sending an XML data from not dontnet process to a .Net via socket
listener.
Here is a data sample:
<VFPData>
<serverdata>
<coderun>updateFloor</coderun>
<area>MD2</area>
<zone>BOXING</zone>
<status>Running</status>
<job>1000139233</job>
|
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...
|
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,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |