Skip to content

Instantly share code, notes, and snippets.

@orleika
Created April 20, 2018 01:58
Show Gist options
  • Save orleika/f4aaa70864ec90f3c204f84f2f600a61 to your computer and use it in GitHub Desktop.
Save orleika/f4aaa70864ec90f3c204f84f2f600a61 to your computer and use it in GitHub Desktop.
some description
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
char buf[2048];
int main(int argc, char *argv[])
{
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0) {
exit(1);
}
{
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
close(sd);
exit(1);
}
}
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(atoi(argv[1]));
localSock.sin_addr.s_addr = INADDR_ANY;
if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock))) {
close(sd);
exit(1);
}
group.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
group.imr_interface.s_addr = inet_addr("127.0.0.1");
if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
close(sd);
exit(1);
}
while (1) {
fflush(stdout);
if(read(sd, buf, sizeof(buf)) < 0) {
close(sd);
exit(1);
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment