Spring集成TCP-IP客户端实现

时间:2018-07-16 10:56:44

标签: spring sockets tcp ip integration

我正在尝试使用Spring集成实现tcp-ip客户端。我的服务器是普通的Java套接字应用程序。我正在尝试使用Spring Integration Client与服务器进行通信。我能够将数据发送到服务器,但是当我从套接字服务器返回响应时,则无法在客户端上获取它并获得套接字关闭异常。我不确定哪里做错了。请帮助我解决问题或提出最佳解决方案(我们需要在Spring集成中开发客户端)。

Spring integration configuration:
<?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="com.tenx.tsysproxy.service.TsysGateway"
                 default-request-channel="input"/>


    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="localhost"
                                   port="1234"
                                   single-use="true"
                                   so-timeout="10000"
    serializer="connectionSerializeDeserialize"
    deserializer="connectionSerializeDeserialize"/>

  <bean id="connectionSerializeDeserialize" class="org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer"/>


  <int:channel id="input"/>

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

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

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

  <bean id="serverReply" class="com.tenx.tsysproxy.service.ServerService"/>

    <int:transformer id="errorHandler"
                     input-channel="errorChannel"
                     expression="payload.failedMessage.payload + ':' + payload.cause.message"/>

</beans>

Java套接字服务器代码:

try {
      SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
      sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);


      while (true) {
        try {
          System.out.println("Waiting for client on port " + sslserversocket.getLocalPort() + "...");
          SSLSocket server = (SSLSocket) sslserversocket.accept();
          System.out.println("Just connected to " + server.getRemoteSocketAddress());

          Scanner sc = new Scanner(server.getInputStream());

          String op = sc.next();

          System.out.println("Input data : " + op);

          DataOutputStream out = new DataOutputStream(server.getOutputStream());
          out.writeBytes(op + "===");
          //out.writeUTF("returning " + op);
          out.flush();
          //server.close();
        } catch (SocketTimeoutException s) {
          System.out.println("Socket timed out!");
          break;
        } catch (IOException e) {
          e.printStackTrace();
          break;
        }
      }

输出:

Waiting for client on port 1234...
Just connected to /127.0.0.1:52987
java.net.SocketException: Connection closed by remote host
Input data : testdata
    at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1565)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:71)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
    at com.tenx.tsysproxy.server.SSLServer.startSSLServer(SSLServer.java:63)
    at com.tenx.tsysproxy.server.SSLServer.main(SSLServer.java:82)

Error:
09:54:59.153 [pool-1-thread-1] ERROR org.springframework.integration.ip.tcp.connection.TcpNetConnection - Error accessing soTimeout
java.net.SocketException: Socket Closed
    at java.net.AbstractPlainSocketImpl.getOption(AbstractPlainSocketImpl.java:279)
    at java.net.Socket.getSoTimeout(Socket.java:1158)
    at sun.security.ssl.BaseSSLSocketImpl.getSoTimeout(BaseSSLSocketImpl.java:448)
    at org.springframework.integration.ip.tcp.connection.TcpNetConnection.handleReadException(TcpNetConnection.java:221)
    at org.springframework.integration.ip.tcp.connection.TcpNetConnection.run(TcpNetConnection.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
09:54:59.153 [main] ERROR org.springframework.i

0 个答案:

没有答案