-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionManager.cpp
More file actions
58 lines (40 loc) · 1.31 KB
/
ConnectionManager.cpp
File metadata and controls
58 lines (40 loc) · 1.31 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
#include <arpa/inet.h>
#include <zconf.h>
#include <string>
#include <fstream>
#include "ConnectionManager.h"
#include <iostream>
ConnectionManager::ConnectionManager(std::string ip, int port) {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, ip.c_str(), &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
}
read(sock, buffer, 1024*10);
std::cout << buffer << std::endl;
usleep(500000);
identify();
}
void ConnectionManager::readFromSocket() {
valread = read(sock, buffer, 1024*10);
buffer[valread] = '\0';
remove("myFile.txt");
std::ofstream jsonFile("myFile.txt", std::ofstream::trunc);
jsonFile << buffer;
jsonFile.close();
}
void ConnectionManager::identify() {
std::string identityString = "{\"identity\": \"queryExecutor\", \"command\": \"identifying\"}";
send(sock, identityString.c_str(), identityString.size(), 0);
}