110,567
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
StarMain();
}
public static void StarMain()
{
foreach (Address address in Common.GetAddress())
{
ThreadPool.QueueUserWorkItem(h => SocketClient(address.IP, address.Port));
Thread.Sleep(10);
}
Console.ReadLine();
}
/// <summary>
/// 程序开始
/// </summary>
public static void SocketClient(string host, int port)
{
try
{
ByteQueue queue = new ByteQueue();
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
Console.WriteLine(ip + " Connected......");
while (true)
{
//建立缓冲区
byte[] recByte = new byte[4096];
int bytes = socket.Receive(recByte, recByte.Length, 0);
//分多次接收
byte[] reallData = new byte[bytes];
Array.Copy(recByte, reallData, reallData.Length);
queue.Enqueue(reallData);
//while可处理同时接收到多个包 防止一帧数据不是完整的包
while (queue.Find())
{
byte[] readBuffer = queue.Dequeue();
//解析雷达数据 包含平均速度/占有率/车流量
string data = BitConverter.ToString(readBuffer);
if (data.StartsWith("FF-FF-FF-FF-CA-CB-CC-CD") && data.EndsWith("EA-EB-EC-ED"))
{
GetData(data, host, port, DateTime.Now);
}
else if (data.StartsWith("0A 0B 0C 0D") && data.EndsWith("EA EB EC ED"))//历史数据
{
Regex regex = new Regex("0A 0B 0C 0D.{21}");
MatchCollection matches = regex.Matches(data, 0);
if (matches.Count > 0)
{
foreach (Match nextmach in matches)
{
string history = nextmach.Value;
string[] strTime = history.Split(' ');
string year = strTime[0] + strTime[1];
string moth = strTime[2];
string day = strTime[3];
string hh = strTime[4];
string mm = strTime[5];
string ss = strTime[6];
DateTime time = Convert.ToDateTime(year + "-" + moth + "-" + day + " " + hh + ":" + mm + ":" + ss);
GetData(data, host, port, time);
}
}
}
else
{
Console.WriteLine(data);
}
}
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e.Message);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e.Message);
System.Timers.Timer t = new System.Timers.Timer(1000); //实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;
t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
}
}
public static void theout(object source, System.Timers.ElapsedEventArgs e)
{
StarMain();
}
//建立缓冲区
byte[] recByte = new byte[4096];
int bytes = socket.Receive(recByte, recByte.Length, 0);
//分多次接收
byte[] reallData = new byte[bytes];
Array.Copy(recByte, reallData, reallData.Length);
queue.Enqueue(reallData);
//while可处理同时接收到多个包 防止一帧数据不是完整的包
while (queue.Find())
{
byte[] readBuffer = queue.Dequeue();
//解析雷达数据 包含平均速度/占有率/车流量
string data = BitConverter.ToString(readBuffer);
if (data.StartsWith("FF-FF-FF-FF-CA-CB-CC-CD") && data.EndsWith("EA-EB-EC-ED"))
{
GetData(data, host, port, DateTime.Now);
}
else if (data.StartsWith("0A 0B 0C 0D") && data.EndsWith("EA EB EC ED"))//历史数据
{
Regex regex = new Regex("0A 0B 0C 0D.{21}");
MatchCollection matches = regex.Matches(data, 0);
if (matches.Count > 0)
{
foreach (Match nextmach in matches)
{
string history = nextmach.Value;
string[] strTime = history.Split(' ');
string year = strTime[0] + strTime[1];
string moth = strTime[2];
string day = strTime[3];
string hh = strTime[4];
string mm = strTime[5];
string ss = strTime[6];
DateTime time = Convert.ToDateTime(year + "-" + moth + "-" + day + " " + hh + ":" + mm + ":" + ss);
GetData(data, host, port, time);
}
}
}
else
{
Console.WriteLine(data);
}
}