title: server client program in c based on sctp socket
body: <p>I have written this server-client program with sctp protocol.when I run this program in my local machine with loopback IP as the destination this program works fine. But when I run the server and client code in two different remote systems the code does not work.</p>
  
  <p>the sctp_sendmsg function takes too much to send and when sending is successful I do not see any message on the server.</p>
  
  <pre><code>    /*
   * Compile:
   *
   *   gcc sctp.c -o server -lsctp -Wall
   *   ln -s server client
   *
   * Invoke:
   *
   *   ./client
   *   ./server
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;libgen.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netinet/sctp.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define MY_PORT_NUM 21233
  
  static void die(const char *s) {
          perror(s);
          exit(1);
  }
  
  static void server(void) {
          int listen_fd, conn_fd, flags, ret, in;
          struct sctp_sndrcvinfo sndrcvinfo;
          struct sockaddr_in servaddr = {
                  .sin_family = AF_INET,
                  .sin_addr.s_addr = htonl(INADDR_ANY),
                  .sin_port = htons(MY_PORT_NUM),
          };
          struct sockaddr_in client_addr;
          socklen_t clilen=sizeof(client_addr);
          struct sctp_initmsg initmsg = {
                  .sinit_num_ostreams = 5,
                  .sinit_max_instreams = 5,
                  .sinit_max_attempts = 4,
          };
  
          listen_fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
          if (listen_fd &lt; 0)
                  die("socket");
  
          ret = bind(listen_fd, (struct sockaddr *) &amp;servaddr, sizeof(servaddr));
          if (ret &lt; 0)
                  die("bind");
  
  
          ret = listen(listen_fd, initmsg.sinit_max_instreams);
          if (ret &lt; 0)
                  die("listen");
  
          for (;;) {
                  char buffer[1024];
  
                  printf("Waiting for connection\n");
  
                  in = sctp_recvmsg(listen_fd, buffer, sizeof(buffer),(struct sockaddr*)&amp;client_addr,&amp;clilen,0,0);
                  printf("%d\n",in);
                      if (in &gt; 0) {
                              printf("Received data: %s\n", buffer);
                              fflush(stdout);
                      }
          }
          close(listen_fd);
  }
  
  static void client(void) {
          int conn_fd, ret;
          const char *msg = "Hello, Server!";
          struct sockaddr_in servaddr = {
                  .sin_family = AF_INET,
                  .sin_port = htons(MY_PORT_NUM),
                  .sin_addr.s_addr = inet_addr("45.125.222.112"),
          };
  
          conn_fd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
          if (conn_fd &lt; 0)
                  die("socket()");
  
          struct sctp_initmsg initmsg = {
                  .sinit_num_ostreams = 5,
                  .sinit_max_instreams = 5,
                  .sinit_max_attempts = 4,
          };
  
          setsockopt(conn_fd, IPPROTO_SCTP, SCTP_INITMSG, &amp;initmsg, sizeof(initmsg));
          int i;
          for(i=0;i&lt;4;i++){
              printf("before sending\n");
              ret = sctp_sendmsg(conn_fd, (void *) msg, strlen(msg) + 1,(struct sockaddr*)&amp;servaddr,sizeof(struct sockaddr),0,0,0,0,0);
              printf("after sending\n");
              if (ret &lt; 0)
                      printf("failed to send %d\n",i);
              else 
                  printf("success %d\n",i);
  
          }
          close(conn_fd);
  
  }
  
  int main(int argc, char **argv) {
  
          if (strstr(basename(argv[0]), "server"))
                  server();
          else
                  client();
  
          return 0;
  }
  </code></pre>