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); }
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(); } }