2017-09-19 82 views
2

我需要實現一個TCP客戶端與Spring集成,使用註釋,沒有XML配置。TCP服務器在python發送文件到TCP客戶端與Spring集成

TCP服務器必須發送文件,我必須使用Spring集成來處理它們,並打印它們(現在)。所以,我用python製作一個TCP服務器,但是對此沒有任何重要性。代碼:

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

而對於這裏的客戶是使用Spring集成代碼:

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

} 

當我運行它,服務器可以進行連接,但他無法發送消息,以及客戶端不打印任何東西。

回答

2

AbstractConnectionFactory用途:

/** 
* 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 { 

默認情況下。所以,你的服務器應該確保\r\n在它發送的包結束。

或者你可以選擇你的目的的任何合適的解串器: https://docs.spring.io/spring-integration/reference/html/ip.html#connection-factories

+0

我設置好的我的代碼,以確保CRLF行分隔符,但他編寫與主機的連接被中止。而我的客戶端沒有收到任何消息,當我切換服務器時他只是有例外。 – user7953086

+0

您還應該確保服務器接收由\ r \ n分隔的消息。這就是客戶端默認工作的方式。 –

+0

如果我想配置每個包的結束,那麼最好的方法是什麼?使用其他解串器之一? – user7953086