예제 #1
0
 /// <summary>
 /// 发送字符串信息到服务端的方法,单个客户端发送消息
 /// </summary>
 /// <param name="sendMsg">发送的字符串信息</param>
 private void ClientSendMsg(string sendMsg)
 {
     //将输入的内容字符串转换为机器可以识别的字节数组
     byte[] arrClientSendMsg = ExplainUtils.HexSpaceStringToByteArray(sendMsg);
     //调用客户端套接字发送字节数组
     socketClient.Send(arrClientSendMsg);
     //将发送的信息追加到聊天内容文本框中
     //txtMsg.AppendText("天之涯:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n");
     LogHelper.WriteLog("天之涯:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n");
 }
예제 #2
0
 /// <summary>
 /// 接收服务端发来信息的方法,单个套接字监听
 /// </summary>
 private void RecMsg()
 {
     while (true) //持续监听服务端发来的消息
     {
         //定义一个1K的内存缓冲区 用于临时性存储接收到的信息
         byte[] arrRecMsg = new byte[1 * 1024];
         //将客户端套接字接收到的数据存入内存缓冲区, 并获取其长度
         int length = socketClient.Receive(arrRecMsg);
         //将套接字获取到的字节数组转换为人可以看懂的字符串
         string strRecMsg = ExplainUtils.convertStrMsg(arrRecMsg);
         //将发送的信息追加到聊天内容文本框中
         //txtMsg.AppendText("So-flash:" + GetCurrentTime() + "\r\n" + strRecMsg + "\r\n");
         LogHelper.WriteLog("So-flash:" + GetCurrentTime() + "\r\n" + strRecMsg + "\r\n");
     }
 }
예제 #3
0
 public static int ParseIntFromBytes(byte[] data, int startIndex, int length, int defaultVal)
 {
     try
     {
         // 字节数大于4,从起始索引开始向后处理4个字节,其余超出部分丢弃
         int    len = length > 4 ? 4 : length;
         byte[] tmp = new byte[len];
         Buffer.BlockCopy(data, startIndex, tmp, 0, len);
         return(ExplainUtils.byteToInteger(tmp));
     }
     catch (Exception e)
     {
         return(defaultVal);
     }
 }
예제 #4
0
 /// <summary>
 /// 发送字符串信息到服务端的方法,多个客户端发送同一条消息
 /// </summary>
 /// <param name="sendMsg">发送的字符串信息</param>
 private void multiClientSendMsg(string sendMsg)
 {
     if (socketClientList.Count > 0)
     {
         for (int i = 0; i < socketClientList.Count; i++)
         {
             //将输入的内容字符串转换为机器可以识别的字节数组
             arrClientSendMsg = ExplainUtils.HexSpaceStringToByteArray(sendMsg);
             //调用客户端套接字发送字节数组
             socketClientList[i].Send(arrClientSendMsg);
             count++;
             //将发送的信息追加到聊天内容文本框中
             //txtMsg.AppendText("天之涯:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n");
             LogHelper.WriteLog("天之涯:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n");
             LogHelper.WriteLog("发送消息条数:" + count + "\r\n");
         }
     }
 }
예제 #5
0
        //组装应答消息
        public static byte[] rtnRespMsg(int msgBodyProps, string phone, int flowId)
        {
            //7E
            //8100            消息ID
            //0004            消息体属性
            //018512345678    手机号
            //0015            消息流水号
            //0015            应答流水号
            //04              结果(00成功, 01车辆已被注册, 02数据库中无该车辆, 03终端已被注册, 04数据库中无该终端)  无车辆与无终端有什么区别 ?
            //313C             鉴权码
            //7E

            MemoryStream ms         = new MemoryStream();
            List <byte>  byteSource = new List <byte>();

            // 1. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byte[] bt1 = ExplainUtils.integerTo1Bytes(pkg_delimiter);
            // 2. 消息ID word(16)
            //ms.Write(ExplainUtils.integerTo2Bytes(cmd_terminal_register_resp), 0, 2);
            byte[] bt2 = ExplainUtils.integerTo2Bytes(cmd_terminal_register_resp);

            // 3. 终端手机号 bcd[6]
            //ms.Write(ExplainUtils.string2Bcd(phone), 0, 6);
            byte[] bt3 = ExplainUtils.string2Bcd(phone);
            // 4. 消息流水号 word(16),按发送顺序从 0 开始循环累加
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt4 = ExplainUtils.integerTo2Bytes(flowId);
            // 5. 应答流水号
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt5 = ExplainUtils.integerTo2Bytes(flowId);
            // 6. 成功
            //ms.Write(ExplainUtils.integerTo1Bytes(0), 0, 1);
            byte[] bt6 = ExplainUtils.integerTo1Bytes(0);
            // 7. 鉴权码
            //ms.Write(System.Text.Encoding.GetEncoding(string_encoding).GetBytes(replyToken), 0, replyToken.Length);
            byte[] bt7 = System.Text.Encoding.GetEncoding(string_encoding).GetBytes(replyToken);
            //8.消息体属性
            byte[] bt8 = ExplainUtils.integerTo2Bytes(bt7.Length + 1 + 2);

            byteSource.AddRange(bt1);
            byteSource.AddRange(bt2);
            byteSource.AddRange(bt8);
            byteSource.AddRange(bt3);
            byteSource.AddRange(bt4);
            byteSource.AddRange(bt5);
            byteSource.AddRange(bt6);
            byteSource.AddRange(bt7);
            // 9. BA 校验码
            // 校验码
            int checkSum = ExplainUtils.getCheckSum4JT808(byteSource.ToArray(), 1, (int)(byteSource.Count));

            //ms.Write(ExplainUtils.integerTo1Bytes(checkSum), 0, 1);
            byte[] bt9 = ExplainUtils.integerTo1Bytes(checkSum);
            // 11. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byteSource.AddRange(bt9);
            byteSource.AddRange(bt1);

            // 转义
            return(ExplainUtils.DoEscape4Send(byteSource.ToArray(), 1, byteSource.ToArray().Length - 1));
        }