I'm working on a distributed system with multiple workers.
Each worker is assigned a specific address - host and port.
As a worker, I'd like to know when another worker comes online and starts listening to their assigned address.
Currently I create a socket and keep trying to connect until I reach a timeout.
An external function calls connectionOk
in a loop.
bool MyClass::connectionOk(struct sockaddr_storage other_worker) {
//connect and disconnect to verify peer is available
int fd = socket(other_worker.ss_family, SOCK_STREAM, 0);
int res = connect(fd, (const sockaddr*)&other_worker, sizeof(other_worker));
close(fd);
return res == 0;
}
However that doesn't seem to work.
A worker gets created, starts listening, but sometimes connectionOk
returns false
and timeout is reached.
Is there a better way to accomplish this?