By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

How to detect the real status of a socket?

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
try the Poll method
"Frank Meng" <me*******@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
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

Nov 16 '05 #2

P: n/a
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" <al***@sympatico.ca> wrote in message
news:Zj*********************@news20.bellglobal.com ...
try the Poll method
"Frank Meng" <me*******@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
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


Nov 16 '05 #3

P: n/a
"alien" <al***@sympatico.ca> wrote in message news:<Zj*********************@news20.bellglobal.co m>...
try the Poll method

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.
Nov 16 '05 #4

P: n/a
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.

me*******@hotmail.com (Frank Meng) wrote in message news:<1a**************************@posting.google. com>...
"alien" <al***@sympatico.ca> wrote in message news:<Zj*********************@news20.bellglobal.co m>...
try the Poll method

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.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.