SCTP用java实现?

发布于 2024-12-05 09:36:46 字数 32 浏览 3 评论 0原文

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

How to implement SCTP protocol between a gateway and a server with java ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不气馁 2024-12-12 09:36:46

如果您的目标是 Java 7,请不要尝试实现它。正如安德鲁和汤姆所说,它已经作为核心功能实现了。

If your target is Java 7 never try to implement it. As Andrew and Tom stated its already implemented as a core feature.

徒留西风 2024-12-12 09:36:46

不需要,只需使用sctp

https://github.com/RestComm/sctp< /a>

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

No need, just use sctp:

https://github.com/RestComm/sctp

Handles most needs, works great.

失眠症患者 2024-12-12 09:36:46

Windows 不支持 SCTP。如果想在 Windows 上使用,请安装 sctp 驱动程序。否则,使用 linux 我添加简单的客户端服务器示例

Client.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();
            }
        } 
    } 
}

Server.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"); 

        } 
    } 
}

Windows cannot support SCTP. if want to does with windows plz install sctp driver.Other wise use linux am adding simple client server example

Client.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();
            }
        } 
    } 
}

Server.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"); 

        } 
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文