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; import java.nio.charset.Charset; import java.nio.CharBuffer; import java.nio.charset.CharsetDecoder; /** * @author sandarenu * $LastChangedDate$ * $LastChangedBy$ * $LastChangedRevision$ */ public class Servidor { static int SERVER_PORT = 3456; static int BUFFER_SIZE = 80; public static void main(String[] args) throws IOException { SocketAddress serverSocketAddress = new InetSocketAddress(SERVER_PORT); ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE); System.out.println("create and bind for sctp address"); SctpServerChannel sctpServerChannel = SctpServerChannel.open().bind(serverSocketAddress); System.out.println("address bind process finished successfully"); Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); SctpChannel sctpChannel; String msgIn=null; MessageInfo messageInfo = null; while ((sctpChannel = sctpServerChannel.accept()) != null) { printInfo(sctpChannel); do{ messageInfo = sctpChannel.receive(buf , System.out, null); System.out.println(messageInfo); buf.flip(); if(buf.remaining() > 0){ msgIn=decoder.decode(buf).toString(); System.out.println("MSG REVEIVED: " +msgIn ); buf.clear(); } if(msgIn.equals("exit")) break; }while(messageInfo != null); // buf.clear(); } } public static void printInfo(SctpChannel sctpChannel) { try { 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()); } catch(IOException e) { e.printStackTrace(); } } }