[编织消息框架][传输协议]stcp简单开发

时间:2023-03-10 03:33:36
[编织消息框架][传输协议]stcp简单开发

jdk1.7支持sctp协议,需要linux安装sctp支持库

测试代码

 public class ServerSCTP {
static int SERVER_PORT = 3456;
static int US_STREAM = 0;
static int FR_STREAM = 1; static SimpleDateFormat USformatter = new SimpleDateFormat("h:mm:ss a EEE d MMM yy, zzzz", Locale.US);
static SimpleDateFormat FRformatter = new SimpleDateFormat("h:mm:ss a EEE d MMM yy, zzzz", Locale.FRENCH); @SuppressWarnings("restriction")
public static void main(String[] args) throws IOException {
com.sun.nio.sctp.SctpServerChannel ssc = com.sun.nio.sctp.SctpServerChannel.open();
ssc.bind(new InetSocketAddress(SERVER_PORT)); ByteBuffer buf = ByteBuffer.allocateDirect(60);
CharBuffer cbuf = CharBuffer.allocate(60);
CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder(); while (true) {
com.sun.nio.sctp.SctpChannel sc = ssc.accept(); /* get the current date */
Date today = new Date();
cbuf.put(USformatter.format(today)).flip();
encoder.encode(cbuf, buf, true);
buf.flip(); /* send the message on the US stream */
com.sun.nio.sctp.MessageInfo messageInfo = com.sun.nio.sctp.MessageInfo.createOutgoing(null, US_STREAM);
sc.send(buf, messageInfo); /* update the buffer with French format */
cbuf.clear();
cbuf.put(FRformatter.format(today)).flip();
buf.clear();
encoder.encode(cbuf, buf, true);
buf.flip(); /* send the message on the French stream */
messageInfo.streamNumber(FR_STREAM);
sc.send(buf, messageInfo); cbuf.clear();
buf.clear(); sc.close();
}
}
}
 @SuppressWarnings("restriction")
public class ClientSCTP {
static int SERVER_PORT = 3456;
public void run() throws IOException {
ByteBuffer buf = ByteBuffer.allocateDirect(60);
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder(); com.sun.nio.sctp.SctpChannel sc = com.sun.nio.sctp.SctpChannel.open(new InetSocketAddress("localhost", SERVER_PORT), 0, 0); /* handler to keep track of association setup and termination */
AssociationHandler assocHandler = new AssociationHandler();
/* expect two messages and two notifications */
com.sun.nio.sctp.MessageInfo messageInfo = null;
do {
messageInfo = sc.receive(buf, System.out, assocHandler);
buf.flip(); if (buf.remaining() > 0 && messageInfo.streamNumber() == ServerSCTP.US_STREAM) { System.out.println("(US) " + decoder.decode(buf).toString());
} else if (buf.remaining() > 0 && messageInfo.streamNumber() == ServerSCTP.FR_STREAM) {
System.out.println("(FR) " + decoder.decode(buf).toString());
}
buf.clear();
} while (messageInfo != null); sc.close();
} public static void main(String[] args) throws IOException {
new ClientSCTP().run();
} static class AssociationHandler extends com.sun.nio.sctp.AbstractNotificationHandler<PrintStream> {
public com.sun.nio.sctp.HandlerResult handleNotification(com.sun.nio.sctp.AssociationChangeNotification not, PrintStream stream) {
if (not.event().equals(com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent.COMM_UP)) {
int outbound = not.association().maxOutboundStreams();
int inbound = not.association().maxInboundStreams();
stream.printf("New association setup with %d outbound streams" + ", and %d inbound streams.\n", outbound, inbound);
} return com.sun.nio.sctp.HandlerResult.CONTINUE;
} public com.sun.nio.sctp.HandlerResult handleNotification(com.sun.nio.sctp.ShutdownNotification not, PrintStream stream) {
stream.printf("The association has been shutdown.\n");
return com.sun.nio.sctp.HandlerResult.RETURN;
}
}
}

导出ClientSCTP.class,ServerSCTP.class

环境部署

1.linux检查是否支持sctp,官方提示必须内核2.6版本以上,有信息显示代表已安装

lsmod | grep sctp

1.1如果没有就下载

官方:http://lksctp.sourceforge.net/

github:https://github.com/sctp/lksctp-tools

tar -zxf lksctp-tools-1.0..tar.gz

cd lksctp-tools-1.0.

./configure

make

make install

2.执行  ./java8.sh -jar testsctp.jar 抛异常

java.lang.UnsupportedOperationException: libsctp.so.1: cannot open shared object file: No such file or directory

原因是没有配置 $LD_LIBRARY_PATH 环境变量

echo $LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib

继续执行 ./java8.sh -jar testsctp.jar 还是抛异常

java.net.SocketException: Protocol not supported

原因是系统没有加载lksctp模块

modprobe sctp

lsmod |grep sctp

再执行出现连接异常

java.net.SocketException: Permission denied

原因是linux selinux 安全限制,解决方式临时关闭

setenforce 0

永久关闭

ehco "SELINUX=disabled" >> /etc/selinux/config

本人测试:连接60000client没aio快,原因是sctp建立链接要求四次握手