how to setup C# TCPClient timeout

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone,


I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, and during
<timeout> interval, if no server response is received, the GetStream method
will return will return other than wait forever.


thanks in advance,
George
 
I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, [...]

The problem is not C# specific, but you have to do this yourself,
using the asynchronous socket usage pattern:
http://msdn2.microsoft.com/en-us/library/bbx2eya8(vs.71).aspx

I think the reason is that no "connection timeout" solution is
inherently fool-proof due to the nature of a socket connection, and it
would be a false promise for the CLR to claim it can support a timeout
for connections. The timeouts you can specify for TcpClient/Socket
operations is only for data transfer.

In short, you BeginConnect() to start the connection attempt, and make
sure you always call EndConnect(), and in between you implement the
timeout yourself, for example by using a timer. If you're using Orcas,
an extension method for a TcpClient/Socket could be an easy way to
handle this.

Since TcpClient is such a thin wrapper around the managed Socket, and
it sounds like you're already trying to get more fine-grained control
over operations, you might want to look at using a Socket directly. It
might provide less surprises than using a TcpClient.
 
Thanks UL-Tomten,


I want to make clear three things (different solutions),

1. If I want to use TCPClient, there is no C# SDK build-in settings for
timetout parameter, right?

2. Asynchronous socket could be used. I think we could use asynchronous
socket at client side and no change at server side, right?

3. Use low layer Socket class? Is there a parameter in Socket class to setup
timeout parameter?


regards,
George

UL-Tomten said:
I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, [...]

The problem is not C# specific, but you have to do this yourself,
using the asynchronous socket usage pattern:
http://msdn2.microsoft.com/en-us/library/bbx2eya8(vs.71).aspx

I think the reason is that no "connection timeout" solution is
inherently fool-proof due to the nature of a socket connection, and it
would be a false promise for the CLR to claim it can support a timeout
for connections. The timeouts you can specify for TcpClient/Socket
operations is only for data transfer.

In short, you BeginConnect() to start the connection attempt, and make
sure you always call EndConnect(), and in between you implement the
timeout yourself, for example by using a timer. If you're using Orcas,
an extension method for a TcpClient/Socket could be an easy way to
handle this.

Since TcpClient is such a thin wrapper around the managed Socket, and
it sounds like you're already trying to get more fine-grained control
over operations, you might want to look at using a Socket directly. It
might provide less surprises than using a TcpClient.
 
1. If I want to use TCPClient, there is no C# SDK build-in settings for
timetout parameter, right?

That's right. (And since TcpClient is a framework class, it's the same
for all CLR languages.)
2. Asynchronous socket could be used. I think we could use asynchronous
socket at client side and no change at server side, right?

Correctamundo. (But this wouldn't change the actual timeout, it would
just allow you to build your own timeout on top of the one TcpClient/
Socket uses internally, which is unspecified in the documentation as
far as I can see, which means you must be prepared for an internal
timeout that's actually less than the one you'd like to use.)
3. Use low layer Socket class? Is there a parameter in Socket class to setup
timeout parameter?

Not for connections. (And if all you're missing in TcpClient is a
connection timeout, then using Socket is no advantage.)
 
Thanks UL-Tomten!

Socket uses internally, which is unspecified in the documentation as
far as I can see, which means you must be prepared for an internal
timeout that's actually less than the one you'd like to use.)

Could you describe in more details please or provide some links? What are
un-documented?
Not for connections. (And if all you're missing in TcpClient is a
connection timeout, then using Socket is no advantage.)

What do you mean for connections? Why no advantage? You mean we can not set
timeout parameter if we use C# low layer Socket related classes to program?


regards,
George
 
Could you describe in more details please or provide some links? What are
un-documented?

The actual connection timeout are un-documented, for example. But I'm
sure you noticed this when you perused the TcpClient documentation.
Not for connections. (And if all you're missing in TcpClient is a
connection timeout, then using Socket is no advantage.)

[...] You mean we can not set timeout parameter if we use C# low
layer Socket related classes to program?

Yes.
 
George said:
I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, and during
<timeout> interval, if no server response is received, the GetStream method
will return will return other than wait forever.

Perhaps you could clarify what it is that the TcpClient class doesn't
already do for you that you want to do.

It is not true that when trying to connect to a server, it will wait
forever if it's unable to connect. TCP always has a timeout for
connections, and this is true even if you are using TcpClient to make
the connection.

So, on the face of it, TcpClient already does what you want. If there's
something else you need, you should be more specific about what that is.

Pete
 
Perhaps you could clarify what it is that the TcpClient class doesn't
already do for you that you want to do.

Maybe you should have read the 7 other messages in this thread. What
George is looking for is a way to control the connection timeout, to
be used by TcpClient when the underlying Socket does a Connect(). Let
me sum up what's been said so far for you:

1. You can't specify a connection timeout, you just have to live with
whatever undocumented timeout the socket implementation uses
2. You can work around the problem by adding your own timeout on top
of the socket timeout, by using BeginConnect
 
UL-Tomten said:
Maybe you should have read the 7 other messages in this thread.

What is your problem?

I did read the 7 other messages, and it is not clear to me that you and
George are on the same page.
What
George is looking for is a way to control the connection timeout, to
be used by TcpClient when the underlying Socket does a Connect().

That is your interpretation, but I saw nothing in his original post to
suggest that's what he's actually looking for, nor anything in his
replies to your posts to suggest that.
Let me sum up what's been said so far for you:

I read the posts, I know what's been said so far, and I do not believe
it's clear that you are actually answering the question that was asked.
That is why I asked for clarification from the OP.

There is no need for you to get snotty about it. I am simply asking for
a more clear question from the OP, to ensure that he is getting the
answer he needs.

Pete
 

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