How can i implement the timeout in TCPClient?

  • Thread starter Thread starter Shine choi
  • Start date Start date
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();
}

.....


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top