Skip to content

Commit

Permalink
Fixed quitting error.
Browse files Browse the repository at this point in the history
  • Loading branch information
quiet-node committed Mar 22, 2022
1 parent 1525482 commit 28f5bba
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 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.
11 changes: 1 addition & 10 deletions utilities/TCPBeacons_CPP/TCPBeacons/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ int __cdecl main(int argc, char* argv[])
}





// >>>>>>> TCP/IP HANDLER <<<<<<<

std::string ipAddress = "127.0.0.1"; // ip of the server
Expand Down Expand Up @@ -131,8 +128,7 @@ int __cdecl main(int argc, char* argv[])

}


// Send Beacons to TCP server then receive the beacons from server and echo out
// Send Beacons to TCP server then echo the beacons from server

bool exit = false;
char buf[4096];
Expand Down Expand Up @@ -167,12 +163,7 @@ int __cdecl main(int argc, char* argv[])

}



std::cout << "Stopping TCP/IP Connection to the Server." << std::endl;
closesocket(sock_);
WSACleanup();


std::this_thread::sleep_for(std::chrono::seconds(5));
}
Binary file added utilities/TCPBeacons_CPP/TCPBeaconsClient.exe
Binary file not shown.
56 changes: 43 additions & 13 deletions utilities/TCPBeacons_CPP/TCPBeaconsServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

#define LISTENING_PORT 1901


void sendBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDelay, int outerDelay)
{
bool exit = false;
char buf[4096];

while (exit == false)
{
for (std::size_t i = 0; i < beacons.size(); i++)
Expand All @@ -32,20 +31,48 @@ void sendBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDelay,
// send beacons to client
send(sock_, beacons[i].c_str(), beacons[i].size() + 1, 0);

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

}
}

Sleep(outerDelay);
}

}

void receiveBeacons(SOCKET sock_, std::vector<std::string> beacons, int innerDelay, int outerDelay)
{
bool exit = false;
char buf[4096];

while (exit == false)
{
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 == SOCKET_ERROR)
{
std::cerr << "Error in recv(). Quitting" << std::endl;
break;
}
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;

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

}
}

Sleep(outerDelay);
}

}

int __cdecl main(int argc, char* argv[])
Expand Down Expand Up @@ -222,21 +250,23 @@ int __cdecl main(int argc, char* argv[])

// Send beacons to clients
std::cout << std::endl;
std::cout << "Sending TCP/IP beacons to " << host << "starts now!" << 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 th(sendBeacons, clientSocket, beacons, innerDelay, outerDelay);
th.detach();
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 28f5bba

Please sign in to comment.