1

The server use java,and communicate with C++ clients via TCP. WHen the user leaves computer over 5 minutes,the client will send a byte array to the server.The java server is written by spring-integration-ip.I confused if I should set using-nio="true" ?Is there any problem about concurrency?HOW I trasform the byte array using custom protocol?I'm new to TCP and spring-integration,thanks for your helps!

I hava encounter a problem when I write my app follow the sample.The server deserialize the byte stream and then echo the service,but the client can't receive the serialized byte stream after the echo service return.

spring-tcp.xml:

<int-ip:tcp-connection-factory 
     id="serverConnectionFactory"
     type="server" port="5678" 
     using-nio="true" 
     serializer="connectionSerializeDeserialize"
     deserializer="connectionSerializeDeserialize" />

<bean id="connectionSerializeDeserialize" 
      class="com.snail.tcp.CustomSerializerDeserializer" />

<int-ip:tcp-inbound-gateway id="gatewayCrLf"
    connection-factory="serverConnectionFactory" request-channel="toSA"
    error-channel="errorChannel" />

<int:channel id="toSA" />

<int:service-activator input-channel="toSA"
    ref="echoService" method="test" />
<bean id="echoService" class="com.snail.tcp.EchoService" />

(de)serialize method:

private static final int LENGTH = 2;
private static final int FLAG = 2;


@Override
public String deserialize(InputStream is) throws IOException {
    logger.debug("begin deserialize");
    int length = Integer.valueOf(parseString(is, LENGTH));
    int flag = Integer.valueOf(parseString(is, FLAG));
    String content = parseString(is, length);
    return content;
}

@Override
public void serialize(String s, OutputStream os) throws IOException {
    logger.debug("begin serialize:" + s);
    byte[] content = s.getBytes();
    os.write(content);
    os.flush();
}

echo service:

 public String test(String input) {
    logger.debug("echo service:" + input);
    //some service...
    return "echo:" + input;
}

main method:

 public static void main(String[] args) {
    Socket socket = null;
    Writer out = null;
    BufferedReader in=null;
    try {
        socket = new Socket("127.0.0.1", 5678);
        socket.setSoTimeout(10000);
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        out.write("1001HELLOWORLD");
        out.flush();

        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        StringBuffer str = new StringBuffer();
        int c;
        while ((c = in.read()) != -1) {
            str.append((char) c);
        }
        System.out.println(str.toString());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

the console log:

[DEBUG] com.snail.tcp.CustomSerializerDeserializer-deserialize(): begin deserialize

[DEBUG] com.snail.tcp.EchoService-test(): echo service:HELLOWORLD

[DEBUG] com.snail.tcp.CustomSerializerDeserializer-serialize(): begin serialize:echo:HELLOWORLD

but the client can't receive the deserialized message 'HELLOWORLD',instead a SocketTimeoutException: Read timed out... I want to know where i did wrong,thx a lot

1
  • You are asking lots of different questions here and not providing much detail. I would suggest you break this down into separate questions and in each, provide some details on what you have tried so far, what you want to do, and exactly what is goign wrong. Commented Oct 31, 2013 at 2:22

1 Answer 1

3

The framework takes care of concurrency; you generally only need NIO if you are supporting a large number of clients; for a small number of clients, NIO is generally not needed.

You can either use a custom (de)serializer or put a transformer downstream of the inbound endpoint.

I suggest you read the Documentation, look at the samples and come back with more specific questions if you still have any.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your advice.Would you please figure out the problem above.
Probably means the serializer is not terminating the message in the way the client expects; do you see a logger.debug("Message sent " + message); log?. It should appear after your serializer DEBUG message. Your inbound message has structure but your outbound just sends the raw data. Doesn't the client expect some structure? You may have to run Wireshark or similar to look at exactly what is being sent.
I debug my server.When it execute os.write(content) in serialize() method ,it raise a java.nio.channels.ClosedChannelException.I only want to return a simple string such as "SUCCESS" to the client instead of a structure.Can i realize this in the serialize() method or other place?
You can send whatever you want (or your client needs); it just looked odd to me. If you are getting a ChannelClosedException, the channel is, er, closed. Probably by the client. If you turn on DEBUG logging all the connection open/close activity will be logged.
I have solved the problem.I only change out.flush() to out.close(),and the client can receive the message!I think it seems to be the server didn't know the end of the message,so it results in a time out. Thank you for the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.