Home > Net >  How to send ISO8583 message using Java
How to send ISO8583 message using Java

Time:01-16

I'am really sorry if this is a duplicate question but I tried many answers in other threads and none of them worked for me.

I'am trying to send an ISO8583 message to a remote server through an SSLSocket using the TLSv1.2 protocol, I configured the certificate with the Keystore and attempted to send a sample ISO8583 message : 08002220010000800000900000011312115000000180105000003

0800: MTI
2220010000800000: Binary Hex Encoded Bitmap (Only fields 3, 7, 11, 24, 41 are present)
900000: Process code
0113121150: Transmission Date&Time
000001: STAN
801: Function code
05000003: Terminal ID

I then converted the message to an array of bytes and sent it with the socket OutputStream but no response came from the server and it is freezing when attempting to read the InputStream.

For the purpose of this question, I chose to test a manually-set sample message and not use any packaging method.

I'm very new to the ISO8583 so I don't exactly know what I'm doing wrong.

Here is the code I tried so far and thank you so much to who ever that will try to help me.

Thread thread = new Thread(() -> {
                try {

                    X509TrustManager[] tmm;
                    KeyStore ks  = KeyStore.getInstance("BKS");
                    InputStream is = getResources().openRawResource(R.raw.tunrootca2);
                    ks.load(is, KEY_PASSWORD.toCharArray());

                    tmm=tm(ks);
                    SSLContext ctx = SSLContext.getInstance("TLSv1.2");
                    ctx.init(null, tmm, null);

                    SSLSocketFactory SocketFactory = ctx.getSocketFactory();
                    SSLSocket socket = (SSLSocket) SocketFactory
                            .createSocket(REMOTE_ENDPOINT, REMOTE_ENDPOINT_PORT);


                    String sampleMessage = "080022200100008000009000000113120000000180105000003";
                    byte[] bytesMessage = sampleMessage.getBytes(StandardCharsets.UTF_16LE);

                    byte[] bytes = packData(bytesMessage);

                    OutputStream out = socket.getOutputStream();
                    out.write(bytes);

                    byte[] buffer = new byte[256];

                    InputStream in = socket.getInputStream();
                    int read;
                    while((read = in.read(buffer)) != -1) {
                        String output = new String(buffer, 0, read);
                        Log.v("SOCKET_OUTPUT", output);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            thread.start();

        });

PackData Function

static byte[] packData(byte[] data) {
        int len = data.length;
        byte buf[] = new byte[len   2];
        buf[0] = (byte) (len >> 8 & 255);
        buf[1] = (byte) (len & 255);
        System.arraycopy(data, 0, buf, 2, len);
        return buf;
    }

CodePudding user response:

Settuing up socket communication can be tricky. If possible, I'd advise using WebSockets for communication, as they're already set up with how the communications protocols connect.

But if you are going to stick with plain sockets:

  1. After your write call, call flush();

         out.write(bytes);
         out.flush();
    
  2. If 1 didn't work, things get trickier. Since you don't control the server side part of this, it's hard to know what you need to send them in order to get them to send you something back. You might try sending a newline character. But otherwise, there's a mismatch between what you are sending and what the server on the other end is expecting

--- Edit ---

I looked up ISO 8583 and have a better idea of what you are trying to do. You can ignore my previous suggestion on using WebServer sockets.

CodePudding user response:

You are missing the basics. You can't build ISO messages using string concatenation. You have to set correct bitmaps for those enable fields. Maybe try to follow the below sample. It will guide you with the basics.

https://kodejava.org/how-do-i-pack-an-iso-8583-message/

  •  Tags:  
  • Related