Client Server Using a File that contains the credentials for clients to log-on.
ID: 3881719 • Letter: C
Question
Client
Server
Using a File that contains the credentials for clients to log-on. Server should be able to listen to atleast 4 clients. Make modification to the source code to allow 5 attempt to log-on. File format contains (Passcode, User ID, Company Name, Department, Manager). With a successful log-on, they are greeted with "You have access " along with the users information. Code can be downloaded at box.com/dcc10e
Iinclude #include #include 4 include 6 void error (char *msg) perror(msg); exit (0) 10 11 int main (int argc, char rg) 12 I 13 int. sokd, port.no, ni 14 ruet ockaddr_in serv addr: 15 struct hostent *server; 16 har buffer[256 17 (argcExplanation / Answer
//Server Program "Runnable Program" Accepts multiple Numbers Clients and validate client login on //server You should required Linux platform to run this program.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<sys/types.h>
//server program
//My program
#define BUFLEN 2048
#define PORT 7805
int Validate(char *buf)
{
FILE *fp=NULL;
fp=fopen("client_verify.txt","r");
if(fp==NULL)
{printf("File Read Problem.");exit(0);}
char buffer[1024];
while(!feof(fp))
{
if(feof(fp))
break;
fgets(buffer,1024,fp);
int flag=1,i=0;
for(;;)
{
if(buffer[i]=='' && buf[i]=='')
{return 1;}
else if(buffer[i]=='' || buf[i]=='' || buffer[i]!=buf[i])
{
flag=0;fclose(fp);return 0;
}
}//for loop end //each word and character end
}//while end
return 0;
}
int main()
{
char buf[BUFLEN];
int ser_sockfd,cli_sockfd;
int ser_len,cli_len;
int recv_len;
struct sockaddr_in ser_address;
struct sockaddr_in cli_address;
cli_len=sizeof(cli_address);
unlink("server_socket");
ser_sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(ser_sockfd==-1)
{
printf("Socket not create");
}
//memset((char *)&ser_address,0,sizeof(ser_address));
bzero(&ser_address,sizeof(ser_address));
ser_address.sin_family=AF_INET;
ser_address.sin_addr.s_addr=INADDR_ANY;
ser_address.sin_port=htons(7805);
//ser_len=sizeof(ser_address);
//printf("Sizeod Ser_add-%d",ser_len);
if((bind(ser_sockfd,(struct sockaddr *)&ser_address,sizeof(ser_address)))!=0)
{
printf("Bind Die");
exit(0);
}
listen(ser_sockfd,55);
printf(" Server Waiting ");
fflush(stdout);
while(1)//Server allow to multiple clients log in request with given ip address "LOCAL"
{
if ((recv_len = recvfrom(ser_sockfd, buf, BUFLEN, 0, (struct sockaddr *) &cli_address, &cli_len)) == -1)
{
printf("recvfrom()");exit(1);
}
//Here will checking with passcode and all
//Whose Sender Details
printf("Received packet from %s:%d ", inet_ntoa(cli_address.sin_addr), ntohs(cli_address.sin_port));
printf("FileNameRecv: %s " , buf);
//Validation
if(Validate(buf))
{
printf("You Are Success, %s ",buf);
}
else
printf("Wrong Information ");
}//while End
}
//SERVER PROGRAM END
//Client program Run on Linux same machine
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
//#include<sys/types.h>
//#include<sys/socket.h>
#include<unistd.h>
//#include<sys/un.h>
//IP- "+917387741823 Whats app.
int main()
{
int sockfd;//Socket file discriptor
int len;
struct sockaddr_in cli_address,ser_address;
int result;
char buffer[2000];
int broadcast=1;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
printf("client socket not create");
}
printf("Socket created ");
bzero(&ser_address,sizeof(ser_address));
ser_address.sin_family=AF_INET;
ser_address.sin_addr.s_addr=INADDR_ANY;
ser_address.sin_port=htons(7803);
// sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&ser_address,sizeof(struct sockaddr));
// //file name send done
//If u want to contact me then use IP ADDRESS INFORMATION No.
char Buf[2048];
int flag=0;
while(1)
{
char passcode[100],userid[100],comp_name[100],dept[100],man[100];
printf("Enter Passcode- ");
scanf("%s",passcode);
strcpy(Buf,passcode);
strcat(Buf," ");
printf("Enter User ID- ");
scanf("%s",userid);
strcat(Buf,userid);
strcat(Buf," ");
printf("Enter Company Name- ");
scanf("%s",comp_name);
strcat(Buf,comp_name);
strcat(Buf," ");
printf("Enter Department- ");
scanf("%s",dept);
strcat(Buf,dept);
strcat(Buf," ");
printf("Enter Manager- ");
scanf("%s",man);
strcat(Buf,man);
strcat(Buf,"");
sendto(sockfd,Buf,strlen(Buf),0,(struct sockaddr *)&ser_address,sizeof(struct sockaddr));
}
close(sockfd);
}
//Client program END
//IF You want to login from another machine that will also i provide code if u interested to get that code //with proper working code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.