当前位置: 首页 > 知识库问答 >
问题:

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

上官迪
2023-03-14

我正在通过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上下文。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>

因此,在这个repo之后,字符串应该转换为字节[]。

我使用与回购完全相同的转换器,所以我只是在这里复制以节省您的时间:

import java.io.UnsupportedEncodingException;

import org.springframework.core.convert.converter.Converter;

public class ByteArrayToStringConverter implements Converter<byte[], String> {

    private String charSet = "UTF-8";

    public String convert(byte[] bytes) {
        try {
            return new String(bytes, this.charSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("caught excepton in converter");
            return new String(bytes);
        }
    }

    /**
     * @return the charSet
     */
    public String getCharSet() {
        return charSet;
    }

    /**
     * @param charSet the charSet to set
     */
    public void setCharSet(String charSet) {
        this.charSet = charSet;
    }

}
public interface SimpleGateway {
    public String send(Message message);
}

我做了一个自定义序列化程序:

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

}

我的主要职能是:

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

回复信息是

# DC API Test System: microstrategy

似乎我已通过发送消息成功连接到服务器,但服务器无法正确识别该消息。任何有用的建议将不胜感激,谢谢!

更新1:

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

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

}
inside serialize
1550182834431
byte[] in serialize is sessions.list
.

# DC API Test System: microstrategy
2019-02-14 17:21:35.185  INFO 91620 --- [       Thread-1] o.s.i.endpoint.EventDrive

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

更新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);

有什么帮助吗,谢谢?

这是我解决问题之前的更新,管理员已将其删除:

我现在收到了服务器的响应,但出现了错误:

Cannot correlate response - no pending reply for server:15099:49469:0fdce5c4-432f-4ce4-b878-2e08d0e96419
inside serialize
1550189909340
byte[] in serialize is sessions.list
.

GenericMessage [payload=byte[35], headers={ip_tcp_remotePort=15099, ip_connectionId=server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839, ip_localInetAddress=/10.21.66.115, ip_address=217.78.6.17, id=3a6ff696-f12f-6328-da1a-5d613d37a4b2, ip_hostname=server, timestamp=1550189909764}]
2019-02-14 19:18:29.850 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.852 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.852 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway  

主要功能是

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

我试图删除最后一个“\n”

message = new GenericMessage<String>("sessions.list\n.");

它不工作,有超时例外。如何删除这些“无法关联响应”错误?

更新1:

我认为服务器会以几行消息进行响应:

sessions.list
.
response
,status_code,0
,status_message,OK
,time,2019-02-16 00:10:49 (+1000)
sessions
.

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

共有2个答案

苍意智
2023-03-14

终于解决了。我会把我的解决方案贴出来,供别人参考。

我的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"/>
赵同
2023-03-14

如果您不需要回复,您应该使用出站通道适配器而不是网关。

 类似资料:
  • 我正在将我的“普通NIO”(=我直接使用了JDK中的软件包)TCP服务器迁移到Netty 4。 我有向所有客户端发送消息的线程,例如运行状况检查数据包,聊天消息广播,直接聊天消息到单个客户端,...使用我保留在某处集合。 我如何在Netty中做到这一点?在一个Netty处理程序和需要发送消息的线程之间共享一个ChannelGroup是否明智?该通道将如下所示: 在所有线程中,我将简单地执行以下操作

  • 问题内容: 我将实现类似于Facebook通知和此网站的内容(StackOverflow的通知会通知我们是否有人为我们的问题写评论/答案等)。请注意,用户将使用我的应用程序作为网站而不是移动应用程序。 我遇到以下获取结果的答案,但我需要推送结果而不是获取结果。 根据建议,我在实体类中创建了一个简单方法,并向其中添加了@PostPersist,但此方法不起作用,因此基于此答案,我添加了persist

  • 当我提交表单时,我在浏览器控制台中看到“emit”消息,所以我知道表单提交事件正在触发,但我没有在服务器上收到测试消息。客户端或服务器端似乎什么也没发生,“socket.emit”函数似乎什么也没做。 我做错了什么?

  • 我对Android系统是新手。我有一个客户端类,我的主要活动引用。client类将客户端套接字连接到充当服务器的外部设备,但是它从不将我试图发送的消息发送到服务器。我知道这不是连接,因为在创建套接字时,我将setKeepAlive()设置为true,当我试图发送消息时不会引发异常,socket.isconnected()返回true,如果我试图在发送消息之前连接套接字,它会引发“已经连接”的异常。

  • 问题内容: 所以现在,我正在制作一个基于客户端服务器应用程序的多线程。在服务器端,我为接受的每个连接创建了一个线程。 在线程类中,我创建了一种将命令发送到客户端的方法。我只想要的是如何将参数发送到所有正在运行的客户端?为简单起见,我只想使此服务器向所有连接的客户端发送消息。 我已经阅读了这篇文章,并从此链接中找到方法。但是,当我尝试使用自己的代码时,中没有类似的方法。 好的,这是我的服务器和线程示

  • 我有一个Netty客户端和一个Netty服务器,并按照主要教程后,为了有一个EchoClient/服务器,我想让它,使我的客户端发送消息到我的服务器,当他第一次连接到它。 下面是我的的方法,这些方法应该解决这个问题: 但是正如你所看到的,教程使用了一个ByteBuf和一个String似乎不起作用! 下面是我如何在我的方法中显示收到的消息: 但是当为使用并在构造函数中初始化它并发送它时,我的服务器不