I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server.Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the data stored in a file from the server )first will be served by the server(i.e,)the server should read the file stored in the server itself and send the information in the file to the client and should stop receiving the further request from other clients.
I wrote the coding for that..But its not working properly.Can somebody plz clear the bug and make my program to work in the way i wanted??
server:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
int main()
{
char buf[20]="Hai";
char buffer[20];
int conarr[20],con=0,i,r,count,new_fd,sockfd;
socklen_t sin_size;
struct sockaddr_storage their_addr;
unsigned int sock,connect,len,child_pid,sd;
struct sockaddr_in servaddr,cliaddr;
sock=socket(AF_INET,SOCK_STREAM,0);
sd = socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(4005);
bind(sock,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(sock,5);
len=sizeof(struct sockaddr_in);
while(1)
{
if((connect=accept(sock,(struct sockaddr*)&cliaddr,&len))>0)
{
printf("\nENTER\n");
con++;
printf("con %d",con);
conarr[con]=connect;
for(i=1;i<=con;i++)
{
write(conarr[i],buf,sizeof(buf));
}
}
}
char op[2];
FILE *f;
char pass[100];
//char i;
new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
sin_size = sizeof their_addr;
printf("\n hello");
f = fopen("file.txt", "r");
while (fgets(pass, 100, f) != NULL)
count++;
op[0] = count + 48;
op[1] = '\0';
printf("no of lines:%s", op);
if ((send(new_fd, op, sizeof op, 0)) == -1)
perror("re");
fseek(f, 0, SEEK_SET);
count = 0;
while (fgets(pass, 100, f) != NULL)
{
if ((send(new_fd, pass, 100, 0)) == -1)
perror("l");
}
fclose(f);
}
Client:
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
struct sockaddr_in serv;
struct addrinfo hints;
int r,sd,nsd,f,w,c,s,snd,lis;
char buffer[12]="hello world\n",buff[100]="YES";
char buf[100];
sd = socket(AF_INET,SOCK_STREAM,0);
printf("Socket descriptor:_%d_\n",sd);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=inet_addr("10.142.17.123");
serv.sin_port=htons(4005);
s=sizeof(serv);
c=connect(sd,(void *)&serv,s);
if(c==0)
printf("%d_connected_%s",c,buffer);
else
{
printf("%d_%d_Error in connection\n",c,s);
exit(1);
}
while(1)
{
r = read(sd,buffer,sizeof(buffer));
if(r>0)
{
printf("Server Says :%s\n",buffer);
printf("\n...");
w = write(sd,buffer,sizeof(buffer));
printf("\nSay yes...");
break;
}
}
int count = 0;
if ((recv(sd, buf, sizeof buf, 0)) == -1)
perror("p");
printf("\n The no of lines in file is %s", buf);
count = atoi(buf);
while (count != 0)
{
if ((recv(sd, buff, sizeof buff, 0)) == -1)
perror("k");
printf("\n%s", buff);
count--;
}
}
In simple the server broadcasts a message to multiple clients.Whichever client responds first will receive the the datas in the file stored in the server.Plz help me to make this program work.
Thank u in advance