I was doing Network Programming with sockets. And there is a problem while my client program was trying to make repeated connections to the server when the server did not start ( particular requirement of the project ).
Here is the problem:
Server did not start: the client waits 3 seconds, then make another connect() request.
When I start the server: the client still attempts to connect every 3 second.
The error message when we run the client program:
$./client localhost
Cannot connect to the server. Retrying...: Connection refused
Cannot connect to the server. Retrying...: Invalid argument
Cannot connect to the server. Retrying...: Invalid argument
...
Here is the client code:
#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT "9734"
#define WAIT_SERVER_TIME 3
int main(int argc, char* argv[]){
if(argc<2){
printf("usage: %s MACHINE_NAME\n",argv[0]);
exit(EXIT_SUCCESS);
}else{
int client_socket_fd = 0 ;
struct addrinfo hints,*results;
/* Get the address info of the server to be connected */
memset(&hints,0,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if( getaddrinfo(argv[1],SERVER_PORT,&hints,&results) !=0){
printf("Cannot recognize the given server name");
exit(EXIT_FAILURE);
}
/* Create the client socket */
if((client_socket_fd = socket( AF_INET, SOCK_STREAM, 0))<0){
perror("Cannot create the client socket.");
exit(EXIT_FAILURE);
}
if(results->ai_addr == NULL){
perror("Cannot find the given server name.");
exit(EXIT_FAILURE);
}
/* Set up the connection */
while(connect(client_socket_fd,results->ai_addr,
results->ai_addrlen) < 0 ){
perror("Cannot connect to the server. Retrying...");
sleep(WAIT_SERVER_TIME);
}
printf("Successfully connected to the server.\n");
// DO STUFFS HERE
exit(EXIT_SUCCESS);
}
}
Here is the server code for testing purpose:
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <ifaddrs.h>
int main(int argc, char* argv[]){
/* SERVER SET UP */
int enable_reuse_socket = 1;
struct sockaddr_in server_address;
socklen_t server_address_len=0;
int server_socket_fd ;
server_socket_fd = socket( AF_INET, SOCK_STREAM, 0);
// Handle error creating a socket
if( server_socket_fd < 0){
perror("Cannot initialize the socket for connection.");
exit( EXIT_FAILURE);
}
// Set up the INET address
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr= htonl(INADDR_ANY);
server_address.sin_port = htons(SERVER_PORT);
server_address_len = sizeof( server_address);
// Bind the socket to the given address
if(setsockopt(server_socket_fd,SOL_SOCKET,SO_REUSEADDR,&enable_reuse_socket
,sizeof(int))<0){
perror("Fail to reuse socket.");
}
if(bind(server_socket_fd,(struct sockaddr*) &server_address,
server_address_len) !=0){
perror("Cannot bind the socket to the given address.");
exit(EXIT_FAILURE);
}
// Allow the socket to listen to the clients
if(listen(server_socket_fd, MAX_CLIENT)!=0){
perror("Cannot allow the socket to listen to incoming client \
messages");
exit(EXIT_FAILURE);
}
struct sockaddr_in client_addr;
socklen_t client_addr_len;
while(1){
int client_fd = accept(server_socket_fd,(struct sockaddr*)&client_addr,
&client_addr_len);
printf("Gotcha!");
}
}