-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_client.cpp
161 lines (146 loc) · 4.34 KB
/
file_client.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
#include <iostream>
#include <sstream>
#include <fstream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#define MAXLINE 4096
using namespace std;
string genPutMsg(string msg)
{
return "put" + to_string(msg.length()) + "_" + msg + " ";
}
string genGetMsg(string msg)
{
return "get" + to_string(msg.length()) + "_" + msg + " ";
}
int main(int argc, char **argv)
{
int sockfd, n;
struct sockaddr_in servaddr;
if (argc < 2)
{
// printf("usage: ./client <ipaddress>\n");
cout << "IP address required!" << endl;
return 0;
}
if (argc > 2)
{
cout << "too many parameters!" << endl;
return 0;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n", argv[1]);
return 0;
}
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
while (true)
{
char op;
cout << "请输入您要进行的操作,1表示查询,2表示插入,3表示退出:";
cin >> op;
if (op != '1' && op != '2' && op != '3')
{
cout << "请输入1,2或3作为您想进行的操作";
continue;
}
if (op == '3')
{
cout << "exiting..." << endl;
char a[6] = "exit ";
if (n = send(sockfd, a, 5, 0) < 0)
//if (n = write(sockfd, a, 4) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
break;
}
else if (op == '2')
{
string path;
cout << "Enter the file name you are willing to handle: ";
cin >> path;
fstream f_File(path);
if (f_File.peek() == EOF)
{
cout << "file not exist";
}
string line;
while (getline(f_File, line))
{
string sendline = genPutMsg(line);
if (send(sockfd, sendline.c_str(), sendline.length(), 0) < 0)
// if (write(sockfd, send_Msg.c_str(), send_Msg.length()) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
cout << "input success" << endl;
}
}
else
{
char buff[MAXLINE] = {0};
string path;
cout << "Enter the file name you are willing to handle: ";
cin >> path;
fstream f_File(path);
if (f_File.peek() == EOF)
{
cout << "file not exist";
}
string line;
while (getline(f_File, line))
{
bzero(buff, sizeof(buff));
string sendline = genGetMsg(line);
if (send(sockfd, sendline.c_str(), sendline.length(), 0) < 0)
// if (write(sockfd, send_Msg.c_str(), send_Msg.length()) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
if (recv(sockfd, buff, MAXLINE, 0) < 0)
{
printf("receive msg error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
if (strcmp(buff, "fail") == 0)
cout << "No matching" << endl;
else
cout << "The result is " << buff << endl;
}
}
}
cout << "exiting..." << endl;
char a[6] = "exit ";
if (n = send(sockfd, a, 5, 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}
close(sockfd);
return 0;
}