example/sctp-server.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>
     7 #include <unistd.h>
     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 void
    15 echo_main (int sock)
    16 {
    17   int stat;
    18   char buffer[1024];
    19 
    20   printf ("sock: %d\n", sock);
    21 
    22   // Stream No.0
    23   sprintf (buffer, "This is a test of stream 0");
    24   stat = sctp_sendmsg (sock, buffer, (size_t)strlen (buffer),
    25                        NULL, 0, 0, 0, 0, 0, 0);
    26 
    27   // Stream No.1
    28   sprintf (buffer, "This is a test of stream 1");
    29   stat = sctp_sendmsg (sock, buffer, (size_t)strlen (buffer),
    30                        NULL, 0, 0, 0, 1, 0, 0);
    31 }
    32 
    33 int
    34 main (int argc, char **argv)
    35 {
    36   int sock_listen, sock_server, stat;
    37   struct sockaddr_in server_addr;
    38   struct sctp_initmsg s_initmsg;
    39   int echo_port;
    40   int i = 0;
    41 
    42   echo_port = 3007;
    43 
    44   sock_listen = socket (AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    45 
    46   memset (&server_addr, 0, sizeof(server_addr));
    47   server_addr.sin_family = AF_INET;
    48   server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
    49   server_addr.sin_port = htons (echo_port);
    50 
    51   stat = bind (sock_listen, (struct sockaddr *)&server_addr, sizeof(server_addr));
    52 
    53   // SCTP parameter
    54   memset (&s_initmsg, 0, sizeof(s_initmsg));
    55   s_initmsg.sinit_num_ostreams = 5;
    56   s_initmsg.sinit_max_instreams = 5;
    57   s_initmsg.sinit_max_attempts = 5;
    58 
    59   stat = setsockopt (sock_listen, IPPROTO_SCTP, SCTP_INITMSG,
    60                      &s_initmsg, sizeof(s_initmsg));
    61   if (stat < 0)
    62     {
    63       perror ("Socket Option error");
    64       exit (-1);
    65     }
    66 
    67   listen (sock_listen, 5);
    68   while (1)
    69     {
    70       printf ("SCTP server accepting\n");
    71       sock_server = accept (sock_listen, (struct sockaddr *)NULL, (socklen_t *)NULL);
    72       if (sock_server == -1)
    73         {
    74           perror ("accept");
    75           exit (-1);
    76         }
    77 
    78       for (i = 0 ; i < 100 ; i++)
    79         {
    80           echo_main (sock_server);
    81         }
    82     }
    83 
    84   close (sock_listen);
    85   return 0;
    86 }
    87