Spring 集成 tcpClient 未收到消息

Spring integration tcpClient not receive message

我正在开发一个与 ServerSocket 通信的应用程序。 我正在使用 spring 集成的 TCP 客户端连接到服务器以发送和接收消息。 各部分如下截图代码:

服务器:

public void startSocketServer(){

    try (final ServerSocket serverSocket = new ServerSocket(9992)) {
           gl.info("Server is listening on: " + serverSocket.getLocalSocketAddress());

         while (true) {
             final Socket socket = serverSocket.accept();
             gl.info("A new client connected");
             new SocketThread(socket).start();
         }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private class SocketThread extends Thread {
    private final Socket socket;
    private final PrintWriter writer;
    private final BufferedReader reader;


    public SocketThread(Socket socket) throws IOException {
        this.socket = socket;
        InputStream input = socket.getInputStream();
        OutputStream output = socket.getOutputStream();

        reader = new BufferedReader(new InputStreamReader(input));
        writer = new PrintWriter(output, true);
    }
    public void run() {
        try {
            while (true) {
                String inputMessage = reader.readLine();
                if (inputMessage != null) {
                    MessageType messageType = getTypeInstance(inputMessage);

                    if (messageType instanceof LoginMessage loginMessage) {
                        if (isAuthenticated(loginMessage.getUsername(), loginMessage.getPassword())) {
                            gl.info("#### SERVER => User authorized");
                            final String messageBody = createConnectionAckMessage();
                            print(writer, messageBody);
                        } else {
                            print(writer, createRefusalMessage());
                        }
                    } else if (messageType instanceof StartTransferingData startData) {
                        getMessages().forEach(message-> print(writer, message));

                    } else if (messageType instanceof DisconnectionAck disAck) {
                        print(writer, "By then")
                        break;
                    }
                }
            }
            socket.close();
        } catch (IOException ex) {
            gl.info("Server exception: " + ex.getMessage());
        }
    }
    private void print(PrintWriter writer, String msg) {
        writer.print(msg);
        writer.print("\r\n");
    }
}

和客户:

public class CapConfig {
  
  @MessagingGateway(defaultRequestChannel = "toTcp", errorChannel = "errorChannel")
  public interface TcpGateway {
    @Gateway
    void send(String in);
  }

  @Bean
  public MessageChannel toTcp() {
    return new DirectChannel();
  }

  @Bean
  public AbstractClientConnectionFactory clientCF() {
     return Tcp.netClient("localhost", 9992)
            .serializer(TcpCodecs.crlf())
            .deserializer(TcpCodecs.crlf())
            .get();
  }

  @Bean
  public IntegrationFlow tcpOutFlow(AbstractClientConnectionFactory connectionFactory) {
    return IntegrationFlows.from(toTcp())
            .handle(Tcp.outboundAdapter(connectionFactory))
            .get();
  }

  @Bean
  public IntegrationFlow tcpInFlow(AbstractClientConnectionFactory connectionFactory) {
     return IntegrationFlows.from(Tcp.inboundAdapter(connectionFactory))
            .transform(stringTransformer)
            .log()
            //---- Do some other stuffs
            .get();
  }
}

场景如下:

客户端向网关发送用户名和密码,然后服务器接收消息并进行身份验证,如果客户端通过身份验证,则服务器向客户端发送connectionAck消息以表明连接已接受。 然后客户端向服务端发送startData消息开始数据传输。

问题是:

当客户端向服务端发送username&pass,服务端向客户端发送connectionAck时,客户端收不到消息!!!!。

有什么帮助吗? 提前致谢。

我只是用 writer.println(msg) 而不是 writer.print(msg),问题就解决了。

我不知道为什么,但它起作用了。