Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <sys/time.h>
- #include <fcntl.h>
- #define MAX_LEN 20
- static int flag_on = 1;
- static struct sockaddr_in multicast_addr;
- static char message_received[MAX_LEN+1];
- static int msgrecv_len;
- static struct ip_mreq mc_req;
- static char* multicast_ip="224.5.6.7";
- static unsigned short multicast_port=5006;
- static struct sockaddr_in from_addr;
- static unsigned int from_len;
- static char message_to_send[MAX_LEN];
- static unsigned int send_len;
- static unsigned char multicast_ttl=1;
- static struct sockaddr_in multicast_addr;
- int sock; /* The socket file descriptor for our "listening"
- socket */
- int connectlist[5]; /* Array of connected sockets so we know who
- we are talking to */
- fd_set socks; /* Socket file descriptors we want to wake
- up for, using select() */
- int highsock; /* Highest #'d file descriptor, needed for select() */
- void setnonblocking(sock)
- int sock;
- {
- int opts;
- opts = fcntl(sock,F_GETFL);
- if (opts < 0)
- {
- perror("fcntl(F_GETFL)");
- exit(EXIT_FAILURE);
- }
- opts = (opts | O_NONBLOCK);
- if (fcntl(sock,F_SETFL,opts) < 0)
- {
- perror("fcntl(F_SETFL)");
- exit(EXIT_FAILURE);
- }
- return;
- }
- void build_select_list()
- {
- int listnum; /* Current item in connectlist for for loops */
- /* First put together fd_set for select(), which will
- consist of the sock veriable in case a new connection
- is coming in, plus all the sockets we have already
- accepted. */
- /* FD_ZERO() clears out the fd_set called socks, so that
- it doesn't contain any file descriptors. */
- FD_ZERO(&socks);
- /* FD_SET() adds the file descriptor "sock" to the fd_set,
- so that select() will return if a connection comes in
- on that socket (which means you have to do accept(), etc. */
- FD_SET(sock,&socks);
- /* Loops through all the possible connections and adds
- those sockets to the fd_set */
- for (listnum = 0; listnum < 5; listnum++)
- {
- if (connectlist[listnum] != 0)
- {
- FD_SET(connectlist[listnum],&socks);
- if (connectlist[listnum] > highsock)
- highsock = connectlist[listnum];
- }
- }
- }
- void deal_with_data(
- int listnum /* Current item in connectlist for for loops */
- )
- {
- /* set the TTL (time to live/hop count) for the send */
- if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &multicast_ttl, sizeof(multicast_ttl))) < 0)
- {
- perror("setsockopt() failed");
- exit(1);
- }
- memset(&multicast_addr, 0, sizeof(multicast_addr));
- multicast_addr.sin_family = AF_INET;
- multicast_addr.sin_addr.s_addr = inet_addr(multicast_ip);
- multicast_addr.sin_port = htons(multicast_port);
- //printf("Type the message below (Press Enter to send, ctrl-C to quit):\n");
- printf("NOW IN STATE SENDER!\n");
- memset(message_to_send, 0, sizeof(message_to_send));
- //while (fgets(message_to_send, MAX_LEN, stdin))
- //message_to_send="batman";
- strcpy(message_to_send,"Hallo\n\0");
- printf("message to send: %s \n", message_to_send);
- send_len = strlen(message_to_send);
- if ((sendto(sock, message_to_send, send_len, 0,
- (struct sockaddr *) &multicast_addr,
- sizeof(multicast_addr))) != send_len)
- {
- perror("Error in number of bytes");
- exit(1);
- }
- memset(message_to_send, 0, sizeof(message_to_send));
- }
- void read_socks()
- {
- memset(message_received, 0, sizeof(message_received));
- from_len = sizeof(from_addr);
- memset(&from_addr, 0, from_len);
- /* block waiting to receive a packet */
- if ((msgrecv_len = recvfrom(sock, message_received, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0)
- {
- perror("recvfrom() failed");
- //break;
- }
- printf("Received %d bytes from %s: ", msgrecv_len, inet_ntoa(from_addr.sin_addr));
- printf("%s", message_received);
- if(strstr(message_received,"haha\0")!=0)
- {
- printf("wuhuu");
- deal_with_data(1);
- }
- }
- int main (argc, argv)
- int argc;
- char *argv[];
- {
- char *ascport; /* ASCII version of the server port */
- int port; /* The port number after conversion from ascport */
- struct sockaddr_in multicast_addr; /* bind info structure */
- int reuse_addr = 1; /* Used so we can re-bind to our port
- while a previous connection is still
- in TIME_WAIT state. */
- struct timeval timeout; /* Timeout for select */
- int readsocks; /* Number of sockets ready for reading */
- /* Make sure we got a port number as a parameter */
- /* Obtain a file descriptor for our "listening" socket */
- if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
- {
- perror("socket() failed");
- exit(1);
- }
- /* So that we can re-bind to it without TIME_WAIT problems */
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
- sizeof(reuse_addr));
- /* Set socket to non-blocking with our setnonblocking routine */
- setnonblocking(sock);
- /* Get the address information, and bind it to the socket */
- //ascport = argv[1]; /* Read what the user gave us */
- //port = atoport(ascport); /* Use function from sockhelp to
- // convert to an int */
- memset((char *) &multicast_addr, 0, sizeof(multicast_addr));
- multicast_addr.sin_family = AF_INET;
- multicast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- multicast_addr.sin_port = htons(multicast_port);
- /* bind to multicast address to socket */
- if ((bind(sock, (struct sockaddr *) &multicast_addr, sizeof(multicast_addr))) < 0)
- {
- perror("bind() failed");
- exit(1);
- }
- /* construct an IGMP join request structure */
- mc_req.imr_multiaddr.s_addr = inet_addr(multicast_ip);
- mc_req.imr_interface.s_addr = htonl(INADDR_ANY);
- /* send an ADD MEMBERSHIP message via setsockopt */
- if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
- {
- perror("setsockopt() failed");
- exit(1);
- }
- /* Set up queue for incoming connections. */
- listen(sock,5);
- /* Since we start with only one socket, the listening socket,
- it is the highest socket so far. */
- highsock = sock;
- memset((char *) &connectlist, 0, sizeof(connectlist));
- while (1) /* Main server loop - forever */
- {
- build_select_list();
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- /* The first argument to select is the highest file
- descriptor value plus 1. In most cases, you can
- just pass FD_SETSIZE and you'll be fine. */
- /* The second argument to select() is the address of
- the fd_set that contains sockets we're waiting
- to be readable (including the listening socket). */
- /* The third parameter is an fd_set that you want to
- know if you can write on -- this example doesn't
- use it, so it passes 0, or NULL. The fourth parameter
- is sockets you're waiting for out-of-band data for,
- which usually, you're not. */
- /* The last parameter to select() is a time-out of how
- long select() should block. If you want to wait forever
- until something happens on a socket, you'll probably
- want to pass NULL. */
- readsocks = select(highsock+1, &socks, (fd_set *) 0,
- (fd_set *) 0, &timeout);
- /* select() returns the number of sockets that had
- things going on with them -- i.e. they're readable. */
- /* Once select() returns, the original fd_set has been
- modified so it now reflects the state of why select()
- woke up. i.e. If file descriptor 4 was originally in
- the fd_set, and then it became readable, the fd_set
- contains file descriptor 4 in it. */
- if (readsocks < 0)
- {
- perror("select");
- exit(EXIT_FAILURE);
- }
- if (readsocks == 0)
- {
- /* Nothing ready to read, just show that
- we're alive */
- printf(".");
- fflush(stdout);
- }
- else
- read_socks();
- } /* while(1) */
- } /* main */
Advertisement
Add Comment
Please, Sign In to add comment