-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathudpsocket.h
More file actions
108 lines (90 loc) · 1.91 KB
/
udpsocket.h
File metadata and controls
108 lines (90 loc) · 1.91 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
106
107
108
#ifndef __UDPSOCKET_H__
#define __UDPSOCKET_H__
#ifdef _WIN32
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define close(x) closesocket(x)
typedef int socklen_t;
#else
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
typedef int SOCKET;
#endif
class udpsocket
{
public:
udpsocket()
{
this->udpsock = 0;
}
udpsocket(SOCKET udpsock, struct sockaddr_in *paddr)
{
this->udpsock = udpsock;
this->sockaddr = *paddr;
}
~udpsocket()
{
}
bool bind(const char *addr, unsigned int short port)
{
udpsock = socket(AF_INET, SOCK_DGRAM, 0);
if (udpsock <= 0)
{
return false;
}
unsigned int yes = 1;
setsockopt(udpsock, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr(addr);
sockaddr.sin_port = htons(port);
if (::bind(udpsock, (struct sockaddr*)&sockaddr, sizeof(struct sockaddr)) < 0)
{
return false;
}
return true;
}
bool connect(const char *addr, unsigned short int port)
{
if (!this->bind("0.0.0.0", 0))
{
return false;
}
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr(addr);
sockaddr.sin_port = htons(port);
return true;
}
void shutdown()
{
::close(udpsock);
}
int recvfrom(char * buf, int len, struct sockaddr * from, socklen_t* fromlen)
{
return ::recvfrom(udpsock, buf, len, 0, from, fromlen);
}
int sendto(const char * buf, int len)
{
return ::sendto(udpsock, buf, len, 0, (const struct sockaddr *)&sockaddr, sizeof(sockaddr));
}
SOCKET getsocket()
{
return udpsock;
}
private:
SOCKET udpsock;
struct sockaddr_in sockaddr;
};
#endif