-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketDatagrama.cpp
More file actions
executable file
·106 lines (87 loc) · 3.01 KB
/
SocketDatagrama.cpp
File metadata and controls
executable file
·106 lines (87 loc) · 3.01 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
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <strings.h>
#include <unistd.h>
#include "SocketDatagrama.h"
#include "PaqueteDatagrama.h"
using namespace std;
SocketDatagrama::SocketDatagrama(int puerto)
{
s = socket(AF_INET, SOCK_DGRAM, 0);
bzero((char *)&direccionLocal, sizeof(direccionLocal));
bzero((char *)&direccionForanea, sizeof(direccionForanea));
direccionLocal.sin_family = AF_INET;
direccionLocal.sin_addr.s_addr = INADDR_ANY;
direccionLocal.sin_port = htons(puerto);
bind(s, (struct sockaddr *)&direccionLocal, sizeof(direccionLocal));
timeout = false;
}
SocketDatagrama::~SocketDatagrama()//Recibe un paquete tipo datagrama proveniente de este socket
{
close(s);
// cout << "Se elimino el socket" << endl;
}
int SocketDatagrama::recibe(PaqueteDatagrama & p) //Envía un paquete tipo datagrama desde este socket
{
socklen_t len = sizeof(direccionForanea);
unsigned char direccion[16];
int retorno = recvfrom(s, p.obtieneDatos(), p.obtieneLongitud(), 0, (struct sockaddr*)&direccionForanea, &len);
p.inicializaPuerto(ntohs(direccionForanea.sin_port));
p.inicializaIp(inet_ntoa(direccionForanea.sin_addr));
//cout << "Mensaje recibido desde: " << inet_ntoa(direccionForanea.sin_addr) << endl;
return retorno;
}
int SocketDatagrama::envia(PaqueteDatagrama & p)
{
direccionForanea.sin_family = AF_INET;
direccionForanea.sin_addr.s_addr = inet_addr(p.obtieneDireccion());
direccionForanea.sin_port = htons(p.obtienePuerto());
int retorno = sendto(s, p.obtieneDatos(), p.obtieneLongitud(), 0, (struct sockaddr*)&direccionForanea, sizeof(direccionForanea));
return retorno;
}
void SocketDatagrama::setTimeout(time_t seg, suseconds_t microseg)
{
timeout = true;
tiempofuera.tv_sec = seg;
tiempofuera.tv_usec = microseg;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tiempofuera, sizeof(tiempofuera));
}
void SocketDatagrama::unsetTimeout()
{
timeout = false;
tiempofuera.tv_sec = 0;
tiempofuera.tv_usec = 0;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tiempofuera, sizeof(tiempofuera));
}
int SocketDatagrama::recibeTimeout(PaqueteDatagrama & p)
{
socklen_t len = sizeof(direccionForanea);
unsigned char direccion[16];
struct timeval t0, t1, res;
gettimeofday(&t0,NULL);
int retorno = recvfrom(s, p.obtieneDatos(), p.obtieneLongitud(), 0, (struct sockaddr*)&direccionForanea, &len);
if (retorno < 0)
{
if (errno != EWOULDBLOCK)
cout << "Error en recvfrom" << endl;
}
else
{
gettimeofday(&t1,NULL);
timersub(&t1,&t0,&res);
p.inicializaPuerto(ntohs(direccionForanea.sin_port));
p.inicializaIp(inet_ntoa(direccionForanea.sin_addr));
//cout << "Mensaje recibido desde: " << inet_ntoa(direccionForanea.sin_addr) << endl;
//cout << "Tiempo de respuesta: " << res.tv_sec << " segundos " << res.tv_usec << " microsegundos" << endl;
}
return retorno;
}
int SocketDatagrama::setBroadcast(){
int yes=1, retorno;
retorno = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int));
return retorno;
}