-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetHandlercpp.cpp
More file actions
51 lines (48 loc) · 1.33 KB
/
NetHandlercpp.cpp
File metadata and controls
51 lines (48 loc) · 1.33 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
#include "netHandler.h"
NetConn::NetConn(const char* destIP, const unsigned int destport)
{
if (WSAStartup(0x202, &WsaData) == SOCKET_ERROR)
{
std::cout << "[PacketClient] WSAStartup failed (error: " << WSAGetLastError() << ")" << std::endl;
result = -1;
WSACleanup();
}
else
{
outsock = socket(AF_INET, SOCK_DGRAM, 0);
sprintf(this->destIP, destIP);
sockaddr_in a;
memset(&a, 0, sizeof(addr));
a.sin_family = AF_INET;
a.sin_port = htons(destport);
a.sin_addr.s_addr = inet_addr(this->destIP);
result = connect(outsock, (struct sockaddr*)&a, sizeof(a));
addr = &a;
}
}
NetConn::NetConn(const char* destIP, const unsigned int destport, sockaddr_in* a)
{
if (WSAStartup(0x202, &WsaData) == SOCKET_ERROR)
{
std::cout << "[PacketClient] WSAStartup failed (error: " << WSAGetLastError() << ")" << std::endl;
result = -1;
WSACleanup();
}
else
{
outsock = socket(FAMILY, SOCK_DGRAM, 0);
addr = a;
result = connect(outsock, (struct sockaddr*)addr, sizeof(addr));
}
}
int NetConn::mysend(const int ID, float xpos, float ypos, float zpos)
{
char payload[512];
sprintf(payload, "VR_TRACKER_SENSOR_%i, %3.4f, %3.4f, %3.4f\0", ID, xpos, ypos, zpos);
int ret = send(outsock, payload, strlen(payload), 0);
return ret;
}
int NetConn::getResult() const
{
return result;
}