linux - client socket connection issue -


hi i'm trying write client application try connect remote server. if can not connect server, try once again after 5 seconds. if socket closed somehow, try connect once again.

i'm getting error connect: transport endpoint connected

what problem ?

static void sig_chld(int signo) {      pid_t   pid;     int stat;     while ( (pid = waitpid(-1, &stat, wnohang)) > 0)         printf("child %d terminated\n", pid);      return; }   int main(int argc, char *argv[]) {  int sockfd, numbytes;   char buf[maxdatasize]; pid_t   childpid; struct hostent *he; struct sockaddr_in their_addr; /* connector's address information */          if ((he=gethostbyname(argv[1])) == null) {  /* host info */             herror("gethostbyname");             exit(1);         }          if ((sockfd = socket(af_inet, sock_stream, 0)) == -1) {             perror("socket");             exit(1);         }          their_addr.sin_family = af_inet;      /* host byte order */         their_addr.sin_port = htons(port);    /* short, network byte order */         their_addr.sin_addr = *((struct in_addr *)he->h_addr);         bzero(&(their_addr.sin_zero), 8);     /* 0 rest of struct */       ( ; ; ) {           while (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)         {                            perror("connect");                 sleep(5);             }           if ( (childpid = fork()) == 0)         {   /* child process */             while(1)             {                     if (send(sockfd, "hello, world!\n", 14, 0) == -1)                 {                             perror("send");                 }                  sleep(3);             }             close(sockfd);          }     }          return 0;     } 

you can't reconnect socket once have tried connect before, if failed. have close , create new one.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -