単純なクライアント サーバー モデルで 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)