timeerelapsedevent不工作在UDP代理c#

本文关键字:代理 UDP 工作 timeerelapsedevent | 更新日期: 2023-09-27 18:11:40

我的服务器正在连续运行以接收来自Agent的数据,但是Agent在一次运行后被关闭。甚至我的timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);也不能正常工作。

代理代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.Threading;
namespace HeartBeatAgent
{
public class Agent
{
    static string clientdata = "IP1 Version 1";
    static UdpClient UdpAgent = new UdpClient();
    static System.Timers.Timer timer = new System.Timers.Timer();
    static int ip = 127001;
    static int port = 6060;
    static IPEndPoint receivePoint;

    static void Main(string[] args)
    {
        UdpAgent = new UdpClient(port);
        receivePoint = new IPEndPoint(new IPAddress(ip), port);
        Thread startClient = new Thread(new ThreadStart(startAgent));
        //Start the Thread
        startClient.Start();
    }
    public static void startAgent()
    {
        Console.WriteLine("This is Agent");
        //Send Data
        System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
        string sendString = clientdata.ToString();
        byte[] sendMsg = encode.GetBytes(sendString);
        //Send to Server to corresponding port of server
        UdpAgent.Send(sendMsg, sendMsg.Length, "localhost", 6767);
        Console.WriteLine("Msg sent to server 4m agent");

        //Receive Data from Server
        byte[] recMsg = UdpAgent.Receive(ref receivePoint);
        System.Text.ASCIIEncoding receive = new System.Text.ASCIIEncoding();
        //Split it up
        string[] temp = receive.GetString(recMsg).Split(new Char[] { ' ' });
        Console.WriteLine(temp[2] + temp[1] + temp[0]);
        **timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);**
        timer.Interval = 10000;
        timer.Start();
    }
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        startAgent();
    }
}
}
服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace HeartBeatServer
{
public class Server
{
    public static void Main(string[] args)
    {
        UdpClient UdpServer = new UdpClient(6767);
        int count = 0;
        while (true)
        {
            try
            {
                Dictionary<string, int> agentCount = new Dictionary<string, int>();
                Console.WriteLine("This is server");
                //Define a Receive point
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                //Receive Data from Agent
                byte[] recData = UdpServer.Receive(ref RemoteIpEndPoint);
                System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
                //Split it up by space
                string[] temp = encode.GetString(recData).Split(new Char[] { ' ' });
                if (agentCount.ContainsKey(temp[0]))
                {
                    var coun = agentCount[temp[0]];
                    agentCount.Add(temp[0], coun++);
                    Console.WriteLine(coun);
                }
                else
                {
                    agentCount.Add(temp[0], count);
                    Console.WriteLine(temp[0], count);
                    //Re-send the Data to Agent
                    byte[] sendData = encode.GetBytes(System.DateTime.Now.ToString());
                    UdpServer.Send(sendData, sendData.Length, "localhost", 6060);
                    Console.WriteLine("Reply Message Sent to Agent 4m Server");
                }

                Console.WriteLine(temp[0] + temp[1] + temp[2]);
                continue;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
}

我需要以5-10秒的时间间隔连续向服务器发送消息。只能实现第一次。知道我哪里出错了吗?

timeerelapsedevent不工作在UDP代理c#

问题是您正在创建一个Thread,它调用一个方法,一旦该方法结束,Thread将停止执行。不要使用线程,将Main方法替换为:

static void Main(string[] args)
{
    UdpAgent = new UdpClient(port);
    receivePoint = new IPEndPoint(new IPAddress(ip), port); 
    timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    timer.Interval = 10000;
    timer.Start();
}

并从startAgent方法中删除所有与计时器相关的代码