1

服务器使用java,通过TCP与C++客户端通信。当用户离开计算机超过5分钟时,客户端将向服务器发送一个字节数组。java服务器是由spring-integration-ip编写的。我是否应该设置using-nio =“true”?有什么问题吗?关于并发?我如何使用自定义协议转换字节数组?我是 TCP 和弹簧集成的新手,感谢您的帮助!

当我按照示例编写我的应用程序时遇到一个问题。服务器反序列化字节流然后回显服务,但回显服务返回后客户端无法接收序列化字节流。

春天-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" />

(反)序列化方法:

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

回声服务:

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

主要方法:

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

控制台日志:

[调试] com.snail.tcp.CustomSerializerDeserializer-deserialize(): 开始反序列化

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

[调试] com.snail.tcp.CustomSerializerDeserializer-serialize(): 开始序列化:echo:HELLOWORLD

但客户端无法接收反序列化消息“HELLOWORLD”,而是 SocketTimeoutException:读取超时...我想知道我做错了什么,谢谢

4

1 回答 1

3

该框架负责并发;如果您支持大量客户端,您通常只需要 NIO;对于少数客户端,一般不需要NIO。

您可以使用自定义(反)序列化程序,也可以将转换器放在入站端点的下游。

我建议您阅读文档,查看示例,如果您仍有任何问题,请返回更具体的问题。

于 2013-10-31T02:48:52.853 回答