-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_socket.cpp
More file actions
82 lines (70 loc) · 1.93 KB
/
tcp_socket.cpp
File metadata and controls
82 lines (70 loc) · 1.93 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
/**
* @file tcp_socket.cpp
* @author Juraj Holub <xholub40@stud.fit.vutbr.cz>
* @brief Create send and recieve tcp sockets.
* @date October 2019
*/
#include "tcp_socket.h"
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
void TcpSocket::connectServer(std::string serverHostName, int serverPort)
{
dst_addr = "host=\"" + serverHostName+ "\", port=\""+ std::to_string(serverPort) + "\"";
struct hostent* server;
if (!(server = gethostbyname(serverHostName.c_str())))
{
throw std::runtime_error("error: Can not create TCP connection to " + dst_addr);
}
//init server addr
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
in_addr *addr = (struct in_addr *) server->h_addr_list[0];
char *ip = inet_ntoa(*addr);
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0)
{
throw std::runtime_error("error: Can not create TCP connection to " + dst_addr);
}
server_addr.sin_port = htons(serverPort);
//create socket
if ((socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0)
{
close(socket_id);
throw std::runtime_error("error: Can not create TCP connection to " + dst_addr);
}
if (connect(socket_id, (const struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)
{
close(socket_id);
throw std::runtime_error("error: Can not create TCP connection to " + dst_addr);
}
}
void TcpSocket::sendData(std::string data)
{
if (send(socket_id, data.c_str(), data.length(), 0) < 0)
{
close(socket_id);
throw std::runtime_error("error: Can not send data through TCP connection " + dst_addr);
}
}
std::string TcpSocket::recvData()
{
std::string data;
unsigned size = 4096;
char tmp[size];
while (recv(socket_id, tmp, size, 0) > 0)
{
data += tmp;
bzero(tmp, size);
}
return data;
}
void TcpSocket::closeConnection()
{
close(socket_id);
}