簡體   English   中英

Spring TCP Socket客戶端未從大型機接收數據

[英]Spring TCP Socket client not receiving data from Mainframe

我正在嘗試使用此示例向使用HEX數據進行通信的大型機發送和接收數據。 所以我的配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

    <context:property-placeholder />

    <!-- Client side -->

    <int:gateway id="gw"
                 service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
                 default-request-channel="input"/>

    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="<ip>"
                                   serializer="CustomSerializerDeserializer"
                                   deserializer="CustomSerializerDeserializer"
                                   port="${availableServerSocket}"
                                   single-use="false"
                                   so-timeout="1800000"
                                   using-nio="false" />

    <bean id="CustomSerializerDeserializer" class="org.springframework.integration.samples.tcpclientserver.CustomSerializerDeserializer" />


    <int:channel id="input" />

    <int-ip:tcp-outbound-gateway id="outGateway"
                                 request-channel="input"
                                 connection-factory="client"
                                 request-timeout="1800000"
                                 reply-timeout="1800000"/>

    <!-- Server side -->

    <int-ip:tcp-connection-factory id="crLfServer"
                                   type="server"
                                   port="${availableServerSocket}"/>

    <int-ip:tcp-inbound-gateway id="gatewayCrLf"
                                connection-factory="crLfServer"
                                request-channel="serverBytes2StringChannel"
                                error-channel="errorChannel"/>

    <int:channel id="toSA" />

    <int:service-activator input-channel="toSA"
                           ref="echoService"
                           method="test"/>

    <bean id="echoService"
          class="org.springframework.integration.samples.tcpclientserver.EchoService" />

    <int:object-to-string-transformer id="serverBytes2String"
                                      input-channel="serverBytes2StringChannel"
                                      output-channel="toSA"/>

</beans>

在我的序列化器/反序列化器中,我具有以下內容:

public void serialize(String input, OutputStream outputStream) throws IOException {
    outputStream.write(buildmsg(input));
    outputStream.flush();
}

public String deserialize(InputStream inputStream) throws IOException {
    String data = "";
    logger.info("inside deserialize");
    DataInputStream dis = new DataInputStream(inputStream);
    int len = dis.readInt();
    if (len > 0) {
        logger.info("len: " + decimaltohex(len));
        logger.info("data: " + data);
        byte[] b = new byte[dis.available()];
        dis.readFully(b);
        data += decimaltohex(len);
        data += byteArrayToHex(b);
    }
    logger.info("full data:" + data);
    return data;
}

public byte[] buildmsg(String body) throws UnsupportedEncodingException {
    String header = "00000000";
    String totMsg = header + body;
    header = massageheader(decimaltohex(totMsg.length() / 2));
    totMsg = header + body;
    logger.info("sending data : " + totMsg);
    return hexStringToByteArray(totMsg);
}

public String decimaltohex(int data) {
    return Integer.toHexString(data);
}

public static String massageheader(String data) {
    String str = String.format("%8s", data).replace(' ', '0').toUpperCase();
    return str;
}

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i + 1), 16));
    }
    return data;
}

public static String byteArrayToHex(byte[] a) {
    StringBuilder sb = new StringBuilder(a.length * 2);
    for (byte b : a) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

輸入本身是一個十六進制字符串(假設是)。 因此,正在發送消息,並且也正確接收了響應,但是沒有將輸出傳遞回Echo服務。 為什么沒有發生? 我的配置有問題嗎?

這不是DEBUG日志,只有INFO; 您的log4j配置一定是錯誤的。

內部反序列化

這只是等待數據的接收線程; 它卡在for循環中,等待大型機關閉連接-我建議您在循環內添加一些調試。

編輯

您的POM中缺少Commons-logging ...

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

或切換到log4j2

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>

並創建一個log4j2配置文件。

Spring Framework 5現在使用其自己的不支持log4j 1.x的commons-logging實現。

如果大型機在響應結束時沒有關閉連接(可能沒有關閉),則讀取循環將永遠不會退出。 循環需要以某種方式知道響應的預期長度或實際長度。

串行器當然也需要進行一些EBCDIC轉換嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM