简体   繁体   English

如何使用 Java 发送 ISO8583 消息

[英]How to send ISO8583 message using Java

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我正在尝试使用 TLSv1.2 协议通过 SSLSocket 向远程服务器发送 ISO8583 消息,我使用密钥库配置了证书并尝试发送示例 ISO8583 消息:08002220010000800000900000011312115000000180105000003

0800: MTI 0800: MTI
2220010000800000: Binary Hex Encoded Bitmap (Only fields 3, 7, 11, 24, 41 are present) 2220010000800000:二进制十六进制编码 Bitmap(仅存在字段 3、7、11、24、41)
900000: Process code 900000:进程代码
0113121150: Transmission Date&Time 0113121150:传输日期和时间
000001: STAN 000001:斯坦
801: Function code 801: Function码
05000003: Terminal ID 05000003:终端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.然后我将消息转换为一个字节数组并使用套接字 OutputStream 发送它,但服务器没有响应,并且在尝试读取 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.我对 ISO8583 很陌生,所以我不完全知道我做错了什么。

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 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;
    }

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.如果可能的话,我建议使用 WebSockets 进行通信,因为它们已经设置了通信协议的连接方式。

But if you are going to stick with plain sockets:但是,如果您要坚持使用普通的 sockets:

  1. After your write call, call flush();在你的 write 调用之后,调用 flush();

     out.write(bytes); out.flush();
  2. If 1 didn't work, things get trickier.如果 1 不起作用,事情会变得更棘手。 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.我查阅了 ISO 8583 并对您正在尝试做的事情有了更好的了解。 You can ignore my previous suggestion on using WebServer sockets.你可以忽略我之前关于使用 WebServer sockets 的建议。

You are missing the basics.您缺少基础知识。 You can't build ISO messages using string concatenation.您不能使用字符串连接构建 ISO 消息。 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/ https://kodejava.org/how-do-i-pack-an-iso-8583-message/

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. 8583 有点像 xml,它是一种格式描述,但是每个处理器都使用它来构建自己的特定协议,因此您确实需要向您要连接的供应商询问协议文档。

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完成写入后刷新 OutputStream,消息可能仍挂在您的操作系统缓冲区中
  • check the vendor documentation whether you need some sort of framing you may need to append a checksum to the message or...检查供应商文档是否需要某种框架,您可能需要 append 对消息的校验和或...
  • you may need to prepend a length header to the message.您可能需要在消息中添加长度 header 。 8583 was originally built on top of relay protocols where the transport handled message length. 8583 最初建立在传输处理消息长度的中继协议之上。 A lot of parsers haven't caught up yet with the transition to TCP/IP:)许多解析器还没有赶上向 TCP/IP 的过渡:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM