-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cc
More file actions
130 lines (117 loc) · 3.23 KB
/
Copy pathclient.cc
File metadata and controls
130 lines (117 loc) · 3.23 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
#include "client.h"
Client::Client(string host, int port) {
// setup variables
host_ = host;
port_ = port;
buflen_ = 1024;
buf_ = new char[buflen_+1];
}
Client::~Client() {
}
void Client::run() {
// connect to the server and run echo program
create();
echo();
}
void
Client::create() {
struct sockaddr_in server_addr;
// use DNS to get IP address
struct hostent *hostEntry;
hostEntry = gethostbyname(host_.c_str());
if (!hostEntry) {
cout << "No such host name: " << host_ << endl;
exit(-1);
}
// setup socket address structure
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_);
memcpy(&server_addr.sin_addr, hostEntry->h_addr_list[0], hostEntry->h_length);
// create socket
server_ = socket(PF_INET,SOCK_STREAM,0);
if (!server_) {
perror("socket");
exit(-1);
}
// connect to server
if (connect(server_,(const struct sockaddr *)&server_addr,sizeof(server_addr)) < 0) {
perror("connect");
exit(-1);
}
}
void
Client::close_socket() {
close(server_);
}
void
Client::echo() {
string line;
// loop to handle user interface
while (getline(cin,line)) {
// append a newline
line += "\n";
// send request
bool success = send_request(line);
// break if an error occurred
if (not success)
break;
// get a response
success = get_response();
// break if an error occurred
if (not success)
break;
}
close_socket();
}
bool
Client::send_request(string request) {
// prepare to send request
const char* ptr = request.c_str();
int nleft = request.length();
int nwritten;
// loop to be sure it is all sent
while (nleft) {
if ((nwritten = send(server_, ptr, nleft, 0)) < 0) {
if (errno == EINTR) {
// the socket call was interrupted -- try again
continue;
} else {
// an error occurred, so break out
perror("write");
return false;
}
} else if (nwritten == 0) {
// the socket is closed
return false;
}
nleft -= nwritten;
ptr += nwritten;
}
return true;
}
bool
Client::get_response() {
string response = "";
// read until we get a newline
while (response.find("\n") == string::npos) {
int nread = recv(server_,buf_,1024,0);
if (nread < 0) {
if (errno == EINTR)
// the socket call was interrupted -- try again
continue;
else
// an error occurred, so break out
return "";
} else if (nread == 0) {
// the socket is closed
return "";
}
// be sure to use append in case we have binary data
response.append(buf_,nread);
}
// a better client would cut off anything after the newline and
// save it in a cache
cout << response;
return true;
}