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建立链接要求四次握手

最新文章

  1. ++a和a++的区别。
  2. python之路七
  3. MySQL 第七天(核心优化一)
  4. EMC DATA DOMAIN 2200 filesys destroy(數據清空及重建)
  5. iOS 证书申请和使用详解(详细版)
  6. scenes &amp; segues within storyboards
  7. hdu5071 2014 Asia AnShan Regional Contest B Chat
  8. [Arduino] Arduino Uno R3 中文介绍
  9. 【HDOJ】4985 Little Pony and Permutation
  10. thinkPHP17---操作绑定到类
  11. (转载)Apache与Tomcat 区别联系
  12. 显示/隐藏Mac隐藏文件
  13. ccf认证 201709-4 通信网络 java实现
  14. Oracle Applications DBA 基础(二)
  15. spring cloud 学习笔记(1)
  16. EF Core中避免贫血模型的三种行之有效的方法(翻译)
  17. eq
  18. IDEA之Git分支以及Stash使用
  19. seaborn画热力图注意的几点问题
  20. Axure 第一个原型 简单的登录页面

热门文章

  1. All you need to know about: solder mask and paste mask
  2. 【JZOJ6367】工厂(factory)
  3. luoguP2580 于是他错误的点名开始了 [Trie]
  4. 0920CSP-S模拟测试赛后总结
  5. flask中abort()函数的使用
  6. VS2010-MFC(常用控件:列表框控件ListBox)
  7. 夏令营501-511NOIP训练18——高三楼
  8. spring AOP 编程--AspectJ注解方式 (4)
  9. vue项目导出EXCEL功能
  10. vue+h-ui+layUI完成列表页及编辑页