-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbcastServer.cpp
More file actions
105 lines (100 loc) · 3.01 KB
/
Copy pathbcastServer.cpp
File metadata and controls
105 lines (100 loc) · 3.01 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
/*
* bcastServer.cpp
*
* SimMgr applicatiopn
*
* This file is part of the sim-mgr distribution.
*
* Copyright (c) 2024 ITown Design LLC, Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "winsock2.h"
#include <ws2tcpip.h>
#include <iostream>
#include <cstdio>
#include <conio.h>
#include <functional>
#include "vetsimDefs.h"
extern int closeFlag; // defined in simstatus.cpp; set to 1 on shutdown
int bcastReply(void)
{
WSADATA wsaData;
auto sts = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (sts == 0)
{
SOCKET sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
int broadcast = 1;
// This option is needed on the socket in order to be able to receive broadcast messages
// If not set the receiver will not receive broadcast messages in the local network.
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast)) < 0)
{
std::cout << "Error in setting Broadcast option";
sts = -1;
}
else
{
struct sockaddr_in Recv_addr;
struct sockaddr_in Sender_addr;
int len = sizeof(struct sockaddr_in);
char recvbuff[50];
int recvbufflen = 50;
Recv_addr.sin_family = AF_INET;
Recv_addr.sin_port = htons(PORT_STATUS);
Recv_addr.sin_addr.s_addr = INADDR_ANY;
auto ret = bind(sock, (sockaddr*)&Recv_addr, sizeof(Recv_addr));
if (ret < 0)
{
std::cout << "Error in BINDING" << WSAGetLastError();
sts = -1;
}
else
{
// Wake up every second so the loop can check closeFlag even
// when no broadcast arrives.
DWORD recvTimeout = 1000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(const char*)&recvTimeout, sizeof(recvTimeout)) < 0)
{
std::cout << "Warning: SO_RCVTIMEO failed; shutdown may be delayed\n";
}
do
{
std::cout << "\nWaiting for message...\n";
memset(recvbuff, 0, sizeof(recvbuff));
auto sts = recvfrom(sock, recvbuff, recvbufflen, 0, (sockaddr*)&Sender_addr, &len);
if (sts != SOCKET_ERROR)
{
std::cout << "Received Message is: " << recvbuff;
if (strcmp("WVS_LOOK", recvbuff) == 0)
{
inet_ntop(AF_INET, &(Sender_addr.sin_addr), recvbuff, INET_ADDRSTRLEN);
std::cout << " From " << recvbuff;
sprintf_s(recvbuff, "WVS_FOUND");
ret = sendto(sock, recvbuff, (int)strlen(recvbuff), 0, (struct sockaddr*)&Sender_addr, len);
sts = 0;
}
}
} while (!closeFlag);
}
}
closesocket(sock);
}
else
{
printf("WSAStartup returns %d\n", sts);
}
WSACleanup();
return (sts);
}