-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientA.cpp
172 lines (147 loc) · 4.31 KB
/
clientA.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <tuple>
#include <sys/types.h>
#include <netdb.h>
using namespace std;
#define ERROR -1
#define localhost "127.0.0.1"
#define TCPA_PORT "25061"
#define TCPB_PORT "26061"
#define Maxs 1000 //max size
#define TCPA_PORT "25061"
struct sockaddr_in clientAadd;
char input[Maxs], output[1000000];
int sockfd;
char username[2048];
void createTCPsocket(){
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//create error happens
if(sockfd == ERROR){
perror("clientA socket TCP error");
exit(1);
}
}
void TCPconnect(){
//from Beej’s guide to network programming
memset(&clientAadd, 0, sizeof clientAadd);
clientAadd.sin_family = AF_INET; //ipv4
clientAadd.sin_addr.s_addr = inet_addr(localhost);
clientAadd.sin_port = htons(25061);
//tcp connect
if((connect(sockfd,(struct sockaddr *)&clientAadd, sizeof clientAadd)) == ERROR){
perror("Connecting Error");
exit(1);
}
}
int main(int argc, char* argv[]){
memset(username, '\0', sizeof username);
strcpy(username, argv[1]); //get the username
createTCPsocket();
TCPconnect();
printf("The client is up and running.\n\n");
if(send(sockfd, username, sizeof username, 0) == ERROR){
perror("client A send error");
exit(1);
}
printf("The client sent \"%s\" to the Central server.\n", username);
//receive the result segment from central
memset(output,'\0',sizeof output);
if(recv(sockfd, output, sizeof output, 0) == ERROR){
perror("client A receive error");
exit(1);
}
//get the states of result: good ? or bad
bool st1 = output[0] == '$'? true : false;
bool st2 = output[2] == '$'? true : false;
bool two_path = output[1] == 'Y'? true : false;
//decode the result
string score = "", state = "", score2 = "";
int i = 3;
vector<string> paths, paths2;
paths.clear();
paths2.clear();
if(st1){
while(i < strlen(output) && output[i] != '*') score += output[i++];
i++;
}
while(i < strlen(output) && output[i] != '#'){
if(output[i] == '+'){
paths.push_back(state);
state = "";
}
else state += output[i];
i++;
}
i++; //skip '#'
if(two_path){
if(st2){
while(i < strlen(output) && output[i] != '*') score2 += output[i++];
i++;
}
while(i < strlen(output)){
if(output[i] == '+'){
paths2.push_back(state);
state = "";
}
else state += output[i];
i++;
}
}
//print the result: first path
string result = "";
if(!st1) cout << "Found no compatibility between " << paths[0] << " and " << paths.back() << endl;
else{
cout << "Found compatibility for " << paths.back() << " and " << paths[0] << ":" << endl;
for(int i = paths.size()-1; ~i; i--){
result += paths[i];
result += " --- ";
}
result.pop_back();
result.pop_back();
result.pop_back();
result.pop_back();
result.pop_back();
cout << result << endl;
cout << "Matching Gap: " << score << endl;
}
//print the result : second path
if(two_path){
if(!st2) cout << "Found no compatibility between " << paths2[0] << " and " << paths2.back() << endl;
else{
cout << "Found compatibility for " << paths2.back() << " and " << paths2[0] << ":" << endl;
result = "";
for(int i = paths2.size()-1; ~i; i--){
result += paths2[i];
result += " --- ";
}
result.pop_back();
result.pop_back();
result.pop_back();
result.pop_back();
result.pop_back();
cout << result << endl;
cout << "Matching Gap: " << score2 << endl;
}
}
close(sockfd);
return 0;
}