Click here to Skip to main content
15,910,603 members
Home / Discussions / C#
   

C#

 
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes10-Oct-11 23:53
Grimes10-Oct-11 23:53 
GeneralRe: TcpClient - Detecting closed connections Pin
BobJanova11-Oct-11 1:26
BobJanova11-Oct-11 1:26 
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes11-Oct-11 2:05
Grimes11-Oct-11 2:05 
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes11-Oct-11 0:02
Grimes11-Oct-11 0:02 
GeneralRe: TcpClient - Detecting closed connections Pin
elgaabeb11-Oct-11 2:13
elgaabeb11-Oct-11 2:13 
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes11-Oct-11 2:19
Grimes11-Oct-11 2:19 
GeneralRe: TcpClient - Detecting closed connections Pin
BobJanova11-Oct-11 3:20
BobJanova11-Oct-11 3:20 
AnswerRe: TcpClient - Detecting closed connections Pin
jschell11-Oct-11 9:21
jschell11-Oct-11 9:21 
TCP the protocol (not TCPClient) for the most part does not do what you are asking.

And although Keep Alive is part of the protocol in general it is not going to be useful. Generally it won't even work unless you control the entire network infrastructure for all the components. And working doesn't mean it is useful.


If a connection pool is in use, then the properties of the connection pool dictate how idle connections are handled. If a connection pool is not in use then idle connections will not be closed (excluding a close from the client.)

A pool will close sockets that are in an error state or closed but that only happens after it is detected. Excluding a pool being configured to send a keep alive (probably not the same as the TCP Keep Alive) it relies on the user functionality to detect such errors. Pool keep alives can be considered to exist only in database pools as determining other types of keep alives depend on the client.

This has nothing to do with language/API either. But briefly looking at the docs for TCPClient I see nothing that suggests a pool is in use.



In general the only way to tell if a socket is still good is to send something and wait for it to indicate it went. This is not 100% but is generally sufficient especially if a reply is expected. Again this is how TCP works.



Additionally, and this impacts the point of Keep Alive (TCP protocol) the point is - why do you think this matters? What exactly are you gaining?

For example lets say you have a data center with two 24x7 servers that talk to each other.
From that the following scenarios exist.
- 99% of the time there is no problem.
- 0.99% of the time there is a socket closure caused by a scheduled bounce of one of the servers.
- 0.01% of the time the socket fails because of a 'network' failure which could include someone kicking out the power cord of one of the servers.

Now for the last case is can occur at ANY time. So the fact that a socket was 'alive' a minute ago doesn't help you any when you actually use it because it can fail when you use it. So the only way do deal with is either your code that USES the socket is written to deal with retries or you accept some infrequent processing errors (and other businesses process deal with it.)

Note that the second case can occur at any time as well.

Conversely the other 99.99% of the time you are now sending keep alives (not necessarily TCP) around the system to no purpose. Why no purpose? Because the system is up. So they don't do anything.

So you have useless traffic which doesn't keep you from writing code to deal with down time anyways.

(An additional considersation is what happens if the server on the other end gets the message but never does anything with it?


Grimes wrote:
How can I get the connection to immediately throw the exception?


Presumably you mean for your test case - but in general the answer is you can't. The only way that happens if is the origination computer already knows that the other end is down. Which only happens if there was a previous error or it got a close from the server. Otherwise the origination MUST wait a non-trivial amount of time for a response. That is how TCP works.

If and ONLY if your computers are in a high quality data center then you can adjust the OS (OS, not app) configuration values to significantly reduce how long it waits. But on windows it cannot be reduced below 30 seconds.

Again this ONLY applies to server WITHIN a data center. It is completely inappropriate for the internet and is unlikely to be appropriate for a business lan.

Grimes wrote:
Could there be a better way to fix this and to see if the connection has
closed?


Closed represents a happy path based on the target serving actually closing the socket. Other failure scenarios do not have anything to with closing the socket.

As an example if a router goes bad the path between the two servers can be unresolvable. Both servers are still up and think their sockets are good but one end never gets messages and the other end fails when it sends.
GeneralRe: TcpClient - Detecting closed connections Pin
Grimes11-Oct-11 19:21
Grimes11-Oct-11 19:21 
QuestionCompare 2 Datatables Pin
jojoba2010-Oct-11 6:13
jojoba2010-Oct-11 6:13 
AnswerRe: Compare 2 Datatables Pin
André Kraak10-Oct-11 8:27
André Kraak10-Oct-11 8:27 
GeneralRe: Compare 2 Datatables Pin
jojoba2010-Oct-11 8:30
jojoba2010-Oct-11 8:30 
GeneralRe: Compare 2 Datatables Pin
André Kraak10-Oct-11 9:30
André Kraak10-Oct-11 9:30 
QuestionFire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils10-Oct-11 4:27
Tonkpils10-Oct-11 4:27 
AnswerRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
DaveyM6910-Oct-11 7:09
professionalDaveyM6910-Oct-11 7:09 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils13-Oct-11 4:37
Tonkpils13-Oct-11 4:37 
AnswerRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz10-Oct-11 12:13
TheGreatAndPowerfulOz10-Oct-11 12:13 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils11-Oct-11 1:01
Tonkpils11-Oct-11 1:01 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz14-Oct-11 4:51
TheGreatAndPowerfulOz14-Oct-11 4:51 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
Tonkpils18-Oct-11 0:17
Tonkpils18-Oct-11 0:17 
GeneralRe: Fire a C# Event/Delegate in C++, or: EventHandling via DLL Pin
TheGreatAndPowerfulOz18-Oct-11 8:23
TheGreatAndPowerfulOz18-Oct-11 8:23 
QuestionHow to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 4:20
fiaolle10-Oct-11 4:20 
AnswerRe: How to highlight all occurrences of a word in a Word document?? Pin
Pete O'Hanlon10-Oct-11 4:52
mvePete O'Hanlon10-Oct-11 4:52 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
fiaolle10-Oct-11 5:08
fiaolle10-Oct-11 5:08 
GeneralRe: How to highlight all occurrences of a word in a Word document?? Pin
Pete O'Hanlon10-Oct-11 5:19
mvePete O'Hanlon10-Oct-11 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.