-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientA.c
154 lines (132 loc) · 3.76 KB
/
clientA.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
file name: clientA.c
coder: Junyu Huang
USC-ID: 8109163913
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#define CENTRAL_PORT_FOR_A "25913" // the port client will be connecting to
#define LOCAL_HOST "127.0.0.1" //IP address of the central
#define MAXDATASIZE 100 // max number of bytes we can get at once
//Get the socket address
void *get_in_addr(struct sockaddr *sa)
{
if(sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in*)sa)->sin_addr); //IPV4
}
return &(((struct sockaddr_in6*)sa)->sin6_addr); //IPV6
}
//To analyze the received results from central
int analyze_result(char *argv[], char *data)
{
char *p = strtok(data," ");
int i = 0;
while(p != NULL)
{
argv[i++] = p;
p = strtok(NULL, " ");
}
return i;
}
int main(int argc, char *argv[])
{
int sockfd, sendbytes,recvbytes,rv;
char recv_buf[MAXDATASIZE],send_buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
char s[INET6_ADDRSTRLEN],argvbuf[MAXDATASIZE];
//make sure the input is a hostname
if(argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//To get the address info
if ((rv = getaddrinfo(LOCAL_HOST, CENTRAL_PORT_FOR_A, &hints, &servinfo)) != 0)
{
printf("getaddrinfo failed\n");
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("clientA: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("clientA: ");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "clientA: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
//clientA bootup finished
printf("The client is up and running\n");
printf("client: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
//clientA sends the input hostname to central
sprintf(send_buf,"%s ", argv[1]);
sendbytes = send(sockfd, send_buf, strlen(send_buf), 0);
if(sendbytes == -1)
{
perror("send");
exit(1);
}
printf("The client sent %sto the Central Server\n",send_buf);
//clientA received the shotrest path and the maping gap from central
recvbytes = recv(sockfd,recv_buf,MAXDATASIZE-1,0);
if(recvbytes == -1)
{
perror("recv");
exit(1);
}
recv_buf[recvbytes] = '\0';
//printf("recvfrom: %s\n",recv_buf);
//Analyze the received data and print them
int spacenum = analyze_result(argv,recv_buf);
if(strcmp(argv[0],"NULL")==0)
{
printf("Found no compatibility for %s and %s\n",argv[1],argv[2]);
}
if(strcmp(argv[0],"NULL")!=0)
{
char *leftover;
float gap = strtod(argv[spacenum-1],NULL);
printf("Found compatibility for %s and %s\n",argv[0],argv[spacenum-2]);
for(int i =0;i<spacenum-1;i++)
{
if(i ==(spacenum-2))
{
printf("<%s>",argv[i]);
}
else
printf("<%s>---",argv[i]);
}
printf("\n");
printf("Compability score: %.2f\n",gap);
}
close(sockfd);
return 0;
}