簡體   English   中英

如何使用C#將Iso8583格式的消息發送到服務器

[英]How Do I Send Message In Iso8583 Format To Server using c#

為了從銀行服務器獲取詳細信息,他們將向我提供IP和端口號。 我正在使用ISO8583消息格式(來自NuGet Manager的OpenIso8583庫)來發送和接收消息。請提供有關如何使用IP地址和端口號將ISO8583消息發送到銀行服務器的解決方案。

公共無效的post(){

        var msg = new Iso8583();
        msg.MessageType = Iso8583.MsgType._0200_TRAN_REQ;

        //Following feilds are mandotary to get balanace info
        msg[Iso8583.Bit._003_PROC_CODE] = "820000";//balanace enquery
        msg[Iso8583.Bit._102_ACCOUNT_ID_1] = "0021210020320000069";//bankid(002)branchid(121)accnumber(0020320000069)           

        //send iso message to server
        var response = NetworkSend("192.32.179.171", 45510, msg);//ipaddress,port,msg

        if (response[Iso8583.Bit._039_RESPONSE_CODE] == "000")
        {
              //success

            // read data of iso message
        }

    }

    private static Iso8583 NetworkSend(string ip, int port, Iso8583 msg)
    {

        // We're going to use a 2 byte header to indicate the message length
        // which is not inclusive of the length of the header
        var msgData = new byte[msg.PackedLength + 2];

        // The two byte header is a base 256 number so we can set the first two bytes in the data 
        // to send like this
        msgData[0] = (byte)(msg.PackedLength % 256);
        msgData[1] = (byte)(msg.PackedLength / 256);

        // Copy the message into msgData
        Array.Copy(msg.ToMsg(), 0, msgData, 2, msg.PackedLength);

        // Now send the message.  We're going to behave like a terminal, which is 
        // connect, send, receive response, disconnect
        var client = new TcpClient();
        var endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
        client.Connect(endPoint);
        var stream = client.GetStream();

        // Send the packed message
        stream.Write(msgData, 0, msgData.Length);

        // Receive the response

        // First we need to get the length of the message out of the socket
        var lengthHeader = new byte[2];
        stream.Read(lengthHeader, 0,2);//this line giving error

        // Work out the length of the incoming message
        var rspLength = lengthHeader[0] * 256 + lengthHeader[1];
        var rspData = new byte[rspLength];

        // Read the bytes off the network stream
        stream.Read(rspData, 0, rspLength);

        // Close the network stream and client
        stream.Close();
        client.Close();

        // Parse the data into an Iso8583 message and return it
        var rspMsg = new Iso8583();

        rspMsg.Unpack(rspData, 0);
        return rspMsg;
    }

您的第一個問題是郵件的包裝。 msgdata [0]和msgdata [1]的值應該交換,否則主機期望的值遠遠超過您發送的值。

至於等待輸入數據的部分,由於數據包碎片,我遇到了同樣的問題。 我要做的是向套接字添加一個OnReceived事件,在此事件中,我的第一次讀取檢查了接收到的數據是否大於1個字節。 如果是這樣,則第一個2個字節就是我的長度指示器,並且我會繼續讀取並追加到緩沖區,直到達到長度指示器指示的總數,或者直到發生超時為止。 我通過遞歸地重新輸入OnReceived事件來完成此操作,直到沒有要讀取的字節為止。

這是一種通過網絡發送ISO消息的方法

public static string Send(Iso8583 msg, string IP, int Port)
{
    var messagebits = msg.ToMsg();
    string result = "";
    try
    {
        TcpClient tcpclnt = new TcpClient();
        tcpclnt.Connect(IP, Port);
        Stream stream = tcpclnt.GetStream();

        ASCIIEncoding asen = new ASCIIEncoding();
        stream.Write(messagebits, 0, messagebits.Length);

        var lengthHeader = new byte[2];
        stream.Read(lengthHeader, 0, 2);

        var rspLength = lengthHeader[0] * 256 + lengthHeader[1];
        var rspData = new byte[rspLength];

        stream.Read(rspData, 0, rspLength);
        tcpclnt.Close();
        Iso8583 msgIso = new Iso8583();
        msgIso.Unpack(rspData, 0);
    }
    catch (Exception) { }
    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM