我已成功使用Spring和TCP接收数据包。
但是,收到消息后,我找不到回复的方式。
编辑: 似乎在读取输入流后,Spring关闭了连接。这似乎有问题,因为输出流具有与输入流完全相同的id,IP和端口。我的输出流似乎也关闭了。我看不到如何解决这个问题
以下是我的代码:
我的XML配置:
<int-ip:tcp-connection-factory id="serverConnectionFactory"
type="server"
port="${server.port}"
lookup-host="false"
single-use="true"
using-nio="true"
so-timeout="${socket.timeout}"
serializer="telegramReceiveSerializer"
deserializer="telegramReceiveSerializer"/>
<int:channel id="serverInput" />
<int:channel id="serverOutput" />
<int-ip:tcp-inbound-gateway id="serverGateway"
request-channel="serverInput"
reply-channel="serverOutput"
connection-factory="serverConnectionFactory"
reply-timeout="${socket.timeout.write}"
/>
<int:service-activator
input-channel="serverInput"
output-channel="clientOutput"
ref="telegramGateway"
method="reply"
requires-reply="false"
send-timeout="${socket.timeout.write}" />
我的回复功能:
public Telegram reply(Telegram receivedRequestTelegram) {
return receivedRequestTelegram;
}
应该在输出流中写入的我的序列化函数:
public void serialize(Telegram telegram, OutputStream out) throws IOException {
...
TelegramHeader header = telegram.getHeader();
TelegramPayload payload = telegram.getPayload();
...
byte[] result = prepareTelegram(header, payload);
out.write(result);
...
}
我的反序列化器从输入流返回电报:
public Telegram deserialize(InputStream in) throws IOException {
...
}
如果有人可以向我解释我做错了什么,那将是很大的帮助。谢谢!
编辑:
我收到以下错误消息:
在处理TcpInboutGateway类(doOnMessage函数)的答复GenericMessage时找不到连接