[编织消息框架][传输协议]sctp简单开发
2024-09-06 07:10:01
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建立链接要求四次握手
最新文章
- ++a和a++的区别。
- python之路七
- MySQL 第七天(核心优化一)
- EMC DATA DOMAIN 2200 filesys destroy(數據清空及重建)
- iOS 证书申请和使用详解(详细版)
- scenes &; segues within storyboards
- hdu5071 2014 Asia AnShan Regional Contest B Chat
- [Arduino] Arduino Uno R3 中文介绍
- 【HDOJ】4985 Little Pony and Permutation
- thinkPHP17---操作绑定到类
- (转载)Apache与Tomcat 区别联系
- 显示/隐藏Mac隐藏文件
- ccf认证 201709-4 通信网络 java实现
- Oracle Applications DBA 基础(二)
- spring cloud 学习笔记(1)
- EF Core中避免贫血模型的三种行之有效的方法(翻译)
- eq
- IDEA之Git分支以及Stash使用
- seaborn画热力图注意的几点问题
- Axure 第一个原型 简单的登录页面
热门文章
- All you need to know about: solder mask and paste mask
- 【JZOJ6367】工厂(factory)
- luoguP2580 于是他错误的点名开始了 [Trie]
- 0920CSP-S模拟测试赛后总结
- flask中abort()函数的使用
- VS2010-MFC(常用控件:列表框控件ListBox)
- 夏令营501-511NOIP训练18——高三楼
- spring AOP 编程--AspectJ注解方式 (4)
- vue项目导出EXCEL功能
- vue+h-ui+layUI完成列表页及编辑页