Click here to Skip to main content
16,017,833 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to add while loop in C# to my console

the while loop should test if there is a telnet on the host server

if there is no telnet it should keep retrying until connection is suceeded



below is the code I would like to add while loop to below

C#
static void Connect(String server, String message) 
{
  try 
  {
    // Create a TcpClient.
    // Note, for this client to work you need to have a TcpServer 
    // connected to the same address as specified by the server, port
    // combination.
    Int32 port = 13000;
    TcpClient client = new TcpClient(server, port);

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

    // Get a client stream for reading and writing.
   // Stream stream = client.GetStream();

    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);

    Console.WriteLine("Sent: {0}", message);         

    // Receive the TcpServer.response.

    // Buffer to store the response bytes.
    data = new Byte[256];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    Console.WriteLine("Received: {0}", responseData);         

    // Close everything.
    stream.Close();         
    client.Close();         
  } 
  catch (ArgumentNullException e) 
  {
    Console.WriteLine("ArgumentNullException: {0}", e);
  } 
  catch (SocketException e) 
  {
    Console.WriteLine("SocketException: {0}", e);
  }

  Console.WriteLine("\n Press Enter to continue...");
  Console.Read();
}
.
Posted
v3
Comments
Sergey Alexandrovich Kryukov 9-Nov-11 10:31am    
And what's your problem?
--SA

Try something like this:

C#
TcpClient client = null;
bool connected = false;
int maxTries = 3;
int tries = 0;
do
{
    tries++;
    try
    {
        client = new TcpClient(addr, port);
        connected = true;
    }
    catch (SocketException)
    {
        connected = false;
        Console.Write(string.Format("Connection attempt {0} failed. ", tries);
        if (tries < maxTries)
        {
            Console.WriteLine("Retrying in 5 seconds...");
            Thread.Sleep(5000);
        }
    }
} while (!connected && tries != maxTries);
 
Share this answer
 
v3
Comments
Richard MacCutchan 9-Nov-11 11:36am    
Shouldn't that be
} while (!connected && tries != maxTries);
?
#realJSOP 9-Nov-11 14:00pm    
Yep. I'm blaming having to type code in this stupid editor. :) (Fixed, nonetheless)
C#
static void Connect(String server, String message) 
{
  bool flag = false;   // it is used to handle loop


while(!flag)
// While begins

{
  try 
  {
    // Create a TcpClient.
    // Note, for this client to work you need to have a TcpServer 
    // connected to the same address as specified by the server, port
    // combination.
    Int32 port = 13000;
    TcpClient client = new TcpClient(server, port);
 
    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         
 
    // Get a client stream for reading and writing.
   // Stream stream = client.GetStream();

    NetworkStream stream = client.GetStream();
 
    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);
 
    Console.WriteLine("Sent: {0}", message);         
 
    // Receive the TcpServer.response.

    // Buffer to store the response bytes.
    data = new Byte[256];
 
    // String to store the response ASCII representation.
    String responseData = String.Empty;
 
    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    Console.WriteLine("Received: {0}", responseData);         
 
    // Close everything.
    stream.Close();         
    client.Close();         
    //set value of flag to true so it may terminate from while loop


    flag = true;


  } 
  catch (ArgumentNullException e) 
  {
    Console.WriteLine("ArgumentNullException: {0}", e);
    //set flag to false so it may remain into loop
    flag = false;


  } 
  catch (SocketException e) 
  {
    Console.WriteLine("SocketException: {0}", e);
    //set flag to false so it may remain into loop
    flag = false;


  }
 }
  Console.WriteLine("\n Press Enter to continue...");
  Console.Read();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900