SCTP Client-Server in Java

As I promised in my previous article here is very simple Client Server example for SCTP in Java. If you still have not installed and tested OpenJDK please follow the instructions given in my previous article.

SCTP Server

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;

import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;

/**
* @author sandarenu
* $LastChangedDate$
* $LastChangedBy$
* $LastChangedRevision$
*/
public class SctpServer {

    public static void main(String[] args) throws IOException {
        SocketAddress serverSocketAddress = new InetSocketAddress(1111);
        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(ByteBuffer.allocate(64000) , null, null);
            System.out.println(messageInfo);

        }
    }
}

SCTP Client

 

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;

/**
* @author sandarenu
* $LastChangedDate$
* $LastChangedBy$
* $LastChangedRevision$
*/
public class SctpClient {

    public static void main(String[] args) throws IOException {
        try {
            SocketAddress socketAddress = new InetSocketAddress( 6050);
            System.out.println("open connection for socket [" + socketAddress + "]");
            SctpChannel sctpChannel = SctpChannel.open();//(socketAddress, 1 ,1 );
            sctpChannel.bind(new InetSocketAddress( 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();

            try {
                sctpChannel.send(byteBuffer, messageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("close connection");
            sctpChannel.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Technorati Tags: ,,,

 

Communicating over SCTP in Java

SCTP - Stream Control Transmission Protocol is a relatively new (standardize in year 200 by IETF) transport layer protocol to design to operate over IP. It is an alternative for TCP and UDP and combines many good features of those protocols. Main intention of developing SCTP was to cater the growing demand of IP telephony market. 

Since SCTP is relatively new protocol programming language support for this is also not as commonly available as TCP or UDP, especially you are Java developer. But is you are a C/C++ developer then you have no problem, there is a very good guide on UNIX - Network Programing Vol1-3rd Edition (chapter 9, 10 and 23).

If you are Java developer then you'd have to use OpenJDK instead of Sun java JDK, since at present only OpenJDK has the support for SCTP. So first thing you have do is to download and install OpenJDK-7.  Currently it only has versions for Linux and Solaris, again if you are using Windows then you'll have to do some more research. I've tried this only on Linux(Fedora), may be later I'll try with Windows as well. If your Linux kernel do not support SCTP, then you'll have install LKSCTP as well. Here is the small getting started guide at OpenJDK site. You can use the small test program given that guide to test whether your SCTP stack if working properly. I'll post small client-server program in SCTP in a later post.

PS: I found this SCTP library for Windows http://www.sctp.be/sctplib/, but couldn't check whether there is a support from OpenJDK-SCTP side for windows. If you find something please let me know as well.

Technorati Tags: ,,,,