-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_capital.c
65 lines (65 loc) · 1.33 KB
/
client_capital.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sock;
// client socket discriptor
int a,b,c,i;
unsigned int len;
char ch[3]="no";
char ch1[3];
struct sockaddr_in client;
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
// client socket is created.
{
perror("socket: ");
exit(-1);
}
client.sin_family=AF_INET;
client.sin_port=htons(10000);
// initializing socket parameters
client.sin_addr.s_addr=INADDR_ANY;
bzero(&client.sin_zero,0);
// appending 8 byte zeroes to 'struct sockaddr_in', to make it equal in size with 'struct sockaddr'.
len=sizeof(struct sockaddr_in);
if((connect(sock,(struct sockaddr *)&client,len))==-1)
//connecting to client
{
perror("connect: ");
exit(-1);
}
while(1)
{
char word[100],capital[100];
printf("Query:\n");
scanf("%s",word);
send(sock,word,sizeof(word),0);
recv(sock,&a,sizeof(a),0);
if(a==1)
{
char ch[100];
recv(sock,ch,sizeof(ch),0);
printf("Answer :%s\n",ch);
}
else
{
printf("NOT FOUND\nENTER CAPITAL\n");
scanf("%s",capital);
send(sock,capital,sizeof(capital),0);
}
printf("\nTo exit...press 'no'\n");
scanf("%s",ch1);
if((i=strcmp(ch,ch1))==0)
{
close(sock);
exit(0);
}
}
}