Guest User

SCTP sender

a guest
Aug 2nd, 2014
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netinet/sctp.h>
  9. #include <arpa/inet.h>
  10.  
  11. #define PORT 5055
  12.  
  13. int main(void)
  14. {
  15.     int sock;
  16.     int retval;
  17.     int iter;
  18.  
  19.     struct sockaddr_in bind_addr, remote_addr;
  20.     char write_buff[1024];
  21.  
  22.     sock = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
  23.     if (sock == -1) {
  24.         perror("socket()");
  25.         exit(-1);
  26.     }
  27.    
  28.     bind_addr.sin_family = AF_INET;
  29.     bind_addr.sin_port = htons(0);
  30.     bind_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  31.     if (bind_addr.sin_addr.s_addr == INADDR_NONE) {
  32.         perror("inet_addr()");
  33.         exit(-1);
  34.     }
  35.  
  36.     retval = sctp_bindx(sock, (struct sockaddr *) &bind_addr, 1, SCTP_BINDX_ADD_ADDR);
  37.     if (retval != 0) {
  38.         perror("sctp_bindx()");
  39.         exit(-1);
  40.     }
  41.  
  42.     remote_addr.sin_family = AF_INET;
  43.     remote_addr.sin_port = htons(PORT);
  44.     remote_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  45.     if (remote_addr.sin_addr.s_addr == INADDR_NONE) {
  46.         perror("inet_addr()");
  47.         exit(-1);
  48.     }
  49.  
  50.     for(iter = 0; iter < 20; iter++) {
  51.         snprintf(write_buff, sizeof(write_buff), "Message #%d", iter);
  52.         retval = sctp_sendmsg(sock, (void *)write_buff, sizeof(write_buff),
  53.                               (struct sockaddr *) &remote_addr, sizeof(remote_addr),
  54.                               0, 0, 0, 0, 0);
  55.         if (retval < 0) {
  56.             perror("sctp_sendmsg()");
  57.             exit(-1);
  58.         }
  59.  
  60.         sleep(1);
  61.     }
  62.  
  63.     retval = close(sock);
  64.     if (retval != 0) {
  65.         perror("close()");
  66.         exit(-1);
  67.     }
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment