2

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;
    }
a2800276
  • 3,272
  • 22
  • 33

3 Answers3

0

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.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Thank you so much for the quick answer. Sadly I tried the first solution but it didn't work, still no data came from the server but thanks anyway. – Moez Baccouche Jan 13 '22 at 13:54
0

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/

bhanuja
  • 16
  • The bitmap is included in the hex string, this should be the problem. He's translating the literal hex-encoded bytes of the message. – a2800276 Jan 17 '22 at 09:52
0

Your code looks ok. What sort of backend are you connecting to? 8583 is a bit like xml, it's a format description, but every processor uses it to build their own specific protocol from it, so you really need to ask the vendor you are connecting to for protocol documentation.

Some things that may be the matter:

  • flush the OutputStream when you are done writing, the message may still be hanging in your OS buffer
  • check the vendor documentation whether you need some sort of framing you may need to append a checksum to the message or ...
  • you may need to prepend a length header to the message. 8583 was originally built on top of relay protocols where the transport handled message length. A lot of parsers haven't caught up yet with the transition to TCP/IP :)
a2800276
  • 3,272
  • 22
  • 33