为了从银行服务器获取详细信息,他们将向我提供IP和PORT号码。我正在使用ISO8583消息格式(来自NuGet Manager的OpenIso8583库)发送和接收消息。请提供有关如何使用IP地址和端口号将ISO8583消息发送到银行服务器的解决方案。
public void 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;
}
答案 0 :(得分:1)
您的第一个问题在于邮件的打包。应交换msgdata [0]和msgdata [1]的值,否则主机的期望远远超过您发送的内容。
至于等待传入数据的部分,由于数据包碎片,我遇到了同样的问题。我所做的是将一个OnReceived事件添加到我的套接字,在这个事件中,我的第一次读取检查收到的数据是否超过1个字节。如果是这样,前2个字节是我的长度指示器,我继续读取并附加到缓冲区,直到我达到长度指示器指示的总数,或者直到发生超时。我通过递归重新输入OnReceived事件来做到这一点,直到没有剩余的字节要读。
答案 1 :(得分:0)
这是一种通过网络发送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;
}