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

测试代码

  1. public class ServerSCTP {
  2. static int SERVER_PORT = 3456;
  3. static int US_STREAM = 0;
  4. static int FR_STREAM = 1;
  5.  
  6. static SimpleDateFormat USformatter = new SimpleDateFormat("h:mm:ss a EEE d MMM yy, zzzz", Locale.US);
  7. static SimpleDateFormat FRformatter = new SimpleDateFormat("h:mm:ss a EEE d MMM yy, zzzz", Locale.FRENCH);
  8.  
  9. @SuppressWarnings("restriction")
  10. public static void main(String[] args) throws IOException {
  11. com.sun.nio.sctp.SctpServerChannel ssc = com.sun.nio.sctp.SctpServerChannel.open();
  12. ssc.bind(new InetSocketAddress(SERVER_PORT));
  13.  
  14. ByteBuffer buf = ByteBuffer.allocateDirect(60);
  15. CharBuffer cbuf = CharBuffer.allocate(60);
  16. CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
  17.  
  18. while (true) {
  19. com.sun.nio.sctp.SctpChannel sc = ssc.accept();
  20.  
  21. /* get the current date */
  22. Date today = new Date();
  23. cbuf.put(USformatter.format(today)).flip();
  24. encoder.encode(cbuf, buf, true);
  25. buf.flip();
  26.  
  27. /* send the message on the US stream */
  28. com.sun.nio.sctp.MessageInfo messageInfo = com.sun.nio.sctp.MessageInfo.createOutgoing(null, US_STREAM);
  29. sc.send(buf, messageInfo);
  30.  
  31. /* update the buffer with French format */
  32. cbuf.clear();
  33. cbuf.put(FRformatter.format(today)).flip();
  34. buf.clear();
  35. encoder.encode(cbuf, buf, true);
  36. buf.flip();
  37.  
  38. /* send the message on the French stream */
  39. messageInfo.streamNumber(FR_STREAM);
  40. sc.send(buf, messageInfo);
  41.  
  42. cbuf.clear();
  43. buf.clear();
  44.  
  45. sc.close();
  46. }
  47. }
  48. }
  1. @SuppressWarnings("restriction")
  2. public class ClientSCTP {
  3. static int SERVER_PORT = 3456;
  4. public void run() throws IOException {
  5. ByteBuffer buf = ByteBuffer.allocateDirect(60);
  6. Charset charset = Charset.forName("ISO-8859-1");
  7. CharsetDecoder decoder = charset.newDecoder();
  8.  
  9. com.sun.nio.sctp.SctpChannel sc = com.sun.nio.sctp.SctpChannel.open(new InetSocketAddress("localhost", SERVER_PORT), 0, 0);
  10.  
  11. /* handler to keep track of association setup and termination */
  12. AssociationHandler assocHandler = new AssociationHandler();
  13. /* expect two messages and two notifications */
  14. com.sun.nio.sctp.MessageInfo messageInfo = null;
  15. do {
  16. messageInfo = sc.receive(buf, System.out, assocHandler);
  17. buf.flip();
  18.  
  19. if (buf.remaining() > 0 && messageInfo.streamNumber() == ServerSCTP.US_STREAM) {
  20.  
  21. System.out.println("(US) " + decoder.decode(buf).toString());
  22. } else if (buf.remaining() > 0 && messageInfo.streamNumber() == ServerSCTP.FR_STREAM) {
  23. System.out.println("(FR) " + decoder.decode(buf).toString());
  24. }
  25. buf.clear();
  26. } while (messageInfo != null);
  27.  
  28. sc.close();
  29. }
  30.  
  31. public static void main(String[] args) throws IOException {
  32. new ClientSCTP().run();
  33. }
  34.  
  35. static class AssociationHandler extends com.sun.nio.sctp.AbstractNotificationHandler<PrintStream> {
  36. public com.sun.nio.sctp.HandlerResult handleNotification(com.sun.nio.sctp.AssociationChangeNotification not, PrintStream stream) {
  37. if (not.event().equals(com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent.COMM_UP)) {
  38. int outbound = not.association().maxOutboundStreams();
  39. int inbound = not.association().maxInboundStreams();
  40. stream.printf("New association setup with %d outbound streams" + ", and %d inbound streams.\n", outbound, inbound);
  41. }
  42.  
  43. return com.sun.nio.sctp.HandlerResult.CONTINUE;
  44. }
  45.  
  46. public com.sun.nio.sctp.HandlerResult handleNotification(com.sun.nio.sctp.ShutdownNotification not, PrintStream stream) {
  47. stream.printf("The association has been shutdown.\n");
  48. return com.sun.nio.sctp.HandlerResult.RETURN;
  49. }
  50. }
  51. }

导出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

  1. tar -zxf lksctp-tools-1.0..tar.gz
  2.  
  3. cd lksctp-tools-1.0.
  4.  
  5. ./configure
  6.  
  7. make
  8.  
  9. 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 环境变量

  1. echo $LD_LIBRARY_PATH
  2. export LD_LIBRARY_PATH=/usr/local/lib

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

java.net.SocketException: Protocol not supported

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

  1. modprobe sctp
  2.  
  3. lsmod |grep sctp

再执行出现连接异常

java.net.SocketException: Permission denied

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

setenforce 0

永久关闭

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

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

[编织消息框架][传输协议]sctp简单开发的更多相关文章

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

    测试代码 public class ServerSTCP { static int SERVER_PORT = 3456; static int US_STREAM = 0; static int F ...

  2. [编织消息框架][传输协议]sctp

    OSI(Open System Interconnect),即开放式系统互联. 一般都叫OSI参考模型,是ISO(国际标准化组织)组织在1985年研究的网络互联模型. 该体系结构标准定义了网络互连的七 ...

  3. [编织消息框架][消息服务]rmi

    RMI(即Remote Method Invoke 远程方法调用) 远程对象: 用于远程客户端调用 必需继承java.rmi.Remote,每个调用方法必须添加java.rmi.RemoteExcep ...

  4. [编织消息框架][netty源码分析]2 eventLoop

    eventLoop从命名上看是专门处理事件 事件系统主要由线程池同队列技术组成,有以下几个优点 1.任务出队有序执行,不会出现错乱,当然前提执行线程池只有一个 2.解偶系统复杂度,这是个经典的生产者/ ...

  5. [编织消息框架][消息服务]jmx

    JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架,使用的是RMI技术. 比较经典的应用jdk bin目录下 jcons ...

  6. [编织消息框架][设计协议]优化long,int转换

    理论部分 一个long占8byte,大多数应用业数值不超过int每次传输多4byte会很浪费 有没有什么办法可以压缩long或int呢? 答案是有的,原理好简单,如果数值不超过int.max_valu ...

  7. [编织消息框架][网络IO模型]BIO

    既然跟网络内容有关就不得不学习网络IO模型,时代在进步,技术也在进步,采取使用那种网络IO模型就已经确定应用程序规模 阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都 ...

  8. [编织消息框架][netty源码分析]7 Unsafe 实现类NioSocketChannelUnsafe职责与实现

    Unsafe 是channel的内部接口,从书写跟命名上看是不公开给开发者使用的,直到最后实现NioSocketChannelUnsafe也没有公开出去 public interface Channe ...

  9. [编织消息框架][JAVA核心技术]动态代理应用7-IRpcSend实现

    根据设计生成两个接口,IRpcSend send方法返回数据要求包装成QResult对象 public interface IRpcSend { public <T> QResult< ...

随机推荐

  1. thinkphp cbd模式

    ThinkPHP从3.0版本开始引入了全新的CBD(核心Core+行为Behavior+驱动Driver)架构模式,因为从底层开始,框架就采用核心+行为+驱动的架构体系,核心保留了最关键的部分,并在重 ...

  2. 判断MDI窗体的子窗体是否存在

    //***************************************************************************//函 数名: CreateForm//返 回 ...

  3. shiro框架的组成和内部结构(下一篇为spring整合shiro)

    1.shiro简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. ​ 实际上,Shir ...

  4. Android Matrix理论与应用详解

    转:http://zensheno.blog.51cto.com/2712776/513652 Matrix学习——基础知识 以前在线性代数中学习了矩阵,对矩阵的基本运算有一些了解,前段时间在使用GD ...

  5. overload和override二者之间的区别

    overload和override三者之间的区别 Overload是重载,是有相同的方法名,但参数类型或个数彼此不同Override是重写,是在子类与父类中,子类中的方法的方法名,参数个数.类型都与父 ...

  6. 像bootstrap一样的去做web编程

    1: 闭包 boot的闭包方式有点特别,普通的闭包是这样的: (function ($) { })(jQuery) 这种写法是怕全局污染,把$封闭在自己的空间里,暴露在外面的只有jQuery,这样,如 ...

  7. Go前言

    Go语言为并发而生 硬件制造商正在为处理器添加越来越多的内核以来提高性能.所有数据中心都在这些处理器上运行,今天的应用程序使用多个微服务来维护数据库连接,消息队列和维护缓存.所以,开发的软件和编程语言 ...

  8. Activiti常用类介绍

    为什么要使用工作流? 传统的设计在流程发生变化时的弊端: 1. 流程相关的属性和业务对象的属性,都放到了业务对象中. 2. 流程相关的逻辑和业务逻辑,都放到的业务逻辑中 常用类 ProcessEngi ...

  9. sql 根据列名查所属表名

    比如 有一个jueseID字段,想知道这个字段是哪个表里的. 第一步: select * from syscolumns where name = 'jueseID' 第二步: select * fr ...

  10. JAVA 类加载机制学习笔记

    JAVA 类生命周期 如上图所示,Java类的生命周期如图所示,分别为加载.验证.准备.解析.初始化.使用.卸载.其中验证.准备.解析这三个步骤统称为链接. 加载:JVM根据全限定名来获取一段二进制字 ...