如何在控制台中添加while循环 [英] how do i add while loop to my console

查看:149
本文介绍了如何在控制台中添加while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C#中将while循环添加到控制台中

while循环应测试主机服务器上是否有telnet

如果没有telnet,则应继续重试,直到连接成功



下面是我想在while循环中添加到下面的代码

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

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

.

推荐答案

尝试如下操作:

Try something like this:

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


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


这篇关于如何在控制台中添加while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆