私のクラスMasterSlaveSynchronizeは、hearbeatの送信と受信の両方に使用されます。両方に使用するデータグラムソケットを1つだけ持っていても大丈夫ですか?送信、受信?
以下の2つの方法は時々実行され、同時に実行できます。
void MasterSlaveSynchronize::sendHearBeat() {
const int HEARBEAT_LEN = 1;
const char HEARBEAT[1] = { '1' };
int n = sendto(sock, HEARBEAT, HEARBEAT_LEN, 0,(const struct sockaddr *) &target_, length_);
if (n < 0)
printf("Sendto"); //TODO ERR
}
void MasterSlaveSynchronize::recivedHearBeat() {
char buf[1024];
if (bind(sock, (struct sockaddr *) &this_, length_) < 0)
printf("binding"); //TODO err
while (1) {
int n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &target_,&length_);
if (n < 0)
printf("recvfrom"); //TODO ER
//TODO update got hearbeat
}