-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver12.cpp
73 lines (67 loc) · 1.28 KB
/
server12.cpp
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
73
#include<bits/stdc++.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
using namespace std;
void build(map<char *,char *> &mp)
{
for(int i=0;i<4;i++)
{
char word[30],mean[50];
cout<<endl<<"Enter Question : ";
fgets(word,30,stdin);
cout<<"\nEnter its Answer : ";
fgets(mean,50,stdin);
mp[word]=mean;
}
}
int main()
{
int csock,ssock,len;
sockaddr_in client,server;
if((ssock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Server Socket not Created\n");
exit(-1);
}
len=sizeof(sockaddr_in);
server.sin_family=AF_INET;
server.sin_port=htons(1500);
server.sin_addr.s_addr=inet_addr("127.0.0.1");
bzero(&server.sin_zero,0);
if(bind(ssock,(sockaddr *)&server,len)==-1)
{
perror("Binding Error\n");
exit(-1);
}
if(listen(ssock,5)==-1)
{
perror("Channel Error\n");
exit(-1);
}
map<char *,char *> mp;
build(mp);
if((csock=accept(ssock,(sockaddr *)&client,(socklen_t *)&len))==-1)
{
perror("Server incapable of accepting data\n");
exit(-1);
}
while(1)
{
char w[30],ans[50];
read(csock,w,sizeof(w));
if(mp.find(w)!=mp.end())
strcpy(ans,mp[w]);
else
{
strcpy(ans,"Try Again");
}
write(csock,ans,sizeof(ans));
}
close(csock);
close(ssock);
exit(0);
}