Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <stdio.h>
- int main (int argc, char * argv[])
- {
- int sfd;
- char buf[512];
- struct sockaddr_in localAddress, serverAddress;
- struct ip_mreqn multicast;
- socklen_t server_len = sizeof(serverAddress);
- sfd = socket (PF_INET, SOCK_DGRAM, 0);
- localAddress.sin_port = htons(7890);
- localAddress.sin_family = AF_INET;
- localAddress.sin_addr.s_addr = INADDR_ANY;
- inet_aton("228.5.6.7", &multicast.imr_multiaddr);
- multicast.imr_address.s_addr = INADDR_ANY;
- multicast.imr_ifindex = 0;
- setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &multicast, sizeof(multicast));
- if (bind (sfd, (struct sockaddr*)&localAddress, sizeof(localAddress)) < 0)
- {
- printf("Erro no bind.");
- return 1;
- }
- while (1)
- {
- recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&serverAddress, &server_len);
- printf("received: %s\n", buf);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment