473,239 Members | 1,552 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,239 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 20412

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.