Reputation: 68
I'm currently working on an ISO 8583 messaging system where I send a message to a server, but after sending the message, the console only prints ISO 8583 message sent to the server. and nothing happens after that. I don't receive any response, and there are no errors or exceptions thrown.
Here is the relevant part of my code:
fun mainSendBkm() {
try {
val messageFactory = MessageFactory<IsoMessage>()
messageFactory.setConfigPath("app/src/main/resources/iso8583.xml")
//messageFactory.useBinaryMessages = true
val clearMessage = messageFactory.newMessage(0x800)
clearMessage.setValue(3, "810000", IsoType.NUMERIC, 6)
clearMessage.setValue(11, "000001", IsoType.NUMERIC, 6)
clearMessage.setValue(12, getLocalTransactionTime(), IsoType.NUMERIC, 6)
clearMessage.setValue(13, getLocalTransactionDate(), IsoType.NUMERIC, 4)
clearMessage.setValue(43, "TRS000000001 TYRABC100110004546 ".padEnd(40), IsoType.ALPHA, 40)
val tcn = generateRandomTCN2()
clearMessage.setValue(63, byteArrayOf(0x01.toByte(), 0x00, 0x08) + tcn, IsoType.BINARY, 11)
val messageBytes = clearMessage.writeData()
println("message :${messageBytes.toHexString()}")
sendToServer(messageBytes)
} catch (e: Exception) {
e.printStackTrace()
}
}
fun generateRandomTCN2(): ByteArray {
val tcn = ByteArray(8)
for (i in tcn.indices) {
tcn[i] = (Math.random() * 255).toInt().toByte()
}
return tcn
}
fun sendToServer(messageBytes: ByteArray) {
val serverAddress = "serverIp"
val serverPort = port
Socket(serverAddress, serverPort).use { socket ->
val outputStream = socket.getOutputStream()
outputStream.write(messageBytes)
outputStream.flush()
println("ISO 8583 message sent to the server.")
receiveFromServer(socket)
}
}
fun receiveFromServer(socket: Socket) {
val inputStream = socket.getInputStream()
val responseBytes = ByteArray(1024)
println("a")
val bytesRead = inputStream.read(responseBytes)
println("b")
println("byteRead :$bytesRead")
val responseFactory = MessageFactory<IsoMessage>()
responseFactory.setConfigPath("app/src/main/resources/iso8583.xml")
val receivedResponse = responseFactory.parseMessage(responseBytes, 0)
println("\nReceived ISO 8583 Response Message:")
println(receivedResponse.debugString())
<?xml version="1.0" encoding="UTF-8"?>
<iso8583>
<message type="0800">
<field num="3" type="NUMERIC" length="6"/>
<field num="11" type="NUMERIC" length="6"/>
<field num="12" type="NUMERIC" length="6"/>
<field num="13" type="NUMERIC" length="4"/>
<field num="43" type="ALPHA" length="40"/>
<field num="63" type="BINARY" length="999"/>
</message>
<parse type ="0810">
<field num="3" type="NUMERIC" length="6"/>
<field num="11" type="NUMERIC" length="6"/>
<field num="12" type="NUMERIC" length="6"/>
<field num="13" type="NUMERIC" length="4"/>
<field num="37" type="BINARY" length="12"/>
<field num="39" type="BINARY" length="2"/>
<field num="43" type="ALPHA" length="40"/>
<field num="63" type="BINARY" length="999"/>
</parse>
</iso8583>
I am currently getting the following error
byteRead : -1
java.text.ParseException: ISO8583 MessageFactory has no parsing guide for message type ffffffd0 [???????????????????
I would be very happy if you answer. Where and what should I do?
Upvotes: 0
Views: 62
Reputation: 1865
First thing is, you are not sending the length bytes to the server, most ISO8583 implementations require you to send the length of the message so they know where the message ends.
You need to understand how the server expects that length and header bytes and send it before the message bytes.
Even after fixing that, you will have to understand the format of the ISOMsg
fields in order to communicate with the server appropriately.
The server may not be responding for many reasons:
For sending the length the right way, you first need to know how the server is expecting them, but let's assume, just for illustration purposes, that it expects two raw bytes with the length.
The code would look something like this:
val outputStream = socket.getOutputStream()
outputStream.write(messageBytes.size >> 8) // send most significant byte
outputStream.write(messageBytes.size) // least significant size
...
Once the size issue is fixed, the server will probably (depending on the server) send a response, but then the work will be just starting.
In order to correctly get the bytes from the server, the length should be read first, and then the rest of the message with readFully
with a byte the size of the length you read, instead of read
. The reason being that the message could be split and arrive in more than one packet, so a call to read could return before the complete message arrive.
You need two things, first get and read the server specs to understand how they are expecting the length, then you will need to work with the other party to understand what is happening on their side.
Upvotes: 0