How to send network request to the host server from iso8583 in C#

215 views Asked by At

I am trying to send the Iso8583 network request to the host server, but it is throwing an error message as "Source array was not long enough" while reading the response.

I have copied this code from the net as I am new to this technology. I have followed some links where this issue was address earlier such as How Do I Send Message In Iso8583 Format To Server using c# but no luck for me. To verify I tried pinging the server and port and it is working and responding.

public void OnPostTryDharam()
{
    System.Net.ServicePointManager.Expect100Continue = false;
    System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient("172.26.100.197", 14401);
                
    var msg = new Iso8583();
    msg.MessageType = Iso8583.MsgType._0800_NWRK_MNG_REQ;
    msg.TransmissionDateTime.SetNow();            

    msg[Iso8583.Bit._011_SYS_TRACE_AUDIT_NUM] = DateTime.Now.ToString("HHmmss");
    msg[Iso8583.Bit._012_LOCAL_TRAN_TIME] = DateTime.Now.ToString("HHmmss");            
    msg[Iso8583.Bit._013_LOCAL_TRAN_DATE] = DateTime.Now.ToString("MMdd");

    // Send a sign on message
    msg[Iso8583.Bit._070_NETWORK_MANAGEMENT_INFORMATION_CODE] = "001";   

    var rsp = Send(msg, "172.26.100.197", 14401);
    var tmessage = "";           
    tmessage = "We are now signed on";
}
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) 
    { 
        // Exception shows here
    }
    
    return result;
}
1

There are 1 answers

4
Gor Grigoryan On

Sending a network request to a host server from ISO8583 in C# involves several steps.

using (var socket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp))
{
   socket.Connect(host, port);
}
var iso8583Message = new Iso8583Message();
// set fields and values of the message

var serializer = new Iso8583Serializer();
var buffer = serializer.Serialize(iso8583Message);

socket.Send(buffer);

var responseBuffer = new byte[2048];
var receivedBytes = socket.Receive(responseBuffer);

var response = serializer.Deserialize(responseBuffer, receivedBytes);

Regarding the error "Source array was not long enough" while reading the response, it could be due to the buffer size that you are using to receive the response is not enough. You may try to increase the buffer size to receive more bytes. Also, make sure that the response buffer is correctly initialized with the correct size, and that the received bytes count is not greater than the buffer size.