Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TCP客户端向外部服务器发送消息,不需要回复消息_Tcp_Spring Integration_Tcpclient - Fatal编程技术网

TCP客户端向外部服务器发送消息,不需要回复消息

TCP客户端向外部服务器发送消息,不需要回复消息,tcp,spring-integration,tcpclient,Tcp,Spring Integration,Tcpclient,我正在通过Spring集成设置一个TCP客户端,以字符串作为有效负载发送消息,不期望返回。可能序列化程序/反序列化程序工作不正常?对不起,我正在学习Spring集成 我可以通过oepnssl连接到外部TCP服务器: --- # DC API Test System: microstrategy sessions.list. . response ,status_code,1 ,status_message,Unrecognised operation ,time,2019-02-15 07:08

我正在通过Spring集成设置一个TCP客户端,以字符串作为有效负载发送消息,不期望返回。可能序列化程序/反序列化程序工作不正常?对不起,我正在学习Spring集成

我可以通过oepnssl连接到外部TCP服务器:

---
# DC API Test System: microstrategy
sessions.list.
.
response
,status_code,1
,status_message,Unrecognised operation
,time,2019-02-15 07:08:08 (+1000)
.
我需要发送的命令是sessions.list\n。\n

现在,我构建了一个tcp客户端,试图连接到服务器:

spring-context.xml

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-5.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.1.xsd">

<bean id="integrationConversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.microstrategy.example.ByteArrayToStringConverter"/>
            </list>
        </property>
</bean> 


<bean id="customeSerilizerDeserlizer" class="com.microstrategy.example.CustomSerializerDeserializer" />

<int:gateway service-interface="com.microstrategy.example.SimpleGateway"
    default-request-channel="output"
    default-reply-channel="reply"/>

<int:channel id="output"/>
<int:channel id="reply" datatype="java.lang.String"/>

<int-ip:tcp-connection-factory
    id="clientFactory"
    type="client"
    host="server"
    port="15099"
    serializer="customeSerilizerDeserlizer"
    single-use="true"
    so-timeout="10000"/>

<int-ip:tcp-outbound-gateway 
    request-channel="output"
    reply-channel="reply"
    connection-factory="clientFactory"
    request-timeout="10000"
    reply-timeout="10000"/>

</beans>
我制作了一个自定义序列化程序:

package com.microstrategy.example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;

public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

}
public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        System.out.println("inside serialize");
        System.out.println(System.currentTimeMillis());


        String string = new String(bytes);
        System.out.println("byte[] in serialize is " + string);
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        System.out.println("inside deserialize");
        System.out.println(System.currentTimeMillis());

        return null;
    }

}
我的主要职能是:

Message<String> message = MessageBuilder.withPayload("sessions.list").build();
String replyMessage = simpleGateway.send(message);
Message<String> message2 = MessageBuilder.withPayload(".").build();
String replyMessage2 = simpleGateway.send(message2);
System.out.println(replyMessage2);
似乎我已通过发送消息成功连接到服务器,但服务器无法正确识别该消息。任何有用的建议将不胜感激,谢谢

更新1:

我将输出添加到序列化程序:

package com.microstrategy.example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;

public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

}
public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        System.out.println("inside serialize");
        System.out.println(System.currentTimeMillis());


        String string = new String(bytes);
        System.out.println("byte[] in serialize is " + string);
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        System.out.println("inside deserialize");
        System.out.println(System.currentTimeMillis());

        return null;
    }

}
输出显示字节[]似乎正确,那么为什么服务器没有按预期返回

更新2:我更改了主函数,因为框架将在每条消息的末尾添加\n。是这样吗

输出是

inside serialize
1550184564485
byte[] in serialize is sessions.list
inside serialize
1550184565003
byte[] in serialize is .
2019-02-14 17:49:35.013 ERROR 91740 --- [           main] o.s.i.ip.tcp.TcpOutboundGateway          : Tcp Gateway exception

org.springframework.integration.MessageTimeoutException: Timed out waiting for response
没有回应

更新3:我可以通过发送一条空消息来打开连接。但为什么其他信息不起作用呢

Message<String> message = MessageBuilder.withPayload("").build();
String replyMessage = simpleGateway.send(message);
System.out.println(replyMessage);
主要功能是

message = new GenericMessage<String>("sessions.list\n.\n");
replyMessage = simpleGateway.send(message);
System.out.println(replyMessage);

我需要捕获所有响应,直到..

如果您不希望收到回复,则应使用出站通道适配器而不是网关。

最终解决了问题。我将发布我的解决方案供其他人参考

我的TCP客户端发送多行命令,以.,结尾,并期望多行响应也以.,结尾。从外部服务器。所以我需要编写自定义序列化程序和反序列化程序。默认的CRLF序列化程序/反序列化程序不符合我的情况

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;

public class CustomSerializerDeserializer implements Serializer<String>, Deserializer<String> {

    @Override
    public String deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        StringBuilder builder = new StringBuilder();
        int c;
        while (true) {
            c = inputStream.read();
            builder.append((char)c);
            if ((char)c == '.') {
                break;
            }
        }

        return builder.toString();
    }

    @Override
    public void serialize(String object, OutputStream outputStream) throws IOException {
        // TODO Auto-generated method stub
        outputStream.write(object.getBytes());
        outputStream.flush();   
    }
}
xml配置是

<int-ip:tcp-connection-factory
    id="clientFactory"
    type="client"
    host="server"
    port="15099"
    ssl-context-support="sslContext"
    serializer="customeSerilizerDeserlizer"
    deserializer="customeSerilizerDeserlizer"
    single-use="true"
    so-timeout="10000"/>
message = new GenericMessage<String>("sessions.list\n.\n");
replyMessage = simpleGateway.send(message);
System.out.println(replyMessage);
message = new GenericMessage<String>("sessions.list\n.");
sessions.list
.
response
,status_code,0
,status_message,OK
,time,2019-02-16 00:10:49 (+1000)
sessions
.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;

public class CustomSerializerDeserializer implements Serializer<String>, Deserializer<String> {

    @Override
    public String deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        StringBuilder builder = new StringBuilder();
        int c;
        while (true) {
            c = inputStream.read();
            builder.append((char)c);
            if ((char)c == '.') {
                break;
            }
        }

        return builder.toString();
    }

    @Override
    public void serialize(String object, OutputStream outputStream) throws IOException {
        // TODO Auto-generated method stub
        outputStream.write(object.getBytes());
        outputStream.flush();   
    }
}
<int-ip:tcp-connection-factory
    id="clientFactory"
    type="client"
    host="server"
    port="15099"
    ssl-context-support="sslContext"
    serializer="customeSerilizerDeserlizer"
    deserializer="customeSerilizerDeserlizer"
    single-use="true"
    so-timeout="10000"/>