-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_util.h
72 lines (65 loc) · 1.4 KB
/
socket_util.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef __SOCKET_UTIL__
#define __SOCKET_UTIL
#ifdef _WIN32
#include<winsock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#endif
/*
* error codes
*/
#define SU_SUCCESS 0
#define SU_ERROR -1
/*
* 16 * 1024
*/
#define MAX_SEND_SIZE 16384
/*
* public functions
*/
int init_socket_util();
void clean_socket_util();
int create_udp_socket(int ip_addr, int port);
int create_udp_socket_nobind(int port);
int send_udp_message(int sock_fd, void * message, int message_len, int destination_ip, int destination_port);
int receive_udp_message(int sock_fd, void * message, int message_len, int & source_ip, int & source_port);
int close_udp_socket(int sock_fd);
// socket utility class
class socket_utility {
private:
WSADATA wsa;
bool is_init = false;
bool socket_utility_init();
public:
/*
*
*/
socket_utility();
/*
*
*/
~socket_utility();
/*
* create UPD socket bind to ip and port
*/
int create_udp_socket(int ip_addr, int port);
/*
* create UPD socket not bind to any ip
*/
int create_udp_socket_nobind(int port);
/*
* send message
*/
int send_udp_message(int sock_fd, void * message, int message_len, int destination_ip, int destination_port);
/*
* receive message
*/
int receive_udp_message(int sock_fd, void * message, int message_len, int & source_ip, int & source_port);
/*
* close socket
*/
int close_udp_socket(int sock_fd);
};
#endif