Вопрос

I am doing some testing with SCTP in a simple client server model. My Server code is following:

public class SCTPServer extends Thread {

    InetSocketAddress serverSocketAddress=null;
    SctpServerChannel sctpServerChannel;
    SctpChannel sctpChannel;
    boolean running = false;
    ByteBuffer bf = ByteBuffer.allocateDirect(160);
    String id= null;
    public SCTPClient  sctpClient= null;
    public SCTPServer(String IP, int  port, String clientIp, int clientPort, String id) {

        try {
            serverSocketAddress = new InetSocketAddress(IP, port);
            sctpServerChannel = SctpServerChannel.open().bind(serverSocketAddress);

        } catch (Exception e) {
            System.out.println("Failed to open Sctp connection Reciver on IP:" + IP + " and Port:" + port+"  "+e);
            System.exit(0);
        }
    }

    public void run()
    {
        running = true;
        while (running) {           

            try {
                sctpClient.sendAsp();
                sctpChannel = sctpServerChannel.accept();
                try {
                sctpChannel.receive(bf, null, null);
                bf.flip();
                byte[] barry = new byte[bf.limit()];   //we may keep the received data on this new byte arrray here or in some 
                bf.get(barry, 0, bf.limit());
                bf.clear();
                System.out.println("new sctp message is received");
                System.out.println(new String(barry));


            } catch (IOException ex) {
                System.out.println("Failed to receive from sctp Channel " + ex);
            }

            } catch (Exception ex) {
                System.out.println("Exception at Starting SCTP Server channel"+ ex);
            }
        }
    }  

}

and the client code following:

public class SCTPClient extends Thread {

    SctpChannel sctpChannel;
    InetSocketAddress socketAddress;//= new InetSocketAddress("202.51.176.44", 5555); 
    boolean runniung = false;
    ByteBuffer bf = ByteBuffer.allocateDirect(160);
    String id= null;

    public SCTPClient(String ip, int port, int maxInStream, int maxOutStream, String id) {
        socketAddress = new InetSocketAddress(ip, port);
        this.id = id;
        //InetSocketAddress sockAd = new InetSocketAddress("202.51.176.44",55556);
        try {
            sctpChannel = SctpChannel.open();
            System.out.println("SCTP  connection opened with IP==" + ip + " port == " + port);
            //sctpChannel.bind(sockAd);
            sctpChannel.connect(socketAddress, maxInStream, maxOutStream);


            runniung = true;
            sendMSG();

        } catch (Exception ex) {
            System.out.println("Exception at opening sctp connection:" + ex);
            System.exit(0);
        }
    }
    public void sendMSG()
    {
        MessageInfo messageInfo =null;
        /*try {
        } catch (IOException ex) {
            System.out.println("Failed to create Message Info " + ex.toString());
        }*/

            messageInfo = MessageInfo.createOutgoing( null, 0);
        ByteBuffer byteBuffer = buildMessage(1, "test");
        try {
            sctpChannel.send(byteBuffer, messageInfo);
            byteBuffer.clear();
            System.out.println("A message has been sent");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed to send the message due to :" + e.toString());
        }
    }

    public static ByteBuffer buildMessage(Integer id,String infoString)
    {

        //We can calculate the total length of the message hard codedly, even before building the total message.
        int totalLength = (id!=null) ? 16 : 8;
        totalLength += (infoString!=null) ? (4+infoString.length()) : 0;
        ByteBuffer data = ByteBuffer.allocateDirect(totalLength);



        //Own part of ASP Up message
        if(id!=null)
        {

            data.putInt(ASPIdentifier);            
        }
        if(infoString!=null)
        {

            data.put(infoString.getBytes());
        }
        return data;
    }
}

The problem here is that, when I try to connect to a remote PC the SCTP association is established successfully. But when the SendMSG function is called the system aborts with following exception

java.net.SocketException: Invalid argument
at sun.nio.ch.SctpChannelImpl.send0(Native Method) Failed to send the message due to :java.net.SocketException: Invalid argument
at sun.nio.ch.SctpChannelImpl.sendFromNativeBuffer(SctpChannelImpl.java:1027)
at sun.nio.ch.SctpChannelImpl.send(SctpChannelImpl.java:987)
at sun.nio.ch.SctpChannelImpl.send(SctpChannelImpl.java:967)
at sctptester.SCTPClient.sendMSG(SCTPClient.java:79)
at sctptester.SCTPClient.<init>(SCTPClient.java:40)
at test.main(test.java:66)
Это было полезно?

Решение

After comparing your client code to a simulator that I just wrote last week, I can see only one real difference. You did not bind your client's socket to a local InetSocketAddress. Where you have:

sctpChannel.bind(sockAd);

You need to put something like:

InetSocketAddress localISA = new InetSocketAddress(localIPAddress, localPort);
sctpChannel.bind(localISA);

I believe this will do it for you, or at least get you past the error you posted.

Другие советы

I think you should add the destination when sending in the client,

        messageInfo = MessageInfo.createOutgoing( null, 0);
        //replace with
        msgInfo = MessageInfo.createOutgoing(new InetSocketAddress(ip, port), 0);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top