示例#1
0
        public static void GetAllProtocolIP()
        {
            string strSql;

            try
            {
                tVehInfo nVehInfo = null;
                System.Data.OleDb.OleDbCommand    GetProTypeCommand = null;
                System.Data.OleDb.OleDbDataReader GetProTypeReader  = null;
                strSql                        = "select distinct IpAddress,type,TaxiNo from vehicle";
                GetProTypeCommand             = myConn.CreateCommand();
                GetProTypeCommand.CommandText = strSql;
                GetProTypeReader              = GetProTypeCommand.ExecuteReader();
                CarInfo_Hash                  = new Hashtable();
                while (GetProTypeReader.Read())
                {
                    nVehInfo           = new tVehInfo();
                    nVehInfo.ipaddress = GetProTypeReader["IpAddress"].ToString();
                    if (GetProTypeReader["type"].ToString().ToUpper() == "TCP")
                    {
                        nVehInfo.isendtype = 1;
                    }
                    else
                    {
                        nVehInfo.isendtype = 0;
                    }
                    if (CarInfo_Hash.ContainsKey((string)GetProTypeReader["IpAddress"]))
                    {
                        CarInfo_Hash[(string)GetProTypeReader["IpAddress"]] = nVehInfo;
                    }
                    else
                    {
                        CarInfo_Hash.Add((string)GetProTypeReader["IpAddress"], nVehInfo);
                    }
                }
                GetProTypeReader.Close();
                GetProTypeReader = null;
                GetProTypeCommand.Dispose();
            }
            catch (Exception ce)
            {
                GprsServer.WriteErrLog("GetAllProtocolIP", ce.Message.ToString() + ce.StackTrace.ToString());
                if (myConn.State.ToString() == "Closed")
                {
                    Conndb();
                }
                return;
            }
        }
        private void cmdSet_Click(object sender, System.EventArgs e)
        {
            try
            {
                GprsServer.DbServer = txtServer.Text.Trim();
                GprsServer.WritePrivateProfileString("DbConn", "DbServer", txtServer.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.DbName = txtDbName.Text.Trim();
                GprsServer.WritePrivateProfileString("DbConn", "DbName", txtDbName.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.UserId = txtUserName.Text.Trim();
                GprsServer.WritePrivateProfileString("DbConn", "UserId", txtUserName.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.PassWord = txtPassWord.Text.Trim();
                GprsServer.WritePrivateProfileString("DbConn", "PassWord", txtPassWord.Text.Trim(), "SysIni\\SysIni.ini");

                GprsServer.CdmaPort = Convert.ToInt16(txtTcpPort.Text.Trim());
                GprsServer.WritePrivateProfileString("PortIni", "CdmaPort", txtTcpPort.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.sCompanyName = txtName.Text.Trim();
                GprsServer.WritePrivateProfileString("Company", "Name", txtName.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.RemoteIp = txtCenterIP.Text.Trim();
                GprsServer.WritePrivateProfileString("PortIni", "TcpAddress", txtCenterIP.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.RemotePort = Convert.ToInt16(txtCenterPort.Text.Trim());
                GprsServer.WritePrivateProfileString("PortIni", "TcpPort", txtCenterPort.Text.Trim(), "SysIni\\SysIni.ini");

                GprsServer.LocaPort = Convert.ToInt16(txtPort.Text.Trim());
                GprsServer.WritePrivateProfileString("PortIni", "UdpPort", txtPort.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.LocaIP = textBox1.Text.Trim();
                GprsServer.WritePrivateProfileString("PortIni", "BindIp1", textBox1.Text.Trim(), "SysIni\\SysIni.ini");

                GprsServer.LocaIP2 = textBox2.Text.Trim();
                GprsServer.WritePrivateProfileString("PortIni", "BindIp2", textBox2.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.LocaPort2 = Convert.ToInt16(textBox3.Text.Trim());
                GprsServer.WritePrivateProfileString("PortIni", "UdpPort2", textBox3.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.AgainCount = Convert.ToInt16(txtCount.Text.Trim());
                GprsServer.WritePrivateProfileString("Instruction", "AgainCount", txtCount.Text.Trim(), "SysIni\\SysIni.ini");
                GprsServer.strTerminal = txtMobile.Text.Trim();
                GprsServer.WritePrivateProfileString("Show", "Mobile", txtMobile.Text.Trim(), "SysIni\\SysIni.ini");
                this.Close();
            }
            catch
            {
                MessageBox.Show("设置错误,请填写正确的参数值。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
示例#3
0
        //接收CDMA数据线程
        public void ProcessService()
        {
            byte[] buff = new byte[1];
            int    recv = 0;

            byte[] Tbuff;
            byte[] AllBuff;
            int    iLenght         = 0;
            int    iIndex          = 0;
            int    iLen            = 0;
            int    iXorValue       = 0;
            string CarIpAddress    = "";
            string IporInstruction = "";
            string strFile         = "";

            byte[]   RecvAffirmBuff = new byte[] { 0x29, 0x29, 0x21, 0x00, 0x05, 0, 0, 0, 0, 0x0D }; //回应终端数组
            EndPoint remote         = client.RemoteEndPoint;                                         //设置远程客户终节点

            while (true)
            {
                try
                {
                    if (client.Connected)
                    {
                        try
                        {
                            buff = new byte[8192];
                            recv = 0;
                            recv = client.Receive(buff);                          //读取数据内容

                            if (recv == 0)
                            {
                                //GprsServer.WriteErrLog("TcpThread_"+System.Threading.Thread.CurrentThread.Name,"接收TCP终端数据错误!");
                                System.Threading.Thread.Sleep(1);
                                continue;
                            }
                        }
                        catch (SocketException se)
                        {
                            if (se.ErrorCode == 10054)
                            {
                                client.Shutdown(SocketShutdown.Both);
                                client.Close();
                                GprsServer.WriteErrLog("TcpThread_" + System.Threading.Thread.CurrentThread.Name, se.Message.ToString() + ";" + se.StackTrace.ToString());
                                break;
                            }
                            else
                            {
                                System.Threading.Thread.Sleep(1);
                                continue;
                            }
                        }
                        catch (Exception ce)
                        {
                            client.Shutdown(SocketShutdown.Both);
                            client.Close();
                            GprsServer.WriteErrLog("TcpThread_" + System.Threading.Thread.CurrentThread.Name, ce.Message.ToString() + ";" + ce.StackTrace.ToString());
                            break;
                        }

                        AllBuff = new byte[recv + TcpBuff.Length];
                        System.Array.Copy(TcpBuff, 0, AllBuff, 0, TcpBuff.Length);
                        System.Array.Copy(buff, 0, AllBuff, TcpBuff.Length, recv);
//						for (iIndex = 1; iIndex <=TcpBuff.Length; iIndex++)
//						{
//							AllBuff[iIndex-1]=TcpBuff[iIndex-1];
//						}
//
//						for (iIndex = 1; iIndex <=recv; iIndex++)
//						{
//							AllBuff[TcpBuff.Length+iIndex-1]=buff[iIndex-1];
//						}

                        for (iIndex = 1; iIndex <= AllBuff.Length; iIndex++)
                        {
                            iLenght = AllBuff.Length - iIndex + 1;
                            if (iLenght < 6)                          //检测数据包长度
                            {
                                //不完整,则将指令保存
                                if (iLenght > 0)
                                {
                                    TcpBuff = new byte[iLenght];
                                    for (iLen = 1; iLen <= iLenght; iLen++)
                                    {
                                        TcpBuff[iLen - 1] = AllBuff[iLen + iIndex - 2];
                                    }
                                }
                                break;
                            }
                            else
                            {
                                //检测当前指令是否是完整的指令,查找数据包头
                                if (AllBuff[iIndex - 1] == 0x29 & AllBuff[iIndex] == 0x29)
                                {
                                    if ((AllBuff.Length - iIndex) >= (AllBuff[iIndex + 2] * 256 + AllBuff[iIndex + 3] + 4))
                                    {
                                        //检测当前指令是否是完整的指令
                                        if ((AllBuff[AllBuff[iIndex + 2] * 256 + AllBuff[iIndex + 3] + iIndex + 3]) == 0x0D)
                                        {
                                            //在接收的数据中获取单条完整的指令数据
                                            Tbuff = new byte[AllBuff[iIndex + 2] * 256 + AllBuff[iIndex + 3] + 5];
                                            for (iLen = 1; iLen <= Tbuff.Length; iLen++)
                                            {
                                                Tbuff[iLen - 1] = AllBuff[iLen + iIndex - 2];
                                            }
                                            iXorValue = GprsServer.Get_CheckXor(ref Tbuff, Tbuff.Length - 2);
                                            if (iXorValue != Tbuff[Tbuff.Length - 2])
                                            {
                                                //校验不合格,继续查找合法指令数据
                                                continue;
                                            }
                                            else
                                            {
                                                //获取车载终端手机号
                                                CarIpAddress = Tbuff[5] + "." + Tbuff[6] + "." + Tbuff[7] + "." + Tbuff[8];

                                                if (GprsServer.bTsFlag)
                                                {
                                                    if (GprsServer.strTerminal.Trim() != "")
                                                    {
                                                        if (GprsServer.strTerminal.Trim() == CarIpAddress)
                                                        {
                                                            string BuffToStr = "";
                                                            for (int i = 0; i < Tbuff.Length; i++)
                                                            {
                                                                BuffToStr += Tbuff[i].ToString("X2") + " ";
                                                            }
                                                            ShowUserMsg("上行TCP", BuffToStr, 0);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        string BuffToStr = "";
                                                        for (int i = 0; i < Tbuff.Length; i++)
                                                        {
                                                            BuffToStr += Tbuff[i].ToString("X2") + " ";
                                                        }
                                                        ShowUserMsg("上行TCP", BuffToStr, 0);
                                                    }
                                                }

                                                //-----------------检测系统哈希表是否包含此终端数据---------------------\\
                                                if (GprsServer.CarID_TcpIP_Hash.ContainsKey(CarIpAddress))
                                                {
                                                    GprsServer.CarID_TcpIP_Hash[CarIpAddress] = remote;                                                  //有更新
                                                }
                                                else
                                                {
                                                    GprsServer.CarID_TcpIP_Hash.Add(CarIpAddress, remote);                                                   //没有添加
                                                }

                                                //--------------------将数据转发到中心处理程序-----------------\\
                                                if (GprsServer.TcpSocket.Connected)
                                                {
                                                    try
                                                    {
                                                        GprsServer.TcpSocket.Send(Tbuff, 0, Tbuff.Length, SocketFlags.None);
                                                        if (GprsServer.bTsFlag)
                                                        {
                                                            if (GprsServer.strTerminal.Trim() != "")
                                                            {
                                                                if (GprsServer.strTerminal.Trim() == CarIpAddress)
                                                                {
                                                                    ShowUserMsg("系统信息", "TCP车载数据成功转发到网络中心处理程序!", 1);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                ShowUserMsg("系统信息", "TCP车载数据成功转发到网络中心处理程序!", 1);
                                                            }
                                                        }
                                                    }
                                                    catch
                                                    {
                                                        if (GprsServer.bTsFlag)
                                                        {
                                                            if (GprsServer.strTerminal.Trim() != "")
                                                            {
                                                                if (GprsServer.strTerminal.Trim() == CarIpAddress)
                                                                {
                                                                    ShowUserMsg("系统信息", "TCP车载数据转发到网络中心处理程序失败!", 3);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                ShowUserMsg("系统信息", "TCP车载数据转发到网络中心处理程序失败!", 3);
                                                            }
                                                        }
                                                    }
                                                }
                                                iIndex  = iIndex + Tbuff.Length - 1;
                                                TcpBuff = new byte[1];
                                            }
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        if ((AllBuff[iIndex + 2] * 256 + AllBuff[iIndex + 3] + 4) > 1024)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            //不完整,则将指令保存
                                            if (iLenght > 0)
                                            {
                                                TcpBuff = new byte[iLenght];
                                                for (iLen = 1; iLen <= iLenght; iLen++)
                                                {
                                                    TcpBuff[iLen - 1] = AllBuff[iLen + iIndex - 2];
                                                }
                                                break;
                                            }
                                            else
                                            {
                                                TcpBuff = new byte[1];
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }                            //检测数据包长度
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                catch
                {
                    //
                }
            }
        }