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

[编织消息框架][传输协议]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. scala中Map集合的简单使用

    import scala.collection.mutable /** * Map集合的简单使用 */ object MapUse { def main(args: Array[String]): U ...

  2. thinkphp 正则路由

    正则路由也就是采用正则表达式定义路由的一种方式,依靠强大的正则表达式,能够定义更灵活的路由规则. 路由表达式支持的正则定义必须以“/”开头,否则就视为规则表达式.也就是说如果采用: '#^blog\/ ...

  3. cf1147

    C——筛法 #include<bits/stdc++.h> using namespace std; ]; int main(){ cin>>n; ; ;i<=n;i++ ...

  4. 几个dp的陈年老题

    真 陈年老题 都是基础的dp优化 主要是展现我基础薄弱,菜得抠脚 1.四边形不等式 四边形不等式:w[i][j]+w[i+1][j+1]<=w[i+1][j]+w[i][j+1] 对于f[i][ ...

  5. 尚学python课程---14、python中级语法

    尚学python课程---14.python中级语法 一.总结 一句话总结: var[1:5] 访问模式:比如字符串,比如列表元祖,字典等 del 删除模式:比如列表.元祖.字典 1.Python的N ...

  6. JS 内置对象 String对象

    JS内置对象   String对象:字符串对象,提供了对字符串进行操作的属性和方法.   Array对象:数组对象,提供了数组操作方面的属性和方法.   Date对象:日期时间对象,可以获取系统的日期 ...

  7. STM32 解析futaba S-bus协议

    S-bus为futaba使用的串行通信协议.实际上为串口通信.但是有几点需要注意: 在大端小端上,网上资料都说的不是很清楚: 跟TTL串口信号相比,S-bus的逻辑电平是反的,需用如下电路对电平反相, ...

  8. Git合并时遇到冲突或错误后取消合并

    当合并分支时遇到错误或者冲突,分支旁边会多出“|MERGING”这个东西 有这个状态存在时,会导致后面想要再合并的时候提示如下 所以需要先取消这次合并,使用“git merge --abort”命令

  9. <小白学技术>将python脚本导出为exe可执行程序

    1.简介(为啥需要导出为exe可执行程序) python写完的程序靠命令来执行,显得太专业,不符合python简单的特点(好吧,主要是太low) 代码给别人执行,别人没有你的python库也没法用(双 ...

  10. 「APIO 2019」桥梁

    题目 三天终于把\(APIO\)做完了 这题还是比较厉害的,如果不知道这是个分块应该就自闭了 考虑一个非常妙的操作,按照操作分块 我们设一个闸值\(S\),把\(S\)个边权修改操作分成一块,把所有的 ...