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