1

我正在一个简单的客户端服务器模型中使用 SCTP 进行一些测试。我的服务器代码如下:

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);
            }
        }
    }  

}

以及以下客户端代码:

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;
    }
}

这里的问题是,当我尝试连接到远程 PC 时,SCTP 关联已成功建立。但是当SendMSG调用该函数时,系统会中止并出现以下异常

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)
4

2 回答 2

1

在将您的客户端代码与我上周刚刚编写的模拟器进行比较后,我只能看到一个真正的区别。您没有将客户端的套接字绑定到本地 InetSocketAddress。你在哪里:

sctpChannel.bind(sockAd);

您需要输入以下内容:

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

我相信这会为你做这件事,或者至少让你克服你发布的错误。

于 2013-03-28T15:08:55.257 回答
0

我认为您应该在客户端发送时添加目的地,

        messageInfo = MessageInfo.createOutgoing( null, 0);
        //replace with
        msgInfo = MessageInfo.createOutgoing(new InetSocketAddress(ip, port), 0);
于 2017-06-07T09:07:53.187 回答