-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPSocket.hpp
150 lines (128 loc) · 3.63 KB
/
TCPSocket.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
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
145
146
147
148
149
150
#ifndef TCPSOCKET_HPP
#define TCPSOCKET_HPP
#include <iostream>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <vector>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#define NC "\e[0m"
#define RED "\033[31m"
#define GRN "\e[0;32m"
#define MAG "\e[0;35m"
#define CYN "\e[0;36m"
#include "PollFd.hpp"
class TCPSocket
{
public:
TCPSocket() {;}
TCPSocket(uint16_t port) : _port(port)
{
nbytes = 0;
_socketFd = socket(AF_INET, SOCK_STREAM, 0); /*1*/
if (_socketFd < 0)
throw TCPSocketException("while creating");
_address.sin_family = AF_INET;
_address.sin_addr.s_addr = INADDR_ANY;
_address.sin_port = htons(_port);
memset(_address.sin_zero, '\0', sizeof _address.sin_zero);
}
~TCPSocket() {;}
TCPSocket &operator=(TCPSocket const& src)
{
_socketFd = src._socketFd;
_address = src._address;
addrlen = src.addrlen;
_port = src._port;
return (*this);
}
void setToNonBlocking()
{
fcntl(_socketFd, F_SETFL, O_NONBLOCK); //server socket must be set to non-blocking
}
void socketBind() /*2*/
{
if (bind(_socketFd, (struct sockaddr *)&_address, sizeof(_address)) < 0)
throw TCPSocketException("while binding");
}
void socketListen()
{
if (listen(_socketFd, 10) < 0)
throw TCPSocketException("while listening");
std::cout << "Server listening on port: " << _port << std::endl << std::endl;
}
int socketAccept()
{
int addrlen = sizeof(_address);
int new_socket;
if ((new_socket = accept(_socketFd, (struct sockaddr *)&_address, (socklen_t *)&addrlen)) < 0)
throw TCPSocketException("while accepting");
return (new_socket);
}
int socketSend(int fd, std::vector<unsigned char> answer)
{
int sentBytes(0);
if ((sentBytes = send(fd, &answer[0], answer.size(), 0)) < 0)
throw TCPSocketException("while sending");
return (sentBytes);
}
std::vector<unsigned char> socketRecv(int i, PollFd &objectPoll)
{
std::vector<unsigned char> buffer(10000);
nbytes = recv(objectPoll.getPfd()[i].fd, &buffer[0], sizeof(buffer), MSG_DONTWAIT);
//std::cout << "nbytes = " << nbytes << "buffer.size() = " << buffer.size() << std::endl;
if (nbytes <= 0)
{
/*if (nbytes == 0)
std::cout << NC << "Socket " << objectPoll.getPfd()[i].fd << " hung up -------------------------------------------\n";
else*/
if (nbytes != 0)
throw TCPSocketException("while receiving");
close(objectPoll.getPfd()[i].fd);
objectPoll.deleteFd(i);
}
return (buffer);
}
/*GETTERS*/
struct sockaddr_in const& getAddress() const
{
return (_address);
}
int const& getSocketFd() const
{
return (_socketFd);
}
int getAddrlen()
{
return (addrlen);
}
private:
int _socketFd;
struct sockaddr_in _address;
int addrlen;
uint16_t _port;
public:
size_t nbytes;
class TCPSocketException: public std::exception
{
public:
TCPSocketException(std::string const& errorM) throw() : _errorM(errorM) {;}
virtual ~TCPSocketException() throw(){;}
virtual const char* what() const throw()
{
perror("");
return (_errorM.c_str());
}
private:
std::string _errorM;
};
};
/*1. Creation of a TCP socket with the following arguments:
* - Socket family: AF_INET for a socket IPv4
* - Socket type: SOCK_STREAM for TCP
* - Socket protocal: associated with SOCK_STREAM, so set to 0.
*/
/*2. When a socket is created with socket(), it exists in a name space but has no address assigned to it. bind() assigns the address specified by the sockaddr_in structure to the socket. This results in assigning a transport address to the socket (using a port).*/
#endif