This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.cpp
executable file
·91 lines (78 loc) · 2.61 KB
/
socket.cpp
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
#include "socket.h"
// ------------------------------------ CONSTRUCTORES ------------------------------------//
Socket::Socket()
{
sock_d = -1;
}
Socket::Socket(int domain, int type, int protocol)
{
int code = open_sock(domain, type, protocol);
if(code != 0){
throw code;
}
}
// ------------------------------------ MÉTODOS PÚBLICOS ------------------------------------//
// open: Función para abrir el socket, sólo se debería llamar en caso
// de haber usado el constructor por defecto.
void Socket::open(int domain, int type, int protocol)
{
if(sock_d > 0){
qDebug() << "Socket::open: el socket ya ha sido abierto.";
}else{
int code = open_sock(domain, type, protocol);
if(code != 0){
throw code;
}
}
}
int Socket::descriptor()
{
return this->sock_d;
}
// close: Cierra el socket.
void Socket::close()
{
if(::close(sock_d) < 0){ // Use "::" for global namespace.
qDebug() << "Socket::close:Error[" << errno << "]: " << strerror(errno);
throw errno; // Could not close socket.
}
qDebug() << "close:socket closed.";
}
//// sendData: Envío de información a través del socket.
//void Socket::sendData(unsigned char *buf, size_t buf_len)
//{
// int data_len = -1;
// struct sockaddr_in * addr;
// addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
// if(addr == NULL){
// qDebug() << "send_frame::Error[malloc] if_out es nulo.\n" ;
// throw 40;
// }
// /* Definiendo los valores de la dirección IP destino */
// addr -> sin_family = AF_INET;
// addr -> sin_port = htons(3213);
// inet_aton("192.168.1.254",&(addr->sin_addr));
// data_len = sendto(sock_d, (const void *)buf, buf_len, 0, (struct sockaddr *)addr, sizeof(struct sockaddr_in));
// if(data_len < 0){
// qDebug() << "send_frame::Error[" << errno << "]: " << strerror(errno);
// free(addr);
// throw 30;
// }
// else{
// qDebug() << "Data sent: " << QString::fromAscii((char*)buf) << "len: " << buf_len;
// free(addr);
// }
//}
// ------------------------------------ MÉTODOS PRIVADOS ------------------------------------//
// open_sock: Método empleado por el construtcor y open();
// Devuelve errno en caso de error o 0 si todo ocurrió normalmente.
int Socket::open_sock(int domain, int type, int protocol)
{
sock_d = socket(domain, type, protocol);
if(sock_d < 0){
qDebug() << "Socket::open_sock::Error[" << errno << "]: " << strerror(errno);
return errno;
}
qDebug() << "socket opened: " << sock_d;
return 0;
}