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: ,,,

 

Reader Comments

Hi,

I can't find your contacts on this site (e-mail for instance) that's why I decided to write here.
I'm new to SCTP protocol. Thank you very much for your SCTP samples.
I need to organize M3UA connection over it. I think you have a similar task. Can you tell me please, is there any M3UA Java API or should I create it from scratch?

Thanks.

Sorry for my writing. English isn't my native language :)

regards,
Roman

You can answer me on roman.vologin@gmail.com

Roman,
I also working on something similar. I couldn't find any java API for m3ua, I'm writing it from the scratch following the RFC http://m3ua.com/m3ua.rfc3332.htmlThere is a C-api which you can take as an guidlinehttp://sourceforge.net/projects/m3ua/.
You can also use Wireshark to analyze the packets.

Hi,

I am also working on some think like that.

Thanks for your server sample that helps a lot.

Now I'm facing issue regarding unsigned byte on M3ua message (byte higher than 127).

Can you help please ?

my email is hamado.net -AT gmail.com

@Hamdo
I'm not clear about your problem...
If you can describe it bit more, I may be able to help you.

Hi Sandarenu,
Thx for reply.
sorry, for my bad english.

As, I'm new with java I cannot find how to send byte higher than 127 (for exemple 200) through sctp connexion.

Hams

@Hamdo
I just uses a byte array for that. Even though in java byte is from -127 to 127 we can use that. we can use 4 bytes to represent 32bit int as specified in m3ua. You can use simple conversion function to convert integer to a byte array.

/**
* Convert 32bit Integer to byte format
* @param value
* @return
*/
public static byte[] int32ToByte(int value) {
return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16),
(byte) (value >>> 8), (byte) value };
}

/**
* Convert 16bit Integet to byte format
* @param value
* @return
*/
public static byte[] int16ToByte(int value) {
return new byte[] { (byte) (value >>> 8), (byte) value };
}

public static final int byte2Int(byte[] b) {
if(b.length != 4){
throw new IllegalArgumentException("Should be a byte array with 4 elements.");
}
return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8)
+ (b[3] & 0xFF);
}

public static final int byte2Int16(byte[] b) {
if(b.length != 2){
throw new IllegalArgumentException("Should be a byte array with 2 elements.");
}
return ((b[0] & 0xFF) << 8) + (b[1] & 0xFF);
}

Thanks Sandarenu.

That was exactly what I wanted.

You are nice.

Hello,

First, Thank you for your small tutorial.
With the server side, i can send sctp data, m2pa (payload 5) packet to the client, but i wanted to have chunk flags 0x07 and not 0x03 ...

the message is ok, but chunk flag is wrong...


final ByteBuffer byteBuffer = ByteBuffer.allocate(64000);

byte [] message = new byte ]{0,0,0,0,0};


byteBuffer.put(message);
byteBuffer.flip();

try {
sctpChannel.send(byteBuffer, messageInfo);
}

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

Hi,

I need to know whether SCTP an be implemented in java. How to implement it in java? Will OpenJDK work in windows?

Hi,
I am new in SCTP. Thanks for your SCTP tutorial, I have a problem.
What if sctpChannel.receive function get empty packet i.e. doesn't receive any packet. I need to know about Blocking support for sctpChannel. Can you please help me on these?

Regards,
Ashiq

Hi,

Your SCTP tutorial helped me a lot. Now what ever I am sending over SCTP(Eg: ASP_UP_ACK), is going as SCTP data(I saw in Wireshark). But I want it as M3UA packet. Ffor that, what should i do. Please guide me....

Thank you.

Hello,

Thanks for the post. Is there a way to send/receive an Object in SCTP? I have an object of type Employee, what SCTP API call do I have to make?

Thanks in advance.

Hi,

I follow your sample, when client send data get this exception. NotYetConnected.

In the server side, arrive the connection.

Any idea?

MessageInfo, SctpChannel and SctpServerChannel are giving errors and showing they are not in the API.