I made a server-client program with auto connection. But the program only works if I first start the server application because the client needs to connect. My goal is for the client to check with a delay of 2 seconds if the server is already created. I made a while statement for a trial and error cycle
try
{
int a = 1;
while (a == 1)
{
cliente.Connect(IP_End);
if (cliente.Connected)
{
connectRead();
a = 2;
}
else
{
while (!cliente.Connected)
{
int milliseconds = 2000;
Thread.Sleep(milliseconds);
cliente.Connect(IP_End);
MessageBox.Show(text);
if (cliente.Connected)
{
connectRead();
}
}
}
}
}
catch(SocketException se)
{
MessageBox.Show(se.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
the error is :
No connection can be made because the target computer actively refused them 192.168.254.28:100
the method is here :
private void connectRead()
{
STW = new StreamWriter(cliente.GetStream());
STR = new StreamReader(cliente.GetStream());
STW.AutoFlush = true;
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.WorkerSupportsCancellation = true;
}