PC Review


Reply
Thread Tools Rate Thread

How can i implement the timeout in TCPClient?

 
 
Shine choi
Guest
Posts: n/a
 
      4th Sep 2003
Hi.. all

My application communicate using TCPClient socket.
Below is my soucrce code.
It works well if server program is executing.
But, when the server computer powers off or server program
is not executed, it takes long time to check the
TCPClient's connect Server.

So, i want to implement function as like timeout.
If there is no response in special time, i want to exit
the routine.
TCPClient has sendtimeout, receivetimeout function.
But, it support .NET Full framework.
In.NET CF, there is no timeout.

How can i solve upper problem?

Help..

Have a nice day...


/////////////////////////////////////////////////////

TcpClient client = new TcpClient();
try
{
int port = Convert.ToInt16(var_port);
IPAddress serverIP = IPAddress.Parse(var_ipaddress);
client.Connect(serverIP,port);
}
catch
{
}
IsConnected = true;
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);

string receive_data;
string commandstr = string.Empty;
string kind_str = string.Empty,info_area = string.Empty;
int area_count = 0, in_count = 0;

if (stream.CanWrite == true)
{
writer.WriteLine(sendstr);
writer.Flush();
}
while(IsConnected)
{
receive_data = reader.ReadLine();
}

.....


///////////////////////////////////////////////////////
 
Reply With Quote
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      4th Sep 2003
Instead of TCPClient use lower-level Socket class and asynchronous methods:
BeginConnect/EndConnect
BeginSend/EndSend
BeginReceive/EndReceive

"Shine choi" <(E-Mail Removed)> wrote in message
news:106a01c3729e$1382e310$(E-Mail Removed)...
> Hi.. all
>
> My application communicate using TCPClient socket.
> Below is my soucrce code.
> It works well if server program is executing.
> But, when the server computer powers off or server program
> is not executed, it takes long time to check the
> TCPClient's connect Server.
>
> So, i want to implement function as like timeout.
> If there is no response in special time, i want to exit
> the routine.
> TCPClient has sendtimeout, receivetimeout function.
> But, it support .NET Full framework.
> In.NET CF, there is no timeout.
>
> How can i solve upper problem?
>
> Help..
>
> Have a nice day...
>
>
> /////////////////////////////////////////////////////
>
> TcpClient client = new TcpClient();
> try
> {
> int port = Convert.ToInt16(var_port);
> IPAddress serverIP = IPAddress.Parse(var_ipaddress);
> client.Connect(serverIP,port);
> }
> catch
> {
> }
> IsConnected = true;
> NetworkStream stream = client.GetStream();
> StreamReader reader = new StreamReader(stream);
> StreamWriter writer = new StreamWriter(stream);
>
> string receive_data;
> string commandstr = string.Empty;
> string kind_str = string.Empty,info_area = string.Empty;
> int area_count = 0, in_count = 0;
>
> if (stream.CanWrite == true)
> {
> writer.WriteLine(sendstr);
> writer.Flush();
> }
> while(IsConnected)
> {
> receive_data = reader.ReadLine();
> }
>
> .....
>
>
> ///////////////////////////////////////////////////////



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to setup C# TCPClient timeout =?Utf-8?B?R2Vvcmdl?= Microsoft C# .NET 8 13th Aug 2007 09:13 PM
Timeout waiting for an answer in TCPClient QDL Microsoft ASP .NET 0 13th Dec 2006 10:20 AM
TcpClient timeout? Danny Tuppeny Microsoft C# .NET 2 9th Oct 2005 07:28 PM
TCPCLIENT and timeout Peter Stojkovic Microsoft VB .NET 3 10th Mar 2005 06:03 PM
TCPClient - Write timeout Niklas Microsoft Dot NET Compact Framework 3 27th Oct 2004 09:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:41 PM.