PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusOpenIGTLinkServerWin32.cxx
Go to the documentation of this file.
1 #include <WS2tcpip.h>
2 #include <winsock2.h>
3 #include <iphlpapi.h>
4 #pragma comment(lib, "Iphlpapi.lib")
5 
7 {
8  DWORD dwRetVal = 0;
9  PIP_ADAPTER_ADDRESSES pAddresses = NULL;
10  ULONG outBufferSize = 0;
11  ULONG iterations = 0;
12  DWORD ipv4StringBufferLength = 63;
13 
14  // Allocate a 15 KB buffer to start with.
15  outBufferSize = 15 * 1024;
16 
17  do
18  {
19  pAddresses = (IP_ADAPTER_ADDRESSES*)HeapAlloc(GetProcessHeap(), 0, (outBufferSize));
20 
21  if (pAddresses == NULL)
22  {
23  return;
24  }
25 
26  dwRetVal = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufferSize);
27 
28  if (dwRetVal == ERROR_BUFFER_OVERFLOW)
29  {
30  HeapFree(GetProcessHeap(), 0, (pAddresses));
31  pAddresses = NULL;
32  }
33  else
34  {
35  break;
36  }
37 
38  iterations++;
39  }
40  while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (iterations < 3));
41 
42  std::vector<std::string> addresses;
43 
44  if (dwRetVal == NO_ERROR)
45  {
46  PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;
47  while (pCurrAddresses)
48  {
49  PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pCurrAddresses->FirstUnicastAddress;
50  if (pUnicast != NULL)
51  {
52  for (unsigned int i = 0; pUnicast != NULL; i++)
53  {
54  sockaddr_in* sa_in = (sockaddr_in*)pUnicast->Address.lpSockaddr;
55  std::string address = inet_ntoa(sa_in->sin_addr);
56  addresses.push_back(address);
57  pUnicast = pUnicast->Next;
58  }
59  }
60 
61  pCurrAddresses = pCurrAddresses->Next;
62  }
63  }
64 
65  if (pAddresses)
66  {
67  HeapFree(GetProcessHeap(), 0, (pAddresses));
68  }
69 
70  std::stringstream ss;
71  ss << "Plus OpenIGTLink server listening on IPs: ";
72  for (unsigned int i = 0; i < addresses.size(); ++i)
73  {
74  ss << addresses[i];
75  if (i + 1 != addresses.size())
76  {
77  ss << ", ";
78  }
79  }
80  ss << " -- port " << self->GetListeningPort();
81  LOG_INFO(ss.str());
82 }
for i
unsigned long DWORD
Definition: ATC3DGm.h:451
const char * address
Definition: phidget22.h:2552
unsigned long ULONG
Definition: ATC3DGm.h:432
This class provides a network interface for data acquired by Plus as an OpenIGTLink server.
void PrintServerInfo(vtkPlusOpenIGTLinkServer *self)