-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathSend_Structure_for_Authentication_Server
72 lines (62 loc) · 1.53 KB
/
Send_Structure_for_Authentication_Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
struct myStruct{
char name[10];
int userId;
char password[10];
};
int main(){
int sockfd;
struct sockaddr_in server,client;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd==-1){
printf("Socket not created\n");
exit(0);
}else{
printf("Socket Created\n");
}
server.sin_family = AF_INET;
server.sin_port = htons(3050);
server.sin_addr.s_addr = INADDR_ANY;
//INET_ADDR("127.0.0.1")
int x=bind(sockfd, (struct sockaddr *)&server, sizeof(server));
if(x==-1){
printf("bind Failure\n");
exit(0);
}else{
printf("bind Successful\n");
}
listen(sockfd, 5);
int s=sizeof(client);
struct myStruct st[5]={{"nikhil1",1,"nikhil1"},
{"nikhil2",2,"nikhil2"},
{"nikhil3",3,"nikhil3"},
{"nikhil14",4,"nikhil4"},
{"nikhil5",5,"nikhil15"}};
struct myStruct myst;
int fd1=accept(sockfd, (struct sockaddr *)&client, &s);
recv(fd1, &myst, sizeof(myst), 0);
//recvfrom(sockfd,buff,100,0,(struct sockaddr *)&client,&s);
printf("userId received :%d\n",myst.userId);
printf("password received :%s\n",myst.password);
int fl=1;
for(int i=0;i<5;i++){
if(myst.userId==st[i].userId && strcmp(myst.password,st[i].password)==0){
fl=0;
break;
}
}
if(fl==0){
char message[20]="Login Successful\n";
send(fd1, message, sizeof(message), 0);
}else{
char message[20]="Login Failure\n";
send(fd1, message, sizeof(message), 0);
}
close(sockfd);
return 0;
}