使用UdpClient,如何重试UdpClient.Connect()直到成功为止?

时间:2016-10-05 14:38:32

标签: c# udpclient

我的客户端/服务器程序可以很好地相互协作,但只有在客户端启动之前服务器启动并运行时才能正常运行。如果客户端在第一次尝试时无法连接,我就无法再次尝试。

这是我的客户connect方法。

public void connect()
    {          
        IPAddress server_address = IPAddress.Parse("127.0.0.1");
        IPEndPoint server_ip = new IPEndPoint(server_address, 5685);
        Console.WriteLine("2");
        bool connected = false;
        while (!connected)
        {
            try
            {
                Console.WriteLine("IN CONNECTED");
                udp_client.Connect(server_ip);
                byte[] send_data = Encoding.ASCII.GetBytes("INIT");
                udp_client.Send(send_data, send_data.Length);
                byte[] received_bytes = udp_client.Receive(ref server_ip);
                string received_data = Encoding.ASCII.GetString(received_bytes);
                if (received_data == "INIT")
                {
                    connected = true;
                    Console.WriteLine("RECEIVED INIT");
                    listen(server_ip);
                }

            }

            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }

我希望看到的是udp_client.Connect(server_ip)循环,直到我收到" INIT"来自服务器的消息。

目前看来,没有循环。它似乎陷入udp_client.Receive(ref server_ip)

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

这是伪代码 - 您必须将某些东西移动到类范围以允许将来发送/接收(您使用不同的方法执行此操作)。这仅用于向您展示连接阻止时的连接方式:

bool isClientConnected = false;
var connector = new System.ComponentModel.BackgroundWorker();

public void connectToUDP(){
    connector.DoWork+= connect;
    connector.RunWorkerAsync();
} 

 private void connect(object sender, DoWorkEventArgs e)
{          
    IPAddress server_address = IPAddress.Parse("127.0.0.1");
    IPEndPoint server_ip = new IPEndPoint(server_address, 5685);
    Console.WriteLine("2");

                try
        {
            Console.WriteLine("Waiting for server...");
            udp_client.Connect(server_ip);
            byte[] send_data = Encoding.ASCII.GetBytes("INIT");
            udp_client.Send(send_data, send_data.Length);
            byte[] received_bytes = udp_client.Receive(ref server_ip);
            string received_data = Encoding.ASCII.GetString(received_bytes);
            if (received_data == "INIT")
            {
                isClietConnected = true;
                Console.WriteLine("now connected");
                listen(server_ip);
            }


        catch (Exception e)
        {
            Console.WriteLine(e);
        }

}
public bool sendReceiveUDP(string send){
     if(!isClientConnected){
          return false;
     }
     //perform send
     return true;
 }

然后,您将使用类范围维护连接的会话,并使用其他方法发送/接收。这仅用于连接,因为您只需要执行一次。

你如何设置这样的东西:

  private bool isConnected = false();
  private bool send(){
      if(!isConnected){
           connect();
      }
      //send
  }
  private bool connect(){
      if(!isConnected){
          //launch connection thread
      }
   }
  private delegate void onNewReceive(string message);
  public event onNewReceive onNewReceiveEvent;
  public void fireEvent(string message){
         onNewReceiveEvent.Invoke(message);
  }
       private void waitForData(object sender, DoWorkEventArgs e){
     //this is the backgroundworker
     while(true){
     receive();
     fireEvent(message);
     }
  }

然后,订阅另一个类中的onNewREceivedEvent并处理入站消息。 onNewReceivedEvent + = processInboundMEsasage();

这是所有伪造的代码和“大脑编译”(对他人的贡献),所以它只适用于演示。没有intellisense,我迷路了。