public void Led_XianShiGngXin(int i, ListView lisW1) { DateTime dt = DateTime.Now; string command = " "; string id = ""; string mc = ""; string zt = ""; string wd = ""; string Bot = ""; string ManJi = ""; string ShiTin = ""; string YinGen = ""; string RfBin = ""; int n, x, y; try { if (i >= 1 && i <= 8) { Client sdt = new Client(); if (lisW1.Items[i].SubItems[2].Text == "在线") { //----------------------------------------------- id = lisW1.Items[i].SubItems[0].Text; n = Convert.ToInt32(id) - 2; id = "" + n; mc = lisW1.Items[i].SubItems[3].Text; if (mc.Length > 4) { mc = mc.Substring(0, 4); } Bot = lisW1.Items[i].SubItems[6].Text; ManJi = lisW1.Items[i].SubItems[12].Text; ShiTin = lisW1.Items[i].SubItems[11].Text; YinGen = lisW1.Items[i].SubItems[8].Text; RfBin = lisW1.Items[i].SubItems[9].Text; wd = lisW1.Items[i].SubItems[5].Text; if (ManJi.IndexOf("正") != -1) { zt = zt + "1"; } else { zt = zt + "0"; } if (ShiTin.IndexOf("正") != -1) { zt = zt + "1"; } else { zt = zt + "0"; } //------------------------------------------------- if (YinGen.IndexOf("正") != -1) { zt = zt + "1"; } else { zt = zt + "0"; } if (RfBin.IndexOf("正") != -1) { zt = zt + "1"; } else { zt = zt + "0"; } if (Bot != "") { Bot = Bot.Remove(0, Bot.Length - 2); if (wd.Length >= 4) { wd = "??"; x = 0; y = 1; } else { x = Convert.ToInt32(Bot); y = Convert.ToInt32(wd); } } else { x = 0; y = 1; wd = "??"; } if (x > y) { zt = zt + "1"; } else { zt = zt + "0"; } command = "+ID-" + id + "-MC-" + mc + "-ZT-" + zt + "-WD-" + wd + " 时间[" + dt + "]\r\n"; byte[] bytearray = Encoding.GetEncoding("GBK").GetBytes(command.Trim());//转码为Byte数组(GBK) //------------------------------------------------------------------------- // MessageBox.Show("ID[" + i + "]名称-" + mc + "-[" + command+"]"); if (lisW1.Items[0].SubItems[2].Text == "在线") { string Ip = lisW1.Items[0].SubItems[1].Text; //sdt.BHUF = ""; sdt.send(Ip, bytearray); } } else { id = lisW1.Items[i].SubItems[0].Text; n = Convert.ToInt32(id) - 2; id = "" + n; mc = lisW1.Items[i].SubItems[3].Text; if (mc.Length > 4) { mc = mc.Substring(0, 4); } int A = int.Parse(id); //MessageBox.Show("ID"+A+mc); sdt.FaiSong_Led(A+1, mc, 0);//发出断线命令 } } } catch (Exception ex) { MessageBox.Show("ID[" + i + "]ERR[" + ex + "]"); } }
/// <summary> /// 向服务器发送信息 /// </summary> private void MsgToServer() { List<string> ListIp = new List<string>(); Client sender = new Client(); string strMsg = "+查询主机\r\n";//指令 byte[] data = Encoding.GetEncoding("GBK").GetBytes(strMsg.Trim());//转码为Byte数组(GBK) //获取当前在线的服务器集合,存储在ListIp中 for (int i = 0; i < listView1.Items.Count;i++ ) { if (listView1.Items[i].SubItems[2].Text=="在线") { ListIp.Add(listView1.Items[i].SubItems[1].Text); } } //遍历在线集合,发送指令数组data foreach(string IpNode in ListIp){ sender.send(IpNode, data); } }
/// <summary> /// 接收消息 /// </summary> /// <param name="socketClientPara"></param> private void RecMsg(object socketClientPara) { Socket socketClient = socketClientPara as Socket; string ip = socketClient.RemoteEndPoint.ToString().Substring(0, socketClient.RemoteEndPoint.ToString().IndexOf(":")); while (true) { //定义一个接受用的缓存区(100M字节数组) //byte[] arrMsgRec = new byte[1024 * 1024 * 100]; //将接收到的数据存入arrMsgRec数组,并返回真正接受到的数据的长度 if (socketClient.Connected) { try { //因为终端每次发送文件的最大缓冲区是512字节,所以每次接收也是定义为512字节 byte[] buffer = new byte[512]; int size = 0; long len = 0; //string fileSavePath = @"Receivefile";//获得用户保存文件的路径 //if (!Directory.Exists(fileSavePath)) //{ // Directory.CreateDirectory(fileSavePath); //} //string fileName = fileSavePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".doc"; ////创建文件流,然后让文件流来根据路径创建一个文件 //FileStream fs = new FileStream(fileName, FileMode.Create); ////从终端不停的接受数据,然后写入文件里面,只到接受到的数据为0为止,则中断连接 string command = ""; DateTime oTimeBegin = DateTime.Now; while ((size = socketClient.Receive(buffer, 0, buffer.Length, SocketFlags.None)) > 0) { // fs.Write(buffer, 0, size); command = Encoding.GetEncoding("GBK").GetString(buffer, 0, size); len += size; MessageBox.Show(command); Client cle = new Client(); cle.send("", Encoding.GetEncoding("GBK").GetBytes(command.Trim())); } //DateTime oTimeEnd = DateTime.Now; //TimeSpan oTime = oTimeEnd.Subtract(oTimeBegin); //fs.Flush(); dict.Remove(socketClient.RemoteEndPoint.ToString().Substring(0, socketClient.RemoteEndPoint.ToString().IndexOf(":"))); //listbOnline.Items.Remove(socketClient.RemoteEndPoint.ToString()); socketClient.Close(); } catch { dict.Remove(socketClient.RemoteEndPoint.ToString().Substring(0, socketClient.RemoteEndPoint.ToString().IndexOf(":"))); //listbOnline.Items.Remove(socketClient.RemoteEndPoint.ToString()); string[] str = new string[]{ "客户端掉线", socketClient.RemoteEndPoint.ToString().Substring(0, socketClient.RemoteEndPoint.ToString().IndexOf(":")) }; uishow.ShwMsgforView(list, str); break; } } else { dict.Remove(ip); //listbOnline.Items.Remove(socketClient.RemoteEndPoint.ToString()); string[] str = new string[]{ "客户端掉线", ip }; uishow.ShwMsgforView(list, str); break; } } }