Connecting Tech Pros Worldwide Help | Site Map

How to detect the real status of a socket?

Frank Meng
Guest
 
Posts: n/a
#1: Nov 16 '05
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.
If I can't fix this problem, I want to detect if the connection is
dead.
if( m_sock == null || !m_sock.Connected )
return false, that mean the client thinks it IS connected to the
server.
But after I sent the message, the server didn't receive anything.
After that, it will never work again, unless the client reconnects the
server.
I can't disconnect the connection after I sent my message, because I
want to hear the response from the server.
If the client can detect the real connection status, the client can
reconnect to the server when the connection is dead.
How do I detect the real status of a socket?

Frank
alien
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to detect the real status of a socket?


try the Poll method
"Frank Meng" <mengfrank@hotmail.com> wrote in message
news:1a370138.0405061803.1948db49@posting.google.c om...[color=blue]
> 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.
> If I can't fix this problem, I want to detect if the connection is
> dead.
> if( m_sock == null || !m_sock.Connected )
> return false, that mean the client thinks it IS connected to the
> server.
> But after I sent the message, the server didn't receive anything.
> After that, it will never work again, unless the client reconnects the
> server.
> I can't disconnect the connection after I sent my message, because I
> want to hear the response from the server.
> If the client can detect the real connection status, the client can
> reconnect to the server when the connection is dead.
> How do I detect the real status of a socket?
>
> Frank[/color]


Ryan Gregg
Guest
 
Posts: n/a
#3: Nov 16 '05

re: How to detect the real status of a socket?


You might also try enabling the keep alive socket option, which will add to
the amount of network traffic a socket will cause, but also tends to allow
the socket to notice that it is no longer connected a little faster than any
other method. You've discovered part of the wonders of socket programming,
which is that if something happens in the network level, it can be difficult
to determine if a socket is really still connected or not.

Ryan Gregg

"alien" <alien@sympatico.ca> wrote in message
news:ZjCmc.44017$3Q4.1057579@news20.bellglobal.com ...[color=blue]
> try the Poll method
> "Frank Meng" <mengfrank@hotmail.com> wrote in message
> news:1a370138.0405061803.1948db49@posting.google.c om...[color=green]
> > 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.
> > If I can't fix this problem, I want to detect if the connection is
> > dead.
> > if( m_sock == null || !m_sock.Connected )
> > return false, that mean the client thinks it IS connected to the
> > server.
> > But after I sent the message, the server didn't receive anything.
> > After that, it will never work again, unless the client reconnects the
> > server.
> > I can't disconnect the connection after I sent my message, because I
> > want to hear the response from the server.
> > If the client can detect the real connection status, the client can
> > reconnect to the server when the connection is dead.
> > How do I detect the real status of a socket?
> >
> > Frank[/color]
>
>[/color]


Frank Meng
Guest
 
Posts: n/a
#4: Nov 16 '05

re: How to detect the real status of a socket?


"alien" <alien@sympatico.ca> wrote in message news:<ZjCmc.44017$3Q4.1057579@news20.bellglobal.co m>...[color=blue]
> try the Poll method[/color]
Thank you for your message.
I added some codes, so every time I will check with Poll first before
sending data.

if(m_socket.Poll(-1, SelectMode.SelectWrite))
{
LogMessage("This Socket is writable.");
}
Surprisingly, it ALWAYS gives me "This Socket is writable".
Even client is in a single thread, if client keeps sending messages
(sometimes only 2 messages in a short time period) without waiting for
response, some packages will never reach the server.
The strange thing is that once I sent message A, then message B.
Message B ( the latter one) reached the server, but message A got
lost.
What is wrong?
I think that servers (like web servers) should be able to handle
thousands of accesses simultaneously.
Frank Meng
Guest
 
Posts: n/a
#5: Nov 16 '05

re: How to detect the real status of a socket?


I just found out socket is a slow method for inter process
communication.
I don't have a lot of connections, but I need to transfer several data
packages from user's process to a system service in a short period of
time from time to time.
After data was transferred, they will be processed with the system
service.
Maybe socket is not my solution.
What should I use then? MMF or named pipe?
I don't mind using Mutex etc. to slow down the process, but I don't
want to loss data.

mengfrank@hotmail.com (Frank Meng) wrote in message news:<1a370138.0405070935.1ac00641@posting.google. com>...[color=blue]
> "alien" <alien@sympatico.ca> wrote in message news:<ZjCmc.44017$3Q4.1057579@news20.bellglobal.co m>...[color=green]
> > try the Poll method[/color]
> Thank you for your message.
> I added some codes, so every time I will check with Poll first before
> sending data.
>
> if(m_socket.Poll(-1, SelectMode.SelectWrite))
> {
> LogMessage("This Socket is writable.");
> }
> Surprisingly, it ALWAYS gives me "This Socket is writable".
> Even client is in a single thread, if client keeps sending messages
> (sometimes only 2 messages in a short time period) without waiting for
> response, some packages will never reach the server.
> The strange thing is that once I sent message A, then message B.
> Message B ( the latter one) reached the server, but message A got
> lost.
> What is wrong?
> I think that servers (like web servers) should be able to handle
> thousands of accesses simultaneously.[/color]
Closed Thread