public delegate void ShowMessageMethod2(TcpClient tc); private void ShowMessage2(TcpClient tc) { String IP地址 = ((IPEndPoint)tc.Client.RemoteEndPoint).Address.ToString(); String 端口 = ((IPEndPoint)tc.Client.RemoteEndPoint).Port.ToString(); String 事件 = "无事件"; String 备注 = "无备注"; String 内容 = "无内容"; NetworkStream stm = tc.GetStream(); byte[] readBuf = new byte[maxBufferSize]; int realByteNumber = stm.Read(readBuf, 0, maxBufferSize); //显示收到的数据 内容 = (StrHexHelp.byteToHexStr(readBuf).Substring(0,realByteNumber * 2)); byte[] writeBuf1; byte[] writeBuf2; //总召唤命令 /* * 长度是18 * 第七位是64 * 第9位6是动作 7是确认 A是结束 */ if ((realByteNumber == 18) && (readBuf[7] == 0X64) &&(readBuf[9] == 0X06)) { 事件 = "总召唤命令"; 备注 = "终端收到总召唤命令(终端1)"; this.系统日志TableAdapter.Insert(IP地址, 端口, System.DateTime.Now.ToString(), 事件, 备注, 内容); 事件 = "总召唤确认帧及结束帧"; 备注 = "终端返回总召唤确认帧(终端2)"; writeBuf1 = new byte[36]; for (int i = 0; i < 18; i++) writeBuf1[i] = readBuf[i]; writeBuf1[4] = 0X80;//4控制域 writeBuf1[9] = 0X07;//9传送原因 for (int i = 18; i < 36; i++) writeBuf1[i] = readBuf[i-18]; writeBuf1[22] = 0X80;//4控制域 writeBuf1[27] = 0X0A;//A传送原因 内容 = (StrHexHelp.byteToHexStr(writeBuf1)); this.系统日志TableAdapter.Insert(IP地址, 端口, System.DateTime.Now.ToString(), 事件, 备注, 内容); writeBuf2 = new byte[6] { 1, 2, 3, 4, 5, 6 }; stm.Write(writeBuf2, 0, 6); //只有这一行起作用 stm.Write(writeBuf1, 0, 36); //这一行无效(并且字节不会被覆盖,就显示短的那一行) goto GameOver; } GameOver: stm.Close(); tc.Close(); this.系统日志TableAdapter.Fill(this.系统日志DataSet.系统日志); dataGridView2.ClearSelection(); dataGridView2.Rows[0].Selected = true; }