comm: handle connect() failures properly
Quoting connect(3posix): "If connect() fails, the state of the socket is unspecified. Conforming applications should close the file descriptor and create a new socket before attempting to reconnect." Change-Id: Ibcdcc0f546560a41009832894659a37947243f2f
This commit is contained in:
@ -30,10 +30,12 @@ JobClient::~JobClient()
|
|||||||
|
|
||||||
bool JobClient::connectToServer()
|
bool JobClient::connectToServer()
|
||||||
{
|
{
|
||||||
|
int retries = CLIENT_RETRY_COUNT;
|
||||||
|
while (true) {
|
||||||
// Connect to server
|
// Connect to server
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if(m_sockfd < 0) {
|
if (m_sockfd < 0) {
|
||||||
perror("[Client@socket()]");
|
perror("[Client@socket()]");
|
||||||
// TODO: Log-level?
|
// TODO: Log-level?
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -48,10 +50,9 @@ bool JobClient::connectToServer()
|
|||||||
memcpy(&serv_addr.sin_addr.s_addr, m_server_ent->h_addr, m_server_ent->h_length);
|
memcpy(&serv_addr.sin_addr.s_addr, m_server_ent->h_addr, m_server_ent->h_length);
|
||||||
serv_addr.sin_port = htons(m_server_port);
|
serv_addr.sin_port = htons(m_server_port);
|
||||||
|
|
||||||
int retries = CLIENT_RETRY_COUNT;
|
|
||||||
while (true) {
|
|
||||||
if (connect(m_sockfd, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
if (connect(m_sockfd, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||||
perror("[Client@connect()]");
|
perror("[Client@connect()]");
|
||||||
|
close(m_sockfd);
|
||||||
// TODO: Log-level?
|
// TODO: Log-level?
|
||||||
if (retries > 0) {
|
if (retries > 0) {
|
||||||
// Wait CLIENT_RAND_BACKOFF_TSTART to RAND_BACKOFF_TEND seconds:
|
// Wait CLIENT_RAND_BACKOFF_TSTART to RAND_BACKOFF_TEND seconds:
|
||||||
|
|||||||
Reference in New Issue
Block a user