-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.class.hpp
107 lines (66 loc) · 1.69 KB
/
connection.class.hpp
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
/****************************************************************************************
* Project 1 in course Computer Communications and Networks, 2014/2015
* Author: Lukáš Rendvanský
* Email: xrendv00@stud.fit.vutbr.cz
* Date: 18.3.2015
* File: connection.class.hpp
* Description: Implementation of connection class - header file.
***************************************************************************************/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <cstring>
#include <cstdlib>
#include "exception.class.hpp"
class Connection {
public:
Connection(long port) {
this->port = port;
}
virtual ~Connection() { }
long GetPort();
int GetSocket();
protected:
long port;
int soc;
socklen_t *sinlen;
struct sockaddr_in socketAddressIn;
};
class ServerConnection: public Connection {
public:
ServerConnection(long port): Connection(port) { }
virtual ~ServerConnection() { }
int GetSocketId();
void CreateSocket();
void Bind();
void Listen();
void Accept();
std::string ReadRequest();
void SendResponse(std::string value);
void Close();
void CloseServer();
private:
int socketId;
};
class ClientConnection: public Connection {
public:
ClientConnection(long port): Connection(port) { }
virtual ~ClientConnection() { }
void SetHostName(std::string value);
std::string GetHostName();
void CreateSocket();
void Connect();
void SendRequest(std::string value);
std::string ReadResponse();
void Close();
private:
struct hostent *hptr;
std::string hostname;
};
#endif /* CONNECTION_H */