我的服务器一直在运行以从代理接收数据,但代理在一次运行后就会关闭。
甚至我的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秒的时间间隔连续向服务器发送消息。能够第一次实现。知道我哪里错了吗?
答案 0 :(得分:0)
问题是你正在创建一个调用方法的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方法中删除所有与计时器相关的代码。