- 论坛徽章:
- 0
|
目前的进展是这样的:
通过设置
setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS,
&evnts, sizeof(evnts));
确实可以监测到对端的一些异常
比如,服务器与客户端建立连接后,客户端的进程被kill或Ctrl C 退出后,客户端会给服务端发送shutdown消息,服务端可以识别这些信息,并进行处理
但这并不能完全解决我的问题,我的问题是,建立连接后,有一方断电,它是不会发shutdown消息的,只是hearbeat ack消息收不到而已,可是 我现在还不知道如何捕获这一信息.不知sctp有无提供这样的接口,相信应该是有的,只是,没有发现...
我的程序:
int
main(int argc, char **argv)
{
int sock_fd,msg_flags;
char readbuf[200];
struct sockaddr_in cliaddr;
struct sctp_sndrcvinfo sri;
struct sctp_event_subscribe evnts;
socklen_t len;
size_t rd_sz;
struct sockaddr_in *paddr, a;
int port = 2905;
unsigned char flag = 1;
paddr = &a;
paddr- >sin_family = AF_INET;
paddr- >sin_port = htons(port);
paddr- >sin_addr.s_addr = inet_addr( "192.168.10.210 ");
sock_fd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
if(fcntl(sock_fd,F_SETFL,O_NONBLOCK) <0)
{
printf( "\r\n PID_UP:: fcntl:O_NONBLOCK error\n ");
return -1;
}
if (-1 == bind(sock_fd, (struct sockaddr*)paddr, sizeof(struct sockaddr))) {
printf( "ERRNO:%d\n ", errno);
perror( "E_bind_Fail ");
return -1;
}
bzero(&evnts, sizeof(evnts));
evnts.sctp_data_io_event = 1;
/* end mod_serv07 */
evnts.sctp_association_event = 1;
evnts.sctp_address_event = 1;
evnts.sctp_send_failure_event = 1;
evnts.sctp_peer_error_event = 1;
evnts.sctp_shutdown_event = 1;
evnts.sctp_partial_delivery_event = 1;
evnts.sctp_adaption_layer_event = 1;
setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS,
&evnts, sizeof(evnts));
listen(sock_fd, 10);
printf( "listen.............\n ");
for ( ; ; ) {
len = sizeof(struct sockaddr_in);
//printf( "=============\n ");
//print_time_info();
rd_sz = sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf),
(struct sockaddr *)&cliaddr, &len,
&sri,&msg_flags);
if(msg_flags & MSG_NOTIFICATION) {
printf( "msg_flags (%d),MSG_NOTIFICATION(%d)\n ",msg_flags,MSG_NOTIFICATION);
print_notification(readbuf);
continue;
}
sctp_sendmsg(sock_fd, readbuf, rd_sz,
(struct sockaddr *)&cliaddr, len,
sri.sinfo_ppid,
sri.sinfo_flags,
sri.sinfo_stream,
0, 0);
}
} |
|