private void button1_Click(object sender, EventArgs e) { byte[] buf = { 0X68, 0, 0, 0, 1, 0, 0, 0, 1, 0 }; //获得要发送文件的路径 string path = @"D:\HS\18032801\lasttime.xml"; using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int r = fsRead.Read(buffer, 0, buffer.Length); byte[] copy = new byte[r]; List <byte> listSend = new List <byte>(); listSend.AddRange(buf); listSend.Add((byte)(r & 0x00FF)); //低字节 listSend.Add((byte)(r >> 8)); //高字节 Array.Copy(buffer, copy, r); listSend.AddRange(copy); byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验 listSend.AddRange(CRC); listSend.Add(0x16); //将泛型集合转换为数组 byte[] newBuffer = listSend.ToArray(); //获得用户在下拉框中选中的IP地址 string ip = cboUser.SelectedItem.ToString(); lock (AsyncSocketSvr.AsyncSocketUserTokenList.UserEndPoint(ip)) { AsyncSocketSvr.AsyncSocketUserTokenList.UserEndPoint(ip).Send(newBuffer, 0, newBuffer.Length, SocketFlags.None); } } }
public override bool ProcessReceive(byte[] buffer, int offset, int count) //接收异步事件返回的数据,用于对数据进行缓存和分包 { m_activeDT = DateTime.UtcNow; List <byte> DataList = new List <byte>(); DataList.AddRange(buffer.Take(count)); bool result = true; try { while (DataList.Count > 6)//(DataCount > sizeof(int)) { if (DataList[0] == 0x68) { //按照长度分包 int packetLength = (DataList[10] + DataList[11] * 256); // AuxiliaryMethod.byte2ToInt(DataList.ToArray(), 10); //获取包长度 if (DataList.Count > packetLength + 14) { if (DataList[packetLength + 14] == 0x16) { byte[] CRC = AuxiliaryMethod.crc16(DataList.ToArray(), packetLength + 12); //CRC校验 if ((CRC[0] == DataList[packetLength + 12]) && (CRC[1] == DataList[packetLength + 13])) //如果CRC16校验正确,数据接收成功 { result = ProcessPacket(DataList.Take(packetLength + 15).ToArray()); DataList.RemoveRange(0, packetLength + 15); //从缓存中清理 } else { DataList.RemoveRange(0, packetLength + 15); //数据出错从缓存中清理 } } else { DataList.RemoveAt(0); } } else { return(true); } } else { DataList.RemoveAt(0); } }//while结束点 } //TRY catch (Exception e) { //以后要注释掉 MessageBox.Show(e.ToString()); } finally { GC.Collect(); } return(true); }
private void btnSend_Click(object sender, EventArgs e) { //string str = txtInput.Text; // byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); List <byte> listSend = new List <byte>(); byte[] buffer = { 0X68, 0, 0, 0, 1, 0, 0, 0, 1, 90, 3, 0, 1, 1, 1 }; //list.Add(0); listSend.AddRange(buffer); byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验 listSend.AddRange(CRC); listSend.Add(0x16); //将泛型集合转换为数组 byte[] newBuffer = listSend.ToArray(); //获得用户在下拉框中选中的IP地址 string ip = cboUser.SelectedItem.ToString(); lock (AsyncSocketSvr.AsyncSocketUserTokenList.UserEndPoint(ip)) { AsyncSocketSvr.AsyncSocketUserTokenList.UserEndPoint(ip).Send(newBuffer); } }
public bool SendPacket(string str) { #region 发送函数 //Web发送的命令格式为deviceID-文件编号 string[] sArray = str.Split('-'); int device_ID = int.Parse(sArray[0]); int fileCode = int.Parse(sArray[1]); string cmd = "select pathName from DeviceInfo where DeviceID='" + device_ID + "'";//这一步可以不用,直接用变量加路径 string path = DbHelperSQL.Execute(cmd).Trim(); //for (int i = 99; i <= 115; i++) //{ string newPath = ""; //switch (fileCode) //{ // case 0x5A: // newPath = Path.Combine(path, "name.xml"); // break; // case 0: newPath = Path.Combine(path, "tmp.xml"); // break; // case 1: // newPath = Path.Combine(path, "mode1.xml"); // break; // case 2: // newPath = Path.Combine(path, "mode2.xml"); // break; // case 3: // newPath = Path.Combine(path, "mode3.xml"); // break; // case 4: // newPath = Path.Combine(path, "mode4.xml"); // break; // case 5: // newPath = Path.Combine(path, "mode5.xml"); // break; // case 6: // newPath = Path.Combine(path, "mode6.xml"); // break; // case 7: // newPath = Path.Combine(path, "mode7.xml"); // break; // case 8: // newPath = Path.Combine(path, "mode8.xml"); // break; // case 9: // newPath = Path.Combine(path, "mode9.xml"); // break; // case 10: // newPath = Path.Combine(path, "mode10.xml"); // break; // case 11: // newPath = Path.Combine(path, "mode11.xml"); // break; // case 12: // newPath = Path.Combine(path, "mode12.xml"); // break; // case 13: // newPath = Path.Combine(path, "mode13.xml"); // break; // case 14: // newPath = Path.Combine(path, "mode14.xml"); // break; // case 15: // newPath = Path.Combine(path, "mode15.xml"); // break; // case 16: // newPath = Path.Combine(path, "mode16.xml"); // break; // case 17: // newPath = Path.Combine(path, "mode17.xml"); // break; // case 18: // newPath = Path.Combine(path, "mode18.xml"); // break; // case 19: // newPath = Path.Combine(path, "mode19.xml"); // break; // case 20: // newPath = Path.Combine(path, "mode20.xml"); // break; // case 21: // newPath = Path.Combine(path, "mode21.xml"); // break; // case 22: // newPath = Path.Combine(path, "mode22.xml"); // break; // case 23: // newPath = Path.Combine(path, "mode23.xml"); // break; // case 24: // newPath = Path.Combine(path, "mode24.xml"); // break; // default: // break; //} byte[] buf = { 0X68, 0, 0, 0, 0, 0, 0, 0, 0, (byte)fileCode }; using (FileStream fsRead = new FileStream(newPath, FileMode.Open, FileAccess.Read)) { byte[] bf = new byte[1024 * 100]; int r = fsRead.Read(bf, 0, bf.Length); byte[] copy = new byte[r]; List <byte> listSend = new List <byte>(); listSend.AddRange(buf); listSend.Add((byte)(r & 0x00FF)); //低字节 listSend.Add((byte)(r >> 8)); //高字节 Array.Copy(bf, copy, r); listSend.AddRange(copy); byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验 listSend.AddRange(CRC); listSend.Add(0x16); AsyncSocketUserToken UserToken = m_asyncSocketServer.AsyncSocketUserTokenList.UseKey(device_ID); lock (UserToken) { UserToken.ConnectSocket.Send(listSend.ToArray()); // UserToken.SendEventArgs.ConnectSocket.Send(listSend.ToArray()); } listSend.Clear(); } //} string m_message = "send Success"; m_asyncSocketUserToken.ConnectSocket.Send(PackData(m_message).ToArray()); Thread.Sleep(20); return(true); #endregion }
public void SendThreadStart() { DataTable dt = new DataTable(); string hs_sql, cmd, path, newPath; int device_ID; byte[] buf = { 0X68, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; while (true) { #region //if (m_asyncSocketServer.Redis.ListLength("commands") > 0) //{ // #region // try // { // string commandStr = m_asyncSocketServer.Redis.ListRightPop<string>("commands"); // SendOption sendOption = JsonConvert.DeserializeObject<SendOption>(commandStr); // int gateWay = Convert.ToInt32(sendOption.Gateway); // if (m_asyncSocketServer.AsyncSocketUserTokenList.ContainKey(gateWay)) // { // byte[] gateway_number = AuxiliaryMethod.intTo4Bytes(gateWay); // byte[] devices_number = AuxiliaryMethod.intTo4Bytes(Convert.ToInt32(sendOption.Devices)); // byte[] commandType = AuxiliaryMethod.StringToBytes(sendOption.Order); // //string str = sendOption.Command; // byte[] commandvalues = AuxiliaryMethod.StringToBytes(sendOption.Command); // if (commandvalues.Length < 9) // { // byte[] dataLength = { 0, 8 }; // byte startFrame = 0x68; //拼凑帧头 // byte endFrame = 0x16; //帧尾 // listSend.Add(startFrame); //全部添加listSend // listSend.AddRange(gateway_number); // listSend.AddRange(devices_number); // listSend.AddRange(commandType); // listSend.AddRange(dataLength); // listSend.AddRange(commandvalues); // byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验, // listSend.AddRange(CRC); //将校验数据添加listSend // listSend.Add(endFrame); // } // else // { // byte[] dataLength = { 0, 0x0A }; // byte startFrame = 0x68; //拼凑帧头 // byte endFrame = 0x16; //帧尾 // listSend.Add(startFrame); //全部添加listSend // listSend.AddRange(gateway_number); // listSend.AddRange(devices_number); // listSend.AddRange(commandType); // listSend.AddRange(dataLength); // listSend.AddRange(commandvalues); // byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验, // listSend.AddRange(CRC); //将校验数据添加listSend // listSend.Add(endFrame); // } // #endregion // AsyncSocketUserToken UserToken = m_asyncSocketServer.AsyncSocketUserTokenList.UseKey(gateWay); // lock (UserToken) // { // UserToken.ConnectSocket.Send(listSend.ToArray()); // } // listSend.Clear(); // Thread.Sleep(50); // } // } // catch // { // } //} //else //{ // Thread.Sleep(50); //} #endregion hs_sql = "select * from SendTable"; dt = DbHelperSQL.OpenTable(hs_sql); if (dt.Rows.Count > 0) { #region hs_sql = "Truncate table SendTable";//清除表中所有行,保留表结构、与delete类似比delete速度快,而且效率高,使用的系统和事务日志资源少 DbHelperSQL.ExecuteSql(hs_sql); for (int i = 0; i < dt.Rows.Count; i++) { try { device_ID = Convert.ToInt32(dt.Rows[i][2]); cmd = "select pathName from DeviceInfo where DeviceID='" + device_ID + "'";//这一步可以不用,直接用变量加路径 path = DbHelperSQL.Execute(cmd).Trim(); newPath = Path.Combine(path, "tmp.xml"); using (FileStream fsRead = new FileStream(newPath, FileMode.Open, FileAccess.Read)) { byte[] bf = new byte[1024 * 200];//声明一个数组存储xml文件数据最大200k int r = fsRead.Read(bf, 0, bf.Length); byte[] copy = new byte[r]; List <byte> listSend = new List <byte>(); listSend.AddRange(buf); listSend.Add((byte)(r & 0x00FF)); //低字节 listSend.Add((byte)(r >> 8)); //高字节 Array.Copy(bf, copy, r); listSend.AddRange(copy); byte[] CRC = AuxiliaryMethod.crc16(listSend.ToArray(), listSend.Count);//调用校验 listSend.AddRange(CRC); listSend.Add(0x16); AsyncSocketUserToken UserToken = m_asyncSocketServer.AsyncSocketUserTokenList.UseKey(device_ID); lock (UserToken) { UserToken.ConnectSocket.Send(listSend.ToArray()); } listSend.Clear(); //hs_sql = "UPDATE HistSendTable SET SendResult='Send Success!' WHERE SendTime = '" + dt.Rows[i][3].ToString().Trim() + "' "; //DbHelperSQL.ExecuteSql(hs_sql); } } catch (Exception e) { hs_sql = "UPDATE HistSendTable SET SendResult='" + e.ToString() + "' WHERE SendTime = '" + dt.Rows[i][3].ToString().Trim() + "' "; DbHelperSQL.ExecuteSql(hs_sql); } #endregion } } else { Thread.Sleep(50); } } }