473,329 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,329 software developers and data experts.

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 20424

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
by: Danny Tuppeny | last post by:
Hi all, I'm using a TcpClient to talk to a newserver. If I don't send any data for a while, will the connection timeout, or does something in the TcpClient keep it alive? If not, and it times...
2
by: Kirk | last post by:
Hello, I'm trying to get the current date/time from a (S)NTP server. It works with UdpClient class, but not with TcpClient class. I want to use TcpClient because i can set timeout values for...
3
by: Peter Stojkovic | last post by:
I have an TcpClient. System.Net.Sockets.TcpClient Everything is working fine. But when the IP-Address is wrong, I DON'T want wait 20seconds for timeout ( seems to be standard ) Is there any...
3
by: Erjan | last post by:
Hi, I am using TcpClient to connect to a device which does not talk NetBios. The TcpClient tries first to do something with Netbios. This probably failes on a timeout and then TcpClient sets up...
6
by: Bjoern Schliessmann | last post by:
Hello, I'm currently trying to implement a simulation program with Kamaelia and need a reliable TCP connection to a data server. From Twisted, I know that a method is called if the connection...
10
by: Zytan | last post by:
I have a TcpClient. I set the read/write timeouts at 1 minute (in milliseconds). I get a NetworkStream from it and confirm the timeouts still exist. I do a NetworkStream.Write() and then a...
2
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down...
3
Airslash
by: Airslash | last post by:
Hello, I've written my own TCPClient and TCPServer class components to handle traffic over the network between applications. When I test the components locally they work. In the local network...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.