码迷,mamicode.com
首页 > 编程语言 > 详细

线程带参数的Udp接收

时间:2016-02-16 18:29:54      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

//work wk = new work(ReadUdpDate);
ParameterizedThreadStart parmThre = new ParameterizedThreadStart(ReadUdpDate);
Thread read = new Thread(parmThre);
read.Start(ipAddress);  

 

 

private void ReadUdpDate(object IP)
{
string message = "";
try
{


while (true)
{
//if (remoteIpep==null)
//{
// remoteIpep = new IPEndPoint(IPAddress.Parse("192.168.6.32"), 26); // 接受的IP地址和端口号
//}
if (ReadUdpClient==null)
{
return;
}
int buff = ReadUdpClient.Client.Available;
if (buff > 0)
{

byte[] bytRecv = ReadUdpClient.Receive(ref remoteIpep);
if (bytRecv.Length > 0)
{
message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);
if (bytRecv.Length==35)
{
string sch = message.Substring(0, 20);
sch = sch.Substring(0, 10);
int countNum = int.Parse(message.Substring(20, 2));
string jiIP = remoteIpep.Address.ToString();

if (qzz.select_diepian(sch, jiIP))
{
qzz.update_diepian(sch, countNum, jiIP);
}
else
{
qzz.insert_diepian(sch, countNum, jiIP);
}
}
else
{
//如果接收到的信息时OK 关闭开关
if (message.ToUpper() == "OK")///&& IP == remoteIpep.Address.ToString())
{

}
else if (message.ToUpper() == "NG")/// && IP == remoteIpep.Address.ToString())
{
if (sendbytes!=null)
{
sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpep);
}
else
{
//MessageBox.Show("还未发送给PLC数据!");
}

}
}


}

}

}

}
catch (Exception ex)
{
MessageBox.Show( ex.Message, "系统提示!");
}
}

线程带参数的Udp接收

标签:

原文地址:http://www.cnblogs.com/VictorBlog/p/5193123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!