Example #1
0
        private void Acceptclient()
        {
            try
            {
                IPAddress   ip     = IPAddress.Any;
                int         myport = int.Parse(ConfigApp.MyPort);
                TcpListener tl     = new TcpListener(ip, myport);
                TcpClient   tc;

                while (true)
                {
                    tl.Start();
                    tc = tl.AcceptTcpClient();
                    string ip1 = tc.Client.RemoteEndPoint.ToString();
                    if (Common.isAllowIP(ip1))
                    {
                        Listen listen = new Listen(tc);
                        Thread thread = new Thread(new ThreadStart(listen.startAccept));
                        thread.Start();
                    }
                    else
                    {
                        LogWrite.WriteLog(ip1, "非法客户端:");
                        tc.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                LogWrite.WriteLog(ex.ToString(), "Socket错误");
                DialogResult dr = MessageBox.Show("创建socket连接失败。", "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (dr == DialogResult.OK)
                {
                    Application.ExitThread();
                    Application.Exit();
                }
            }
        }
Example #2
0
        private static ArrayList getAllowIPlist()
        {
            ArrayList al = new ArrayList();

            try
            {
                string file = System.Windows.Forms.Application.StartupPath + "\\hosts";
                System.IO.FileStream   fs = new System.IO.FileStream(file, System.IO.FileMode.Open);
                System.IO.StreamReader sr = new System.IO.StreamReader(fs, System.Text.Encoding.GetEncoding("GBK"));

                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    else if (line.IndexOf(';') == 0)
                    {
                        continue;
                    }
                    else
                    {
                        al.Add(line.Trim());
                    }
                }

                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                LogWrite.WriteLog(ex.Message, "加载hosts出错");
                al.Add("*.*.*.*");
            }
            return(al);
        }
Example #3
0
        public static bool SLSendData(string sendtxt, string nodeNo, string trantype, ref XmlDocument rec)
        {
            TcpClient sendTcp = new TcpClient();

            byte[] mss         = Encoding.GetEncoding("utf-8").GetBytes(sendtxt);
            int    hostTimeOut = timeOut;
            int    apphostport = int.Parse(MultPara.getHostPort(nodeNo));

            string ip = MultPara.getHostIP(nodeNo);


            sendTcp.SendTimeout = timeOut;

            try
            {
                if (Common.isIP(ip))
                {
                    sendTcp.Connect(IPAddress.Parse(ip), apphostport);
                }
                else
                {
                    sendTcp.Connect(ip, apphostport);
                }
            }
            catch (Exception exc)
            {
                sendTcp.Close();
                LogWrite.WriteLogUTF8(exc.Message, "连接服务器失败,原因(" + trantype + "):", nodeNo);
                return(false);
            }
            NetworkStream ns = sendTcp.GetStream();

            ns.WriteTimeout = timeOut;
            ns.ReadTimeout  = timeOut;
            try
            {
                LogWrite.WriteLogUTF8(sendtxt, "发送核心报文(" + trantype + "):", nodeNo);
                if (ns.CanWrite)
                {
                    int len1 = mss.Length;
                    if (len1 > sendSize)
                    {
                        int a = len1 / sendSize;
                        int b = len1 % sendSize;
                        for (int i = 0; i < a; i++)
                        {
                            ns.Write(mss, i * sendSize, sendSize);
                        }
                        if (b > 0)
                        {
                            ns.Write(mss, len1 - b, b);
                        }
                    }
                    else
                    {
                        ns.Write(mss, 0, len1);
                    }

                    if (ns.CanRead)
                    {
                        byte[] len = new byte[8];

                        ns.Read(len, 0, 8);
                        int L = int.Parse(Encoding.GetEncoding("utf-8").GetString(len));

                        byte[] recbuff = new byte[L];

                        int index = 0;
                        while (index < L)
                        {
                            int ct = ns.Read(recbuff, index, L - index);
                            if (ct <= 0)
                            {
                                break;
                            }
                            index += ct;
                        }
                        ns.Close();
                        sendTcp.Close();
                        string recv = Encoding.GetEncoding("utf-8").GetString(recbuff);
                        LogWrite.WriteLogUTF8(recv, "接收核心报文(" + trantype + "):", nodeNo);
                        try
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(recv);
                            rec = doc;
                            return(true);
                        }
                        catch (Exception ex)
                        {
                            LogWrite.WriteLogUTF8(ex.Message, "解析报文出错(" + trantype + "):", nodeNo);
                            return(false);
                        }
                    }
                    else
                    {
                        LogWrite.WriteLogUTF8("连接不可用", "接收报文错误,原因(" + trantype + "):", nodeNo);
                        ns.Close();
                        sendTcp.Close();

                        return(false);
                    }
                }
                else
                {
                    LogWrite.WriteLogUTF8("连接不可用", "往服务器发数据失败,原因(" + trantype + "):", nodeNo);
                    ns.Close();
                    sendTcp.Close();

                    return(false);
                }
            }
            catch (Exception ex)
            {
                ns.Close();
                sendTcp.Close();
                LogWrite.WriteLogUTF8(ex.Message, "通讯时出现异常(" + trantype + "):", nodeNo);
            }
            return(false);
        }
Example #4
0
        public void startAccept()
        {
            try
            {
                NetworkStream ns    = tcpClient.GetStream();
                int           l1    = 6;
                byte[]        cRead = new byte[l1];

                int it1 = 0;
                while (it1 < l1)
                {
                    int ct1 = ns.Read(cRead, it1, l1 - it1);
                    if (ct1 <= 0)
                    {
                        break;
                    }
                    it1 += ct1;
                }


                string head = System.Text.Encoding.GetEncoding("GBK").GetString(cRead);
                int    len  = int.Parse(head);
                byte[] text = new byte[len];
                int    it   = 0;
                while (it < len)
                {
                    int ct = ns.Read(text, it, len - it);
                    if (ct <= 0)
                    {
                        break;
                    }
                    it += ct;
                }

                string body = Encoding.GetEncoding("GBK").GetString(text);
                sdata.Jysj = DateTime.Now.ToString("HH:mm:ss");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(body);
                XmlNode tranCode  = doc.SelectSingleNode("//ebank//tranCode");
                XmlNode nodeNo    = doc.SelectSingleNode("//ebank//nodeNo");
                string  trandType = ""; //内部交易代码
                string  nodeno    = ""; //机构号
                string  sendEbank = ""; //返回报文
                if (tranCode != null && nodeNo != null)
                {
                    string index = ConfigApp.Lsh;
                    trandType = tranCode.InnerText;
                    nodeno    = nodeNo.InnerText.Replace("|", "");

                    LogWrite.WriteLog(head + body, "接收(" + trandType + ")" + "[" + index + "]", nodeno);



                    ET bm = new ET(doc, trandType, nodeno);
                    sendEbank = bm.Done();

                    //Eticket bm1 = new Eticket(doc, trandType, nodeno);
                    //sendEbank = bm1.Done();

                    if (ConfigApp.Show)//显示交易结果
                    {
                        sdata.Wd    = nodeno;
                        sdata.Wdm   = MultPara.getBankName(nodeno);
                        sdata.Jym   = trandType;
                        sdata.Jysj1 = DateTime.Now.ToString("HH:mm:ss");
                        try
                        {
                            XmlDocument xxd = new XmlDocument();
                            xxd.LoadXml(sendEbank.Substring(6));
                            sdata.Errcode = xxd.SelectSingleNode("//ebank//hostReturnCode").InnerText.Trim();
                            sdata.Errmsg  = xxd.SelectSingleNode("//ebank//hostErrorMessage").InnerText.Trim();
                        }
                        catch
                        {
                            sdata.Errcode = "";
                            sdata.Errmsg  = sendEbank;
                        }
                    }

                    byte[] sendData = Encoding.GetEncoding("GBK").GetBytes(sendEbank);
                    if (ns.CanWrite)
                    {
                        ns.Write(sendData, 0, sendData.Length);
                        LogWrite.WriteLog(sendEbank, "返回(" + trandType + ")" + "[" + index + "]", nodeno);
                    }
                    else
                    {
                        LogWrite.WriteLog("", "返回网银端时通讯异常(" + trandType + ")" + "[" + index + "]", nodeno);
                        sdata.Errcode = "9999";
                        sdata.Errmsg  = "通讯异常";
                    }
                }
                else
                {
                    LogWrite.WriteLog(body, "非法的请求报文:");
                    sdata.Errcode = "9999";
                    sdata.Errmsg  = "无效交易码";
                }
                ns.Close();
                if (ConfigApp.Show)
                {
                    try { Form1.FinalQueue.Enqueue(sdata); }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                LogWrite.WriteLog(ex.ToString(), "处理失败:");
            }
            finally
            {
                tcpClient.Close();
            }
        }
Example #5
0
        public void DD()
        {
            int count = 0;

            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("net");
                dt.Columns.Add("cor");
                dataDic = new DataSet();
                dataDic.Tables.Add(dt);

                string line = null;
                string id   = "";
                string net  = "";
                string cor  = "";



                FileStream   fs = new FileStream(@"DD.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                while (true)
                {
                    line = sr.ReadLine();
                    count++;
                    if (line != null)
                    {
                        line = line.Trim();
                        if (line.Length > 0)
                        {
                            if (line.IndexOf(";") == 0)
                            {
                                continue;
                            }

                            if (line.IndexOf("[") == 0 && line.IndexOf("]") > 0)
                            {
                                id = line.Substring(line.IndexOf('[') + 1, line.IndexOf(']') - (line.IndexOf('[') + 1));

                                continue;
                            }
                            else
                            {
                                int itmp = line.IndexOf(" ");
                                net  = line.Substring(0, itmp);
                                line = line.Substring(itmp).Trim();
                                itmp = line.IndexOf(" ");
                                cor  = line.Substring(0, itmp);

                                DataRow dr = dataDic.Tables[0].NewRow();
                                dr["id"]  = id;
                                dr["net"] = net;
                                dr["cor"] = cor;
                                dataDic.Tables[0].Rows.Add(dr);
                            }
                        }
                        else
                        {
                            id = "";
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                LogWrite.WriteLog("原因:" + ex.Message, "'DD' 初始化失败,行" + count.ToString());
                throw ex;
            }
        }
Example #6
0
        private static string SendData(string s, string nodeNo)
        {
            string recv = "error";

            byte[] body = Encoding.GetEncoding("GBK").GetBytes(s);

            byte[] head = new byte[2];
            head[0] = Convert.ToByte(body.Length >> 8);
            head[1] = Convert.ToByte(body.Length % 256);
            TcpClient sendTcp = new TcpClient();
            //string Restr = "";
            ini        myini       = new ini(filename);
            int        apphostport = Convert.ToInt32(myini.ReadValue(nodeNo, "hostPort").ToString());
            IPAddress  ip          = IPAddress.Parse(myini.ReadValue(nodeNo, "hostIP").ToString());
            IPEndPoint ipe         = new IPEndPoint(ip, apphostport);

            sendTcp.SendBufferSize = 10240;
            sendTcp.SendTimeout    = 10 * 1000;

            try
            {
                sendTcp.Connect(ip, apphostport);
            }
            catch (Exception ex)
            {
                sendTcp.Close();
                LogWrite.WriteLog("原因:" + ex.Message, "[加密机]连接服务器失败", nodeNo);
                return(recv);
            }
            NetworkStream ns = sendTcp.GetStream();

            ns.WriteTimeout = 10 * 1000;
            ns.ReadTimeout  = 10 * 1000;
            try
            {
                if (ns.CanWrite)
                {
                    ns.Write(head, 0, head.Length);
                    ns.Write(body, 0, body.Length);
                    //LogWrite.WriteLog(s, "[加密机]发送数据:", nodeNo);
                    if (ns.CanRead)
                    {
                        byte[] len = new byte[2];
                        ns.Read(len, 0, 2);
                        int    L       = len[0] * 256 + len[1];
                        byte[] recbuff = new byte[L];

                        int index = 0;
                        while (index < L)
                        {
                            int cout = ns.Read(recbuff, index, L - index);
                            if (cout <= 0)
                            {
                                break;
                            }
                            index += cout;
                        }

                        recv = Encoding.GetEncoding("gbk").GetString(recbuff);
                        //LogWrite.WriteLog(recv, "[加密机]接收数据:", nodeNo);
                    }
                }
                else
                {
                    LogWrite.WriteLog("连接不可用", "[加密机]发数据失败:", nodeNo);
                }
            }
            catch (Exception ex)
            {
                LogWrite.WriteLog(ex.Message, "[加密机]通讯故障,原因:", nodeNo);
            }
            finally
            {
                ns.Close();
                sendTcp.Close();
            }
            return(recv);
        }