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

Socket Connected Status

P: 2
I have developed a C# application which sends the data to the connected client.
I am using TCP/IP protocol. The client connects with my application using 'Telnet command' through command prompt.Once the client is connected the application starts sending data to the client i.e. on the command prompt.The data is comprised of 10,000 lines and one line is sent after every 1 second to the client. The application is working pretty good. But whenever network issue arises(network deadly slows down) the application failed to send the data to the client and Telnet doesnt display "any connection to host is lost" message . what i want to do is if connection breaks up due to network problems then display a "client disconnected" message on my application. I am not succeeded yet in my task. because before i send the data i check the connection and socket.Connected still returns true even when network slows down.
How can i resolve this issue ????
please help me
Feb 25 '09 #1
Share this Question
Share on Google+
3 Replies

P: 2
is there no one eligible to answer my query...
Mar 5 '09 #2

P: 13,264
Have a timeout period and display the message when the timeout expires before the data has been sent?
Mar 5 '09 #3

Expert 5K+
P: 7,872
The .Connected property is only updated after a read/write, you would need to try to send data and have it fail before the .Connected property would be updated.
I have posted a simple solution in previous threads about this subject, but a user pointed out a situation in which they believe it did not work (power failure). I have not yet tested that theory.

Expand|Select|Wrap|Line Numbers
  1.         /// <summary>
  2.         /// Determines the connected state of a given socket. Note: Socket must not have data available to be read in its buffer and have no pending connections
  3.         /// </summary>
  4.         /// <param name="s">A socket with no data available to be read and no pending connections</param>
  5.         /// <returns>True on disconnected socket</returns>
  6.         public bool isDisconnected(Socket s)
  7.         {
  8.             bool part1 = s.Poll(1000, SelectMode.SelectRead);
  9.             bool part2 = (s.Available == 0);
  10.             //if (part1 & part2)//connection is closed 
  11.             return (part1 && part2); 
  12.         }
Mar 5 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.