How to detect the real status of a socket?

  • Thread starter Thread starter Frank Meng
  • Start date Start date
F

Frank Meng

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
 
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 said:
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top