How can i implement the timeout in TCPClient?

S

Shine choi

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();
}

.....


///////////////////////////////////////////////////////
 
A

Alex Feinman [MVP]

Instead of TCPClient use lower-level Socket class and asynchronous methods:
BeginConnect/EndConnect
BeginSend/EndSend
BeginReceive/EndReceive
 
Top