C
C#12mo ago
JJ

heart beat in console program

I have a code that implements multi-threaded socket communication with c++, and I want to add an additional heart beat code. However, when the server is waiting for a client message through recv for each thread, how does make run the heart beat? If you let me know the logic or hint, I want to try it.

while ((nReceive = ::recv(hClient,
szBuffer, sizeof(szBuffer), 0)) > 0)
{
puts(szBuffer);
//Send received strings to all connected clients
SendChattingMessage(szBuffer);
memset(szBuffer, 0, sizeof(szBuffer));
}

while ((nReceive = ::recv(hClient,
szBuffer, sizeof(szBuffer), 0)) > 0)
{
puts(szBuffer);
//Send received strings to all connected clients
SendChattingMessage(szBuffer);
memset(szBuffer, 0, sizeof(szBuffer));
}
recv enter infiniteloop, and waiting until recevie message from client. i wanna make heart beat code. but It can't since recv. this is all code: https://ideone.com/TWRvnB
Ideone.com
Ideone.com
Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.
15 Replies
Buddy
Buddy12mo ago
C++ and C# are two entirely different languages $cpp
MODiX
MODiX12mo ago
We're partnered with Together C & C++, check them out here: https://discord.gg/vnyVmAE
JJ
JJOP12mo ago
If C#, How to resolve this problem? I think idea(algorithm) is same whatever any langauge
Buddy
Buddy12mo ago
There are different heartbeat implementations, most of the time you can just send a single byte and that is the heartbeat, each 15th second or so.
JJ
JJOP12mo ago
yea, i know, but since receive, I can't image idea. because
while ((nReceive = ::recv(hClient,
szBuffer, sizeof(szBuffer), 0)) > 0)
while ((nReceive = ::recv(hClient,
szBuffer, sizeof(szBuffer), 0)) > 0)
Threads on the server enter an infinite standby state through recv, so that any thread cannot send a message to the client by heartbeats after a certain period of time.
Buddy
Buddy12mo ago
Huh?
JJ
JJOP12mo ago
Could you understand my meaning?
Jimmacle
Jimmacle12mo ago
you're sending and receiving on the same thread?
JJ
JJOP12mo ago
upload code is not include thread about heart beat
Jimmacle
Jimmacle12mo ago
i didn't look at the code, i don't understand what the problem is
JJ
JJOP12mo ago
this is my code https://ideone.com/7YeptO I included heart beat thread
Ideone.com
Ideone.com
Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.
Jimmacle
Jimmacle12mo ago
i'm not asking for more code, i'm asking for you to explain the issue more clearly i don't know C++ that well anyway
JJ
JJOP12mo ago
Even if a heartbeat thread is created after acceptance or before acceptance, it is executed only at the beginning, but is not executed again after being executed once. This is because each thread is in a standby state through receive. this is my issue
Jimmacle
Jimmacle12mo ago
am i missing something or is your heartbeat thread not a loop? if you don't use a loop it's only going to execute the code once
JJ
JJOP12mo ago
oh thanks, I understood. Actuall I didn't know thread execute in the loop continuosly
Want results from more Discord servers?
Add your server