-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPSocket.h
53 lines (42 loc) · 1.29 KB
/
UDPSocket.h
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: UDPSocket.h
* Author: fsilva
*
* Created on December 17, 2015, 3:08 PM
*/
#ifndef UDPSOCKET_H
#define UDPSOCKET_H
#include <iostream>
#include "Message.h"
#include <cstring>
#include "PB_Eotq.proto3.pb.h"
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/coded_stream.h>
/* includes for socket communication */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h> /* -> for closing socket */
#define MAX_BUFSIZE 548 // 576 - 8 (udp header) - 20 (ipv4 header) = 548
// NOTE: 14 bytes (ethernet header) --> doesn't count for this
class UDPSocket {
public:
UDPSocket();
UDPSocket(const UDPSocket& orig);
virtual ~UDPSocket();
bool createSocket();
void closeSocket();
bool bindSocket(std::string ip_addr, int p);
Message* listenMessage();
bool sendMessage(Message &msg, struct sockaddr_in r_addr, int r_port);
struct sockaddr_in convertIP(std::string &ip);
private:
struct sockaddr_in l_addr, r_addr;
int sock_desc;
char buffer[MAX_BUFSIZE];
};
#endif /* UDPSOCKET_H */