[编织消息框架][传输协议]sctp简单开发
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
tar -zxf lksctp-tools-1.0.17.tar.gz cd lksctp-tools-1.0.17 ./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建立链接要求四次握手
作者: | solq |
博客地址: | http://www.cnblogs.com/solq111 |
博客版权: | 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。 如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。 淘宝店: 海豚极货店 QQ群:9547527 |
如果你热爱生活、热爱编程、热爱吉他。扫一扫加我微信 |
我的新书《编织消息框架》目前进行中,感谢大家关注! |
本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。 |