fmdaboville
fmdaboville

Reputation: 1771

TCP Server in python sending files to TCP Client with Spring integration

I need to implement a TCP Client with Spring Integration, using annotation, without xml conf.

The TCP Server has to send files, and i have to use Spring Integration to handle them, and print them (for now). So, i make a TCP Server with python, but there is no importance for this. Code :

import socket as s

host = ''
port = 2303

co = s.socket(s.AF_INET, s.SOCK_STREAM)
co.bind((host, port))
co.listen(5)
print("Server is listening on port {}".format(port))

conn, addr = co.accept()
print('Connected by', addr)

while True:
    try:
        data = conn.recv(1024)

        if not data: break

        print("Client Says: " + data)
        conn.sendall("Server Says:hi")

    except s.error:
        print("Error Occured.")
        break

print("Closing connection")
conn.close()

And for the client here is the code using spring integration :

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpInboundGateway;
import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory;
import org.springframework.integration.transformer.ObjectToStringTransformer;
import org.springframework.messaging.MessageChannel;


@Configuration
public class TCPInputChannel {

    private static final Logger LOGGER = Logger.getLogger(TCPInputChannel.class);

    @Bean
    TcpNetClientConnectionFactory clientConnectionFactory() {
        LOGGER.info("create TcpNetClientConnectionFactory");
        TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory("localhost", 2303);
        cf.setSingleUse(false);
        cf.setSoTimeout(10000);
        return cf;
    }

    @Bean
    TcpInboundGateway gateway() {
        LOGGER.info("create TcpInboundGateway");
        TcpInboundGateway g = new TcpInboundGateway();
        g.setConnectionFactory(clientConnectionFactory());
        g.setClientMode(true);
        g.setRetryInterval(1000);
        g.setRequestChannel(input());
        return g;
    }

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

    @ServiceActivator(inputChannel = "input", outputChannel = "respString")
    ObjectToStringTransformer stringTransformer() {
        LOGGER.info("create ObjectToStringTransformer");
        ObjectToStringTransformer st = new ObjectToStringTransformer();
        return st;
    }

    @ServiceActivator(inputChannel = "respString")
    public String receive(String recv) {
        LOGGER.info("Recv: " + recv);
        return "echo";
    }

}

When I run it, the server can make a connection, but he can't send messages, and the client don't print anything.

Upvotes: 2

Views: 251

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

The AbstractConnectionFactory uses:

/**
 * Reads data in an InputStream to a byte[]; data must be terminated by \r\n
 * (not included in resulting byte[]).
 * Writes a byte[] to an OutputStream and adds \r\n.
 *
 * @author Gary Russell
 * @since 2.0
 */
public class ByteArrayCrLfSerializer extends AbstractPooledBufferByteArraySerializer {

By default. So, your server should ensure \r\n in the end of package it sends.

Or you can select any appropriate deserializer for your purpose: https://docs.spring.io/spring-integration/reference/html/ip.html#connection-factories

Upvotes: 2

Related Questions