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
/
socketudp.cpp
executable file
·80 lines (68 loc) · 1.8 KB
/
socketudp.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
#include "socketudp.h"
//SocketUDP::SocketUDP()
//{
// this->domain = AF_INET;
// this->type = SOCK_DGRAM;
// sock_d = -1;
//}
SocketUDP::SocketUDP(int protocol):
Socket(AF_INET, SOCK_DGRAM, protocol)
{
}
void SocketUDP::open(int protocol)
{
this->domain = AF_INET;
this->type = SOCK_DGRAM;
if(sock_d > 0){
qDebug() << "SocketUDP::open: el socket ya ha sido abierto.";
}else{
int code = open_sock(this->domain, this->type, protocol);
if(code != 0){
throw code;
}
}
}
void SocketUDP::bind(SockAddr & addr)
{
int code = ::bind(this->sock_d,(addr.getRawAddress()),addr.length());
if(code < 0){
qDebug() << "Error [" << errno << "]: " << strerror(errno);
throw errno;
}
qDebug() << "bin: socket binded to " << addr.toQString();
}
void SocketUDP::bind(uint32_t *ip, uint16_t port)
{
InetSockAddr addr(ip, port);
this->bind(addr);
}
int SocketUDP::sendTo(const void *message, size_t length, SockAddr & dstaddr)
{
int sent = ::sendto(this->sock_d, message, length, 0, dstaddr.getRawAddress(),dstaddr.length());
if(sent < 0){
qDebug() << "Error[" << errno << "]:" << strerror(errno);
throw errno;
}
qDebug() << "sendTo: " << sent << " bytes sent.";
return sent;
}
int SocketUDP::recvFrom(void *buffer, size_t length, SockAddr &from)
{
unsigned int addr_length = from.length();
int read = ::recvfrom(this->sock_d,buffer, length, 0, from.getRawAddress(), &addr_length);
if(read < 0){
qDebug() << "Error[" << errno << "]:" << strerror(errno);
throw errno;
}
qDebug() << "recvFrom: " << read << "bytes recieved.";
return read;
}
//void SocketUDP::setSockOpt()
//{
//}
//void SocketUDP::getSockOpt()
//{
//}
//void SocketUDP::getSockName()
//{
//}