-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTcpClient.cpp
More file actions
32 lines (24 loc) · 811 Bytes
/
TcpClient.cpp
File metadata and controls
32 lines (24 loc) · 811 Bytes
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
#include "TcpClient.h"
TcpClient::TcpClient() {
this->server_port = 5000;
this->server_ip = "127.0.0.1";
this->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&this->addr, 0, sizeof(struct sockaddr_in));
this->addr.sin_family = AF_INET;
this->addr.sin_port = htons(this->server_port);
inet_pton(AF_INET, this->server_ip.c_str(), &this->addr.sin_addr);
connect(this->sock, (const struct sockaddr *)&this->addr, sizeof(struct sockaddr_in));
}
TcpClient::~TcpClient() {
shutdown(sock, SHUT_RDWR);
close(sock);
}
void TcpClient::sendRequest(std::string request) {
if (send(this->sock, request.c_str(), request.size(), 0) < 0) {
perror("Send failed : ");
return;
}
}
std::string TcpClient::getResponse() {
return httpRead(sock);
}