/*
LINUX,MULTICAST,UDP SERVER
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/shm.h>
/*------------------------------------------------------------------------------------
TR_CODE DESC FRAME_ID MULTICAST_GROUP REAL_PORT
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19711
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19712
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19713
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19714
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19715
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19716
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19717
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19718
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19719
A3012 체결 FID_KOSDAQCHEGYUL 233.37.54.51 19720
------------------------------------------------------------------------------------*/
/*USAGE------------------------------------------------------------------------------------
rcvmulticastdata 233.37.54.51 19711
rcvmulticastdata 233.37.54.51 19712
rcvmulticastdata 233.37.54.51 19713
rcvmulticastdata 233.37.54.51 19714
rcvmulticastdata 233.37.54.51 19715
rcvmulticastdata 233.37.54.51 19716
rcvmulticastdata 233.37.54.51 19717
..
..
..
..
------------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
int send_s, recv_s;
unsigned int yes=1, ttl=32;
struct sockaddr_in mcast_group;
struct ip_mreq mreq;
struct sockaddr_in from;
int rlen, len, rtn;
if (argc != 3) {
printf("usage : %s multicastaddr port &\n", argv[0]);
exit(0);
}
for (;;)
{
//socket for multicast receive
memset((char *)&mcast_group, 0, sizeof(mcast_group));
mcast_group.sin_family = AF_INET;
mcast_group.sin_port = htons(atoi(argv[2]));
mcast_group.sin_addr.s_addr = inet_addr(argv[1]);
if ((recv_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("error: can't create receive socket\n");
exit(0);
}
//multicast group register, and multicast network is defind by root user
mreq.imr_multiaddr = mcast_group.sin_addr;
//mreq.imr_interface.s_addr = htonl(INADDR_ANY);
//"192.178.33.142" is multicast network interface
mreq.imr_interface.s_addr = inet_addr("192.178.33.142");
if (setsockopt(recv_s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
printf("error: add membership\n");
exit(0);
}
//socket re-use option set
if (setsockopt(recv_s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
{
printf("error: so_reuseaddr setsockopt\n");
exit(0);
}
//socket bind
if (bind(recv_s, (struct sockaddr*)&mcast_group, sizeof(mcast_group)) < 0) {
printf("error: bind receive socket\n");
exit(0);
}
for (;;)
{
len = sizeof(from);
if ((rlen = recvfrom(recv_s, rbuf, SZ_MAX, 0, (struct sockaddr *)&from, &len)) < 0)
{
printf("error: recvfrom\n");
exit(0);
}
rbuf[rlen] = '\0';
printf("%s\n", rbuf);
}
close(recv_s);
usleep(1000);
}
return(0);
}