-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
130 lines (115 loc) · 3.41 KB
/
Copy pathclient.cpp
File metadata and controls
130 lines (115 loc) · 3.41 KB
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
//
// client.cpp
// ee450
//
// Created by Xin Huang on 2018/3/18.
// Copyright © 2018年 Xin Huang. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <signal.h>
#include <iostream>
using namespace std;
#define APORT 21164
#define AWSPORT 24164
#define TCPPORT_CLIENT 25164
#define MAX_LENGTH 100
#define QUEUE_LIMIT 10
const char* localhost = "127.0.0.1";
#define fill '@'
#define BUF_SIZE 1000
#define LONG_BUF 3*sizeof(long int)*sizeof(long int)
/*
* Encoding a word so that we sent the full buffer
* str: string to be encoded.
*/
void encode(char* str){
int len = strlen(str);
for(int i = len; i < BUF_SIZE; i++){
*(str+i) = fill;
}
}
/*
* Encoding a word so that we sent the full buffer
* str: string to be decoded.
*/
void decode(char* str){
int start = 0;
while(str[start]!=fill){
start++;
if(start == BUF_SIZE)
break;
}
for(int i = start; i < BUF_SIZE; i++){
*(str+i)='\0';
}
}
int main(int argc, char const *argv[]){
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = PF_INET;
serv_addr.sin_addr.s_addr = inet_addr(localhost);
serv_addr.sin_port = TCPPORT_CLIENT;
char bufSendToServ[BUF_SIZE] ;
char fctBuffer[BUF_SIZE];
char bufRecvFromServ[BUF_SIZE] ;
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
//create a tcp client side socket.
cout<<"The client is up and running."<<endl;
bool flag = true;
while(flag){
int s = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(s!=-1)
break;
}
memset(fctBuffer,'\0',sizeof(bufSendToServ));
strcpy(fctBuffer, argv[1]);
encode(fctBuffer);
send(sock, fctBuffer, sizeof(fctBuffer), 0);
string query = "";
for(int i = 2; i < argc-1; i++){
string str(argv[i]);
query+=str+' ';
}
query+=argv[argc-1];
//build up query
memset(bufSendToServ,0,sizeof(bufSendToServ));
strcpy(bufRecvFromServ, query.c_str());
encode(bufRecvFromServ);
bufSendToServ[strlen(bufSendToServ)]='\0';
send(sock, bufRecvFromServ,sizeof(bufRecvFromServ), 0);
//send the query to the server.
cout<< "The client sent < "<<query<<" > and < "<<argv[1]<<" > to AWS"<<endl;
//end of phase 1
int found;
memset(bufRecvFromServ,'\0',sizeof(bufRecvFromServ));
int len = recv(sock,bufRecvFromServ, sizeof(bufRecvFromServ), 0);
decode(bufRecvFromServ);
found = atoi(bufRecvFromServ);
//receive number of result
if(found == 0){
cout<<"Found no matches for < "<<query <<" >:"<<endl;
}
else if(found==1){
memset(bufRecvFromServ,'\0',sizeof(bufRecvFromServ));
int len = recv(sock, bufRecvFromServ, sizeof(bufRecvFromServ), 0);
cout<<"Found a match for < "<<query <<" >:"<<endl;
decode(bufRecvFromServ);
cout<<"< "<<bufRecvFromServ<<" >"<<endl;
}
else{//no need for this proj, but left to further use.
cout<<"Found < "<<found<<" > matches for < "<<query <<" >:"<<endl;
for(int i = 0; i < found; i++){
memset(bufRecvFromServ,'\0',sizeof(bufRecvFromServ));
int len = recv(sock, bufRecvFromServ, sizeof(bufRecvFromServ), 0);
decode(bufRecvFromServ);
cout<<"< "<<bufRecvFromServ<<" >"<<endl;
}
}
close(sock);
return 0;
}