My problem definition is ,I have to send a message from one node in a network and it has to be broadcasted to all other nodes in the network.The program what I have given below will be running in all the nodes in the network.The same program should be capable of sending(broadcasting) and receiving. (i.e, there is no separate program for server and client like a chat program).I have written a code but I am not clear with how it has to be done.Kindly help me in this.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
void sigchld_handler(int s)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
void *get_in_addr(struct sockaddr *sa)
{
if(sa->sa_family==AF_INET)
{
return &(((struct sockaddr_in *)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
char pass[100];
char pass1[100];
int main()
{
int sockfd,new_fd,numbytes;
struct addrinfo hints,*servinfo,*p;
struct sockaddr_storage their_addr;
socklen_t sin_size;
socklen_t *size;
struct sockaddr *names;
int yes=1,len=0;
struct sigaction sa;
char s[INET6_ADDRSTRLEN],s1[INET6_ADDRSTRLEN];
char name[20];
int rv;
memset(&hints,0,sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;
if((rv=getaddrinfo(NULL,"2001",&hints,&servinfo))==-1)
{
fprintf(stderr,"getaddrinfo:%s",gai_strerror(rv));
return 1;
}
printf("\n \n getaddrinfo:%d",rv);
printf("\n------------------------------");
for(p=servinfo;p!=NULL;p=p->ai_next)
{
if((sockfd=socket(p->ai_family,p->ai_socktype,p->ai_protocol))==-1)
{
perror("server:socket");
continue;
}
printf("\n server socket launched...");
if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&yes,sizeof(int))==-1)
{
perror("setsockopt");
return 0;
}
printf("\n server setsocket option ...");
if(bind(sockfd,p->ai_addr,p->ai_addrlen)==-1)
{
close(sockfd);
perror("server:bind");
continue;
}
printf("\n socket binded....");
break;
}
if(p==NULL)
{
fprintf(stderr,"server:failed");
return 2;
}
if(listen(sockfd,10)==-1)
{
perror("listen");
return 0;
}
sa.sa_handler=sigchld_handler;
sa.sa_flags=SA_RESTART;
if(sigaction(SIGCHLD,&sa,NULL)==-1)
{
perror("sigaction");
exit(0);
}
printf("\n\nlisten to socket :%d",sockfd);
while(1)
{
sin_size=sizeof their_addr;
new_fd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);
printf("\n sockfd=%d new_fd=%d",sockfd,new_fd);
if(new_fd==-1)
{
perror("accept");
continue;
}
if((rv=getpeername(new_fd,names,&sin_size))==-1)
perror("r");
inet_ntop(their_addr.ss_family,names,s1,sizeof s1);
printf("\n Peer name:%s",s);
inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s,sizeof s);
printf("\n\nserver:connected %s",s);
if(!fork())
{
while(1)
{
//printf("\n enter the text to the client:");
//gets(pass);
len=strlen("CAN_U");
if((send(new_fd,"CAN_U",len+1,0))==-1)
perror("error");
if((strcmp(pass,"bye"))==0)
{
close(new_fd);
break;}
pass[0]='\0';
if(recv(new_fd,name,sizeof name,0)==-1)
perror("error");
if((strcmp("CAN_U",name))==0)
{
if(send(sockfd,"YES",strlen("YES"),0)==-1)
perror("error5");
printf("\nSuccess") ;
}
}
close(new_fd);
}
close(new_fd);
}
return 0;
}
Plz help me in this.