Thursday, 12 September 2013

TcpClient connection attempts in C#

TcpClient connection attempts in C#

I have a simple program. I wanted to check if the server actually is
online so my users can connect to it before doing stuff, so on the form
load event I have the following:
private void frmMain_Load(object sender, EventArgs e)
{
try
{
tcp.Connect("110.174.194.138", 8484);
MessageBox.Show("siccces");
}
catch
{
MessageBox.Show("failed");
}
if (tcp.Connected)
{
//do whatever once program has connected.
}
else
{
MessageBox.Show("FAILED");
}
}
It all works fine when the server is online (and it can connect). It
connects in 0.1 seconds and nothing bad happens .However, when the server
is off, it attempts to reconnect about 5 times I think, which takes up to
15 seconds. How can I make it attempt to connect only once so I can show a
failed message instantly?
Thanks.

No comments:

Post a Comment