example/sctp-client.cc
author Hajime Tazaki <tazaki@sfc.wide.ad.jp>
Mon, 21 Nov 2016 16:01:12 +0900
changeset 772 274220a5a949
permissions -rw-r--r--
follow up fix for waf-1.8 upgrade
     1 // 
     2 // libstcp1-dev is needed
     3 // 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <string.h>        /* for memset */
     7 #include <unistd.h>        /* for memset */
     8 #include <sys/socket.h>
     9 #include <sys/types.h>
    10 #include <arpa/inet.h>
    11 #include <netinet/in.h>
    12 #include <netinet/sctp.h>
    13 
    14 int
    15 main (int argc, char **argv)
    16 {
    17   int connect_sock, stat, port, slen, i, flags;
    18   struct sctp_initmsg initmsg;
    19   struct sockaddr_in server_addr;
    20   struct sctp_event_subscribe s_events;
    21   struct sctp_status s_status;
    22   struct sctp_sndrcvinfo s_sndrcvinfo;
    23   char buffer[1024];
    24 
    25   port = 3007;
    26 
    27   connect_sock = socket (AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    28   memset (&initmsg, 0, sizeof (initmsg));
    29   initmsg.sinit_num_ostreams = 3;          // Number of Output Stream
    30   initmsg.sinit_max_instreams = 3;      // Number of Input Stream
    31   initmsg.sinit_max_attempts = 4;
    32   stat = setsockopt (connect_sock, IPPROTO_SCTP, SCTP_INITMSG,
    33                      &initmsg, sizeof (initmsg) );
    34   if (stat < 0)
    35     {
    36       perror ("setsockopt error");
    37       exit (-1);
    38     }
    39 
    40   memset (&server_addr, 0, sizeof (server_addr));
    41   server_addr.sin_family = AF_INET;
    42   server_addr.sin_port = htons (port);
    43   server_addr.sin_addr.s_addr = inet_addr (argv[1]);
    44 
    45   stat = connect (connect_sock, (struct sockaddr *)&server_addr, 
    46                   sizeof (server_addr) );
    47   if (stat < 0)
    48     {
    49       perror ("connect error");
    50       exit (-1);
    51     }
    52 
    53   memset (&s_events, 0, sizeof (s_events));
    54   s_events.sctp_data_io_event = 1;
    55   stat = setsockopt (connect_sock, SOL_SCTP, SCTP_EVENTS,
    56                      (const void *)&s_events, sizeof (s_events));
    57   if (stat < 0)
    58     {
    59       perror ("event error");
    60       exit (-1);
    61     }
    62 
    63   slen = sizeof (s_status);
    64   stat = getsockopt (connect_sock, SOL_SCTP, SCTP_STATUS,
    65                      (void *)&s_status, (socklen_t *)&slen);
    66 
    67   printf ("assoc id  = %d\n", s_status.sstat_assoc_id );
    68   printf ("state     = %d\n", s_status.sstat_state );
    69   printf ("instrms   = %d\n", s_status.sstat_instrms );
    70   printf ("outstrms  = %d\n", s_status.sstat_outstrms );
    71 
    72 
    73   for (i = 0 ; i < 100 ; i++)
    74     {
    75       stat = sctp_recvmsg (connect_sock, (void *)buffer, sizeof (buffer),
    76                            (struct sockaddr *)NULL, 0, &s_sndrcvinfo, &flags);
    77       printf ("stat = %d\n", stat);
    78       if (stat > 0)
    79         {
    80           buffer[stat] = 0;
    81           printf ("(Stream %d) %s\n", s_sndrcvinfo.sinfo_stream, buffer);
    82         }
    83     }
    84   /* Close our socket and exit */
    85   close (connect_sock);
    86   return 0;
    87 }
    88 
    89