Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/sctp.h>
- #include <arpa/inet.h>
- #include <strings.h>
- #define PORT 5055
- int main(void)
- {
- int sock;
- int retval;
- char read_buff[1024];
- struct sockaddr_in bind_addr;
- struct sctp_sndrcvinfo sinfo;
- struct sctp_event_subscribe events;
- bzero(&sinfo, sizeof(sinfo));
- sock = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
- if (sock == -1) {
- perror("socket()");
- exit(-1);
- }
- bind_addr.sin_family = AF_INET;
- bind_addr.sin_port = htons(PORT);
- bind_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- if (bind_addr.sin_addr.s_addr == INADDR_NONE) {
- perror("inet_addr()");
- exit(-1);
- }
- retval = sctp_bindx(sock, (struct sockaddr *) &bind_addr, 1, SCTP_BINDX_ADD_ADDR);
- if (retval != 0) {
- perror("sctp_bindx()");
- exit(-1);
- }
- retval = listen(sock, 5);
- if (retval != 0) {
- perror("listen()");
- exit(-1);
- }
- events.sctp_data_io_event = 1;
- if ( setsockopt(sock, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0 ) {
- perror("setsockopt()");
- exit(-1);
- }
- size_t count = 20;
- int msg_flags = 0;
- while(count != 0) {
- retval = sctp_recvmsg(sock, (void *)read_buff, sizeof(read_buff),
- NULL, NULL, &sinfo, &msg_flags);
- if (retval < 0) {
- perror("sctp_recvmsg()");
- exit(-1);
- }
- printf("Got message! Assoc_id: 0x%X, msg: %s\n", sinfo.sinfo_assoc_id, read_buff);
- if (retval == 0) {
- break;
- }
- count--;
- }
- retval = close(sock);
- if (retval != 0) {
- perror("close()");
- exit(-1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment