469,306 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

how to setup C# TCPClient timeout

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
<timeoutinterval, if no server response is received, the GetStream method
will return will return other than wait forever.
thanks in advance,
George
Aug 13 '07 #1
4 20102

On Aug 13, 10:44 am, George <Geo...@discussions.microsoft.comwrote:
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/lib...a8(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.

Aug 13 '07 #2
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" wrote:
>
On Aug 13, 10:44 am, George <Geo...@discussions.microsoft.comwrote:
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/lib...a8(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.

Aug 13 '07 #3
George wrote:
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
<timeoutinterval, 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
Aug 13 '07 #4
UL-Tomten wrote:
On Aug 13, 7:43 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
>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 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
Aug 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Daniel | last post: by
2 posts views Thread by Danny Tuppeny | last post: by
3 posts views Thread by Peter Stojkovic | last post: by
3 posts views Thread by Erjan | last post: by
6 posts views Thread by Bjoern Schliessmann | last post: by
Airslash
3 posts views Thread by Airslash | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.