This repository was archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
119 lines (100 loc) · 2.62 KB
/
Copy pathClient.cpp
File metadata and controls
119 lines (100 loc) · 2.62 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
//
// Created by X G on 2/12/22.
//
/**
* Ref: ECE650 tpc_example and beej's Notebook
*/
#include "Client.h"
Client::Client(const char * hostname, int port) {
this->errorCode = 0;
this->port = port;
this->hostname = hostname;
initHit();
if(getAddress() == -1){
this->errorCode = -1;
return;
}
if(createSocket() == -1){
this->errorCode = -1;
return;
}
if(connectSocket() == -1){
this->errorCode = -1;
return;
}
freeaddrinfo(this->res);
}
void Client::initHit() {
memset(&this->hit, 0, sizeof(this->hit));
this->hit.ai_family = AF_UNSPEC;
this->hit.ai_socktype = SOCK_STREAM;
}
int Client::getAddress() {
if(getaddrinfo(this->hostname, std::to_string(this->port).c_str(), &this->hit, &this->res) != 0) return printError("Error: cannot get address info for host");
return 0;
}
int Client::createSocket() {
this->socket_fd = socket(this->res->ai_family, this->res->ai_socktype, this->res->ai_protocol);
if(this->socket_fd == -1) return printError("Error: cannot create socket");
return 0;
}
int Client::connectSocket() {
if (connect(this->socket_fd, this->res->ai_addr, this->res->ai_addrlen) == -1) return printError("Error: cannot connect to socket");
return 0;
}
int Client::printError(std::string error) const {
std::cerr << "(no-id): " << error << std::endl;
return -1;
}
int Client::tryRecvMessage(char *message, int mode, int fd) {
int numbytes = 0;
if ((numbytes = recv(fd, message, MAX_TCP_LEN - 1, mode)) == -1) {
perror("recv");
return -1;
}
message[numbytes] = '\0';
return numbytes;
}
int Client::trySendMessage(char message[MAX_TCP_LEN], int fd) {
int len;
len = strlen(message);
if (sendall(fd, message, &len) == -1) {
perror("sendall");
printf("We only sent %d bytes because of the error!\n", len);
return -1;
}
return 0;
}
/**
* Static Helper funcion send all message
* @param s socket
* @param buf buffer
* @param len
* @return
*/
int Client::sendall(int s, char *buf, int *len)
{
int total = 0;
int bytesleft = *len;
int n;
while(total < *len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total;
return n==-1?-1:0;
}
void Client::close() const {
::close(this->socket_fd);
}
int Client::getSocketFd() const {
return socket_fd;
}
int Client::getErrorCode() const {
return errorCode;
}
int Client::recvMessage(int fd, void *message, int length) {
return recv(fd, message, length, 0);
}