1. 指定在某个stream上发送消息
在调用sctp_sendmsg函数发送消息的时候,我们可以设置stream ID,
sctp_sendmsg(sock_op->socket_fd(), message, MAX_BUFFER,
(sockaddr *)&servaddr, sizeof(servaddr), 0, 0, stream, 0, 0);
倒数第三个参数就是stream,我们可以指定在某个stream发送消息,
- 一个SCTP association包含多个stream,默认情况下最大有10个stream;
- 每个stream上的传输相互独立,比如说stream 0的传输出现阻塞了,不会影响stream 1上的消息传输
- 每个stream上传输的数据包都有序列号
2. 设置最大最小stream数
可以通过设置socket的属性来设置最大的stream数,
sctp_initmsg initmsg;
sctp_event_subscribe evnts;
// Specify that a maximum of 5 streams will be available per socket
memset( &initmsg, 0, sizeof(initmsg) );
initmsg.sinit_num_ostreams = 5;
initmsg.sinit_max_instreams = 5;
initmsg.sinit_max_attempts = 4;
if(0 != setsockopt( sock_fd, IPPROTO_SCTP, SCTP_INITMSG,
&initmsg, sizeof(initmsg) ))
3. 同一个stream上数据包的序列
发送端每一个消息发送五次,都在同一个stream 1上发送,
printf("[Client]: Input message content!\n");
std::cin >> message ;
for(int i = 0; i < 5; i++)
{
std::string newMsg = std::move(message + (std::to_string(i)));
printf("[Client]: Send message: %s\n", newMsg.c_str());
sctp_sendmsg(sock_op->socket_fd(), newMsg.c_str(), MAX_BUFFER,
(sockaddr *)&servaddr, sizeof(servaddr), 0, 0, stream, 0, 0);
}
第一次发送5个‘hello’,Client端打印如下,
[Client]: Send message: hello0
[Client]: Send message: hello1
[Client]: Send message: hello2
[Client]: Send message: hello3
[Client]: Send message: hello4
我们再来看一下Sever端的接收情况,同时把每个消息的ssn打印出来,
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello0') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(0)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello1') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(1)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello2') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(2)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello3') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(3)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello4') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(4)
可以看到,每个消息都有一个自己的序列号,保证消息是有序的,
我们继续再往server端发送5个'world',server端的接收消息依旧是有序的,
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world0') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(5)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world1') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(6)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world2') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(7)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world3') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(8)
====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world4') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(9)
4. 如果出现丢包的情况
这里我们没法儿模拟丢包的场景,unix网络编程这本书中描述的,如果一个数据包丢失,也就是如果出现NACK
- 这个stream上的后续的数据包会保存到队列中;
- 其他的stream不受影响;
- 丢失的数据包会进行重传,重传成功后,后续队列中的数据包继续按顺序发送;