10.9w+
社区成员
/// <summary>
/// 程序开始
/// </summary>
public static void StartMain()
{
try
{
ByteQueue queue = new ByteQueue();
int port = 4001;
string host = "192.168.0.81";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipe);
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"))
{
string repStr = data.Replace("-", " ");
Regex regex = new Regex("07 81 08.{24}");
MatchCollection matches = regex.Matches(repStr, 0);
//判断是否是统计数据
if (matches.Count > 0)
{
List<T_RadarStatistics> statisticsList = HandleStatisticsData(repStr, host);
foreach (var item in statisticsList)
{
StringBuilder builder = new StringBuilder();
//builder.AppendFormat("\n 地址:{0}:{1} ", ip, port);
builder.Append("\n 时间: " + DateTime.Now);
builder.Append("\n 测量线: " + item.Measureline);
builder.Append("\n 车道: " + item.StatisLlane);
builder.Append("\n 平均车速: " + item.SumAvspeed);
builder.Append("\n 时间占有率: " + item.SumOccupancy);
builder.Append("\n 车流量: " + item.SumVolume);
builder.Append("\n 车头实距: " + item.HeadWay);
builder.Append("\n----------------------------------------------------------");
Console.WriteLine(builder.ToString());
}
}
}
}
}
}
catch (ArgumentNullException e)
{
//MessageBox.Show("ArgumentNullException: {0}", e.Message);
}
catch (SocketException e)
{
//MessageBox.Show("SocketException: {0}", e.Message);
}
}
foreach(string host in clientHostList)
{
var svr = host;
ThreadPool.QueueUserWorkItem(h=> StartMain(svr, port));
}
public static void StartMain(string host, int port)
{
.....
同时检查一遍、方法里不应该乱用其它“共享变量”。
然后在主程序中写var clientHostList = new string[ ]{"192.168.0.81"};
var port = 12345;
foreach(string host in clientHostList)
{
var h = host;
ThreadPool.QueueUserWorkItem(h=> StartMain(h, port));
}
static void Main(string[] args)
{
var clientHostList = new string[] { "192.168.0.81", "192.168.0.87" };
var port = 4001;
foreach (string host in clientHostList)
{
var svr = host;
ThreadPool.QueueUserWorkItem(h => StartMain(svr, port));
}
}
/// <summary>
/// 程序开始
/// </summary>
public static void StartMain(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);
if (socket.Connected)
{
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"))
{
string repStr = data.Replace("-", " ");
Regex regex = new Regex("07 81 08.{24}");
MatchCollection matches = regex.Matches(repStr, 0);
//判断是否是统计数据
if (matches.Count > 0)
{
List<T_RadarStatistics> statisticsList = HandleStatisticsData(repStr, host);
foreach (var item in statisticsList)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("\n 地址:{0}:{1} ", ip, port);
builder.Append("\n 时间: " + DateTime.Now);
builder.Append("\n 测量线: " + item.Measureline);
builder.Append("\n 车道: " + item.StatisLlane);
builder.Append("\n 平均车速: " + item.SumAvspeed);
builder.Append("\n 时间占有率: " + item.SumOccupancy);
builder.Append("\n 车流量: " + item.SumVolume);
builder.Append("\n 车头实距: " + item.HeadWay);
builder.Append("\n----------------------------------------------------------");
Console.WriteLine(builder.ToString());
}
}
}
}
}
}
catch (ArgumentNullException e)
{
//MessageBox.Show("ArgumentNullException: {0}", e.Message);
}
catch (SocketException e)
{
//MessageBox.Show("SocketException: {0}", e.Message);
}
}
ByteQueue queue = new ByteQueue();
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
byte[] buffer = new byte[1024];
Socket socket;
连接很简单
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(txt_ip.Text, int.Parse(txt_port.Text));
然后丢一个这个就好了
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
当然实现的方式是这样的.
void ReceiveMessage(IAsyncResult ar)
{
var socket = ar.AsyncState as Socket;
var length = socket.EndReceive(ar);
byte[] reallData = new byte[length];
Array.Copy(buffer, reallData, length);
var abc=string.Join("-", reallData.Select(d => d.ToString("X2")).ToArray());
//处理
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
}