Skip to content

Commit

Permalink
updated tcp beacons app
Browse files Browse the repository at this point in the history
  • Loading branch information
quiet-node committed Mar 23, 2022
1 parent 28f5bba commit d990685
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 27 deletions.
Binary file modified utilities/TCPBeacons_CPP/Application/TCPBeaconsClient.exe
Binary file not shown.
Binary file modified utilities/TCPBeacons_CPP/Application/TCPBeaconsServer.exe
Binary file not shown.
Binary file removed utilities/TCPBeacons_CPP/TCPBeacons.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion utilities/TCPBeacons_CPP/TCPBeacons/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int __cdecl main(int argc, char* argv[])
{
// >>>>>>> FILE HANDLER <<<<<<<

// init attributes
// init attributes
std::string fileName, mode1, mode2;
int innerDelay = 10;
int outerDelay = 1000;
Expand Down
Binary file removed utilities/TCPBeacons_CPP/TCPBeaconsClient.exe
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>..\Application</OutDir>
<OutDir>..\Application\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
Expand Down
36 changes: 11 additions & 25 deletions utilities/TCPBeacons_CPP/TCPBeaconsServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
#include <thread>
#include <chrono>


#pragma comment (lib, "ws2_32.lib")

#define LISTENING_PORT 1901


Expand All @@ -40,10 +38,9 @@ void sendBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDelay,

}
}

Sleep(outerDelay);
std::cout << "Stop sending beacons..." << std::endl;
Sleep(300);
}

}

void receiveBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDelay, int outerDelay)
Expand All @@ -55,40 +52,30 @@ void receiveBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDel
{
for (std::size_t i = 0; i < beacons.size(); i++)
{
if (sock_ != SOCKET_ERROR)
{


// echo beacons sent from client
ZeroMemory(buf, 4096);
int byteRecv = recv(sock_, buf, 4096, 0);
if (byteRecv == 0)
{
std::cout << "Client disconnected" << std::endl;
if (byteRecv == SOCKET_ERROR)
{
std::cerr << "Error in recv(). Quitting" << std::endl;
break;
}

break;
}

// echo the message
std::cout << std::string(buf, 0, byteRecv) << std::endl;

// exit when user hit esc
if (GetAsyncKeyState(VK_ESCAPE)) {
exit = true;
break;
}
Sleep(innerDelay);

}
}

Sleep(outerDelay);
std::cout << "" << std::endl;
std::cout << "Client disconnected..." << std::endl;
Sleep(300);
}

}

int __cdecl main(int argc, char* argv[])
Expand All @@ -99,6 +86,7 @@ int __cdecl main(int argc, char* argv[])
std::vector<std::string> beacons;
sockaddr_in hint;
fd_set master; // select()::fd_set master is a set of 1 listening value and multiple client values
std::vector<std::thread> threads;



Expand Down Expand Up @@ -238,7 +226,6 @@ int __cdecl main(int argc, char* argv[])
std::cout << host << " connected on port " << ntohs(client.sin_port) << std::endl;
}


// Send a message to client to inform connected
std::string welcomeMsg = "Connected to the listening server on port " + std::to_string(LISTENING_PORT);
send(clientSocket, welcomeMsg.c_str(), welcomeMsg.size() + 1, 0);
Expand All @@ -251,22 +238,21 @@ int __cdecl main(int argc, char* argv[])
// Send beacons to clients
std::cout << std::endl;
std::cout << "Sending TCP/IP beacons to " << host << "! Start now!" << std::endl;

bool exit = false;

char buf[4096];

// Server sends beacons to clients through multi threads
std::thread sendThread(sendBeacons, clientSocket, beacons, innerDelay, outerDelay);
sendThread.detach();
std::thread recvThread(receiveBeacons, clientSocket, beacons, innerDelay, outerDelay);
recvThread.detach();

}

}



}

// Cleanup winsock
WSACleanup();
system("pause");
Expand Down

0 comments on commit d990685

Please sign in to comment.