我正在使用 Java 使用 Java 和 SCTP 协议创建一对多连接。
我已经启动并运行了 SctpMultiChannel 并且可以接收消息,但是是否需要做一些特别的事情才能向所有连接发送消息?
The error message:
java.net.SocketException: Invalid argument
at sun.nio.ch.SctpChannelImpl.send0(Native Method)
at sun.nio.ch.SctpMultiChannelImpl.send0(SctpMultiChannelImpl.java:976)
at sun.nio.ch.SctpMultiChannelImpl.sendFromNativeBuffer(SctpMultiChannelImpl.java:888)
at sun.nio.ch.SctpMultiChannelImpl.send(SctpMultiChannelImpl.java:847)
at sun.nio.ch.SctpMultiChannelImpl.send(SctpMultiChannelImpl.java:824)
at org.test.MultiServer.run(MultiServer.java:56)
以及完整的代码:
SocketAddress serverSocketAddress = new InetSocketAddress(SOCKET_PORT);
SctpMultiChannel smc;
try {
Logger.info("Start");
smc = SctpMultiChannel.open().bind(serverSocketAddress);
ByteBuffer buf = ByteBuffer.allocateDirect(160);
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
AssociationHandler assocHandler = new AssociationHandler();
MessageInfo messageInfo;
while ((messageInfo = smc.receive(buf, System.out, assocHandler)) != null) {
Logger.debug(messageInfo.toString());
buf.flip();
Logger.debug(decoder.decode(buf).toString());
Logger.info(smc.associations().size() + "");
ByteBuffer buf1 = ByteBuffer.allocateDirect(60);
CharBuffer cbuf1 = CharBuffer.allocate(60);
charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
cbuf1.put("foo").flip();
encoder.encode(cbuf1, buf1, true);
buf1.flip();
for (Association ass : smc.associations()) {
MessageInfo mi = MessageInfo.createOutgoing(ass, null, 1);
smc.send(buf, mi);
}
cbuf1.clear();
buf1.clear();
buf1.flip();
}
smc.close();
} catch (IOException e) {
Logger.error("Server error");
e.printStackTrace();
}
这是我到目前为止所做的,我的客户发送一条消息,然后等待接收一条新消息。我的目标是采用非阻塞方法,这样我就可以在同一通道上发送和接收消息,而不是使用接收查询进行阻塞。
有人有这方面的经验吗?
最好的,亨里克