- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_socket.cpp
More file actions
Latest commit
148 lines (124 loc) · 3.77 KB
/
Copy pathserver_socket.cpp
File metadata and controls
148 lines (124 loc) · 3.77 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by papraczy on 01/11/2019.
//
#include<stdio.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<zconf.h>
#include<iostream>
#include<thread>
#include<string.h>
usingnamespacestd;
#include"server_socket.h"
#include"DataAnalyser.h"
#include"SafeQueue.h"
#include"Calculator.h"
#definePORT7772
/*
* generates data to be sent over a TCP socket
* */
voidServer_socket::generateData(void)
{
for (uint16_t i = 0; i < DATA_LEN; i++)
{
this->data_arr[i] = i;
}
}
/*
* Create a TCP socket of type IPv4
* */
intServer_socket::create_socket() {
//AF_INET- IPv4, SOCK_STREAM- TCP socket, 0- IP
this->server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->server_socket == -1) {
cout << "Could not create socket";
exit(EXIT_FAILURE);
}
return0;
}
/*
* Bind an IP address and port to the socket
* */
intServer_socket::bind_socket() {
structsockaddr_in address;
//indicate socket address family- IPv4
address.sin_family = AF_INET;
//convert IP address to a binary format
address.sin_port = htons(PORT);
inet_pton(AF_INET, "192.168.1.1", &(address.sin_addr.s_addr));
//Bind
if (bind(this->server_socket, (structsockaddr *) &address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
return0;
}
/*
* Puts the server socket into the listening state,
* accepts the incoming connections and
* starts a thread for data analysis
* */
intServer_socket::listen_accept() {
cout << "listening: " << endl;
if (listen(this->server_socket, 1) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
structsockaddr client_addr; // address of the client
socklen_t client_addr_len = sizeof(client_addr); // length of the address
if((this->new_socket = accept(this->server_socket, &client_addr,
&client_addr_len)) > 0)
{
//client connection established
cout << "Connected" << endl;
this->start_threads(&this->new_socket, this->data_queue);
return0;
}
return0;
}
Server_socket::Server_socket() {
this->data_queue = newSafeQueue();
this->create_socket();
this->bind_socket();
this->listen_accept();
}
voidServer_socket::start_threads(int *new_socket, SafeQueue *data_queue) {
//read data incoming to a client socket
auto data_reader = [](int *new_socket, SafeQueue *data_queue) {
cout << "Receieved this: ";
while (true) {
try
{
//send data
char *buffer = "aaaaaaaaaaaaaaaaaa";
u_int
int n = send(*new_socket, buffer, strlen(buffer), 0);
sleep(1);
//read data
// do stuff
/*long buffer[1024] = {0};
int valread = read(*new_socket, buffer, 512);
//prepare data packet
string data_read = "";
//convert a data packet into a string of hex for the analysis purposes
for (int i = 0; i < valread; i++) {
data_read += Calculator::getHex(int(buffer[i]) & 0xFF);
}
data_queue->push(data_read);*/
}
catch (std::exception const &exc) {
std::cerr << "Exception caught " << exc.what() << "\n";
}
}
};
//start a thead for analysing data
DataAnalyser *data_analyser = newDataAnalyser(data_queue);
thread analyse_data_handler(*data_analyser);
//start a thread for reading data from the client socket
thread read_data_handler(data_reader, &this->new_socket, this->data_queue);
read_data_handler.join();
}