This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
lms.cpp
77 lines (64 loc) · 1.67 KB
/
lms.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
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "lms.h"
#include <string.h>
#ifdef _WIN32
// Windows
#include <Ws2tcpip.h>
#else
// Linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#endif
SOCKET lms_connect()
{
std::string lmsAddress = "localhost";
std::string lmsPort = "16992";
SOCKET s = INVALID_SOCKET;
struct addrinfo *addr, hints;
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
throw std::runtime_error("error: unable to connect to LMS");
}
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(lmsAddress.c_str(), lmsPort.c_str(), &hints, &addr) != 0)
{
throw std::runtime_error("error: unable to connect to LMS");
}
if (addr == NULL)
{
throw std::runtime_error("error: unable to connect to LMS");
}
for (addr; addr != NULL; addr = addr->ai_next)
{
s = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (s == INVALID_SOCKET)
{
continue;
}
if (connect(s, addr->ai_addr, (int)addr->ai_addrlen) == 0)
{
break;
}
closesocket(s);
s = INVALID_SOCKET;
}
if (addr != NULL)
{
freeaddrinfo(addr);
}
if (s == INVALID_SOCKET)
{
throw std::runtime_error("error: unable to connect to LMS");
}
return s;
}