Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the server-side code of SCTP one-to-many mode

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to write server-side code in SCTP one-to-many mode. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The following source code is based on the linux operating system. The one-to-multi-mode server-side code based on SCTP protocol is implemented, which deals not only with user data, but also with notification messages, namely notification messages.

# include # include static void handle_event (void * buf) {struct sctp_assoc_change * sac; struct sctp_send_failed * ssf; struct sctp_paddr_change * spc; struct sctp_remote_error * sre; struct sctp_shutdown_event * sse; union sctp_notification* snp; snp = (sctp_notification*) buf Switch (snp- > sn_header.sn_type) {case SCTP_ASSOC_CHANGE: {sac = & snp- > sn_assoc_change Printf ("assoc_change: state=%hu, error=%hu, instr=%hu outstr=%hu associd=%d\ n", sac- > sac_state, sac- > sac_error, sac- > sac_inbound_streams, sac- > sac_outbound_streams Sac- > sac_assoc_id) Break;} case SCTP_SEND_FAILED: {ssf = & snp- > sn_send_failed Printf ("sendfailed: len=%hu err=%d assoc_i=%d ssf_data=%d\ n", ssf- > ssf_length, ssf- > ssf_error, ssf- > ssf_assoc_id, ssf- > ssf_data [0]); break } case SCTP_PEER_ADDR_CHANGE: {spc = & snp- > sn_paddr_change; struct sockaddr_in * sin = (struct sockaddr_in *) & spc- > spc_aaddr; char addrbuf [INET6 _ ADDRSTRLEN] Inet_ntop (AF_INET, & sin- > sin_addr, addrbuf, INET6_ADDRSTRLEN); printf ("peeraddrchange:% s state=%d, error=%d\ n", addrbuf, spc- > spc_state, spc- > spc_error); break } case SCTP_REMOTE_ERROR: {sre = & snp- > sn_remote_error; printf ("remote_error: err=%hu len=%hu\ n", ntohs (sre- > sre_error), ntohs (sre- > sre_length); break } case SCTP_SHUTDOWN_EVENT: {sse = & snp- > sn_shutdown_event; printf ("shutdown event: assoc_id=%d\ n", sse- > sse_assoc_id); break } default: {printf ("unknown type:% hu\ n", snp- > sn_header.sn_type); break;}} int main (int agrc, char* agrv []) {/ * Create a 1-to-many style SCTP socket. * / int fd =-1; if ((fd = socket (AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0) {perror ("socket"); exit (1);} / * Enable all notifications and events * / struct sctp_event_subscribe event; event.sctp_data_io_event = 1; event.sctp_association_event = 1 Event.sctp_address_event = 1; event.sctp_send_failure_event = 1; event.sctp_peer_error_event = 1; event.sctp_shutdown_event = 1; event.sctp_partial_delivery_event = 1; event.sctp_adaption_layer_event = 1; if (setsockopt (fd, IPPROTO_SCTP, SCTP_EVENTS, & event, sizeof (event))! = 0) {perror ("setevent failed") Exit (1);} / * Configure auto-close timer. * / int timeout = 5; if (setsockopt (fd, IPPROTO_SCTP, SCTP_AUTOCLOSE, & timeout, 4) < 0) {perror ("setsockopt SCTP_AUTOCLOSE"); exit (1);} / * Bind the socket to all local addresses. * / struct sockaddr_in sin; bzero ((char*) & sin, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_port = htons (19000); sin.sin_addr.s_addr = inet_addr ("192.168.10.120") If (bind (fd, (struct sockaddr *) & sin, sizeof (sin)) =-1) {perror ("bind"); exit (1);} / * Enable accepting associations. * / if (listen (fd, 1) < 0) {perror ("listen"); exit (1);} char buffer [256]; int bufferlen = 256; bzero (buffer, bufferlen); struct sockaddr_in clientaddr; int fromlen = sizeof (clientaddr); struct sctp_sndrcvinfo sndrcvinfo; int msg_flag While (true) {int length = sctp_recvmsg (fd, buffer, bufferlen, (struct sockaddr*) & clientaddr, (socklen_t*) & fromlen, & sndrcvinfo, & msg_flag) If (msg_flag & MSG_NOTIFICATION) {printf ("* *\ n"); printf ("Event: notificaiton length=%d\ n", length) Handle_event ((void*) buffer);} else {printf ("* *\ n") Printf ("Event: data event length=%d\ n", length); char addrbuf [100]; inet_ntop (AF_INET, & clientaddr.sin_addr, addrbuf, INET6_ADDRSTRLEN); int port = ntohs (clientaddr.sin_port) Printf ("data from=%s:%d\ n", addrbuf, port); printf ("data=%s\ n", buffer);} if (close (fd) < 0) {perror ("close"); exit (1);} return (0) } after reading the above, do you have any further understanding of how to write server-side code in SCTP one-to-many mode? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report