【问题标题】:SCTP implementation with java?用java实现SCTP?
【发布时间】:2011-09-17 22:08:40
【问题描述】:

如何用java实现网关和服务器之间的SCTP协议?

【问题讨论】:

  • 你走了多远?还是您要求实施?
  • 这里是 Chris Hegarty 关于 JDK7 中 SCTP 的博客:mhttp://blogs.oracle.com/chegar/entry/sctp_in_java
  • 您好,非常感谢您的快速响应,实际上我处于零点,还没有做任何事情,我只知道 sctp 是如何工作的,并会尝试真正的实现。我需要某种 API 来建立服务器和网关之间的连接。

标签: java gateway sctp 3g-network


【解决方案1】:

如果您的目标是 Java 7,请不要尝试实现它。正如 Andrew 和 Tom 所说,它已经作为核心功能实现。

【讨论】:

    【解决方案2】:

    不需要,用sctp

    https://github.com/RestComm/sctp

    满足大多数需求,效果很好。

    【讨论】:

      【解决方案3】:

      Windows 不支持 SCTP。如果想用 windows plz 安装 sctp 驱动程序。其他明智的使用 linux 正在添加简单的客户端服务器示例

      客户端.java

      package mai;
      import java.io.IOException;   
      import java.net.InetAddress; 
      
      import java.net.InetSocketAddress; 
      
      import java.net.SocketAddress; 
      
      import java.nio.ByteBuffer;
      
      import com.sun.nio.sctp.MessageInfo;
      
      import com.sun.nio.sctp.SctpChannel;
      
      
      public class Client 
      {
      
          public static void main(String[] args)
          { 
              try { 
                  //SocketAddress socketAddress = new InetSocketAddress( 6050); 
                  InetSocketAddress socketAddress = new InetSocketAddress("192.9.200.193", 4444);
                  System.out.println("open connection for socket [" + socketAddress + "]"); 
                  SctpChannel sctpChannel = SctpChannel.open(socketAddress, 1,1);  //(socketAddress, 1 ,1 ); 
                  sctpChannel.bind(new InetSocketAddress(4444)); //6060
                  sctpChannel.connect(socketAddress, 1 ,1);
      
                  System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses()); 
                  System.out.println("sctpChannel.getAllLocalAddresses() = " + sctpChannel.getAllLocalAddresses()); 
                  System.out.println("sctpChannel.isConnectionPending() = " + sctpChannel.isConnectionPending()); 
                  System.out.println("sctpChannel.isOpen() = " + sctpChannel.isOpen()); 
                  System.out.println("sctpChannel.isRegistered() = " + sctpChannel.isRegistered()); 
                  System.out.println("sctpChannel.provider() = " + sctpChannel.provider()); 
                  System.out.println("sctpChannel.association() = " + sctpChannel.association());
      
                  System.out.println("send bytes"); 
                  final ByteBuffer byteBuffer = ByteBuffer.allocate(64000); 
                  //Simple M3ua ASP_Up message 
                  byte [] message = new byte []{1,0,3,1,0,0,0,24,0,17,0,8,0,0,0,1,0,4,0,8,84,101,115,116};
      
                  final MessageInfo messageInfo = MessageInfo.createOutgoing(null, 0); 
                  System.out.println("messageInfo = " + messageInfo); 
                  System.out.println("messageInfo.streamNumber() = " + messageInfo.streamNumber());
      
                  byteBuffer.put(message); 
                  byteBuffer.flip();  
                    sctpChannel.send(byteBuffer, messageInfo); 
      
                  System.out.println("close connection"); 
                  sctpChannel.close();
      System.in.read();
              } catch (Exception e) { 
                  e.printStackTrace(); 
                  try {
                      System.in.read();
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
              } 
          } 
      }
      

      服务器.java

      package mai;
      import java.io.IOException; 
      import java.net.InetSocketAddress; 
      import java.net.SocketAddress; 
      import java.nio.ByteBuffer;
      import java.util.Arrays;
      
      import com.sun.nio.sctp.MessageInfo; 
      import com.sun.nio.sctp.SctpChannel; 
      import com.sun.nio.sctp.SctpServerChannel;
      
      public class Server {
          static ByteBuffer rxBuffer;
          public static void main(String[] args)throws Exception {
              rxBuffer = ByteBuffer.allocateDirect(64000);
      
              rxBuffer.clear();
              rxBuffer.rewind();
              rxBuffer.flip();
          //  SctpChannel xx=SctpChannel.open();
              com.sun.nio.sctp.SctpChannel sc = com.sun.nio.sctp.SctpChannel.open(); 
              SocketAddress serverSocketAddress = new InetSocketAddress(4444); 
              System.out.println("create and bind for sctp address"); 
              SctpServerChannel sctpServerChannel =  SctpServerChannel.open().bind(serverSocketAddress); 
              System.out.println("address bind process finished successfully");
      
              SctpChannel sctpChannel; 
              while ((sctpChannel = sctpServerChannel.accept()) != null) { 
                  System.out.println("client connection received"); 
                  System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses()); 
                  System.out.println("sctpChannel.association() = " + sctpChannel.association()); 
                  MessageInfo messageInfo = sctpChannel.receive(rxBuffer=ByteBuffer.allocateDirect(64000) , null, null); 
                   int len= messageInfo.bytes();
                  System.out.println("Server...      Total bytes recived "+len);
                   System.out.println("Server...      "+messageInfo);
                  rxBuffer.flip();
                  byte[] data = new byte[len];
                  rxBuffer.get(data);
                  rxBuffer.clear();
      
                      System.out.println("Server.....     data  "+Arrays.toString(data));
                    System.out.println("Server.....    close connection"); 
      
              } 
          } 
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-13
        • 2010-12-30
        • 2011-06-22
        • 2017-07-10
        • 2012-08-06
        • 1970-01-01
        • 2017-09-23
        • 2013-02-20
        • 2018-09-18
        相关资源
        最近更新 更多