How to check if the other end of the socket is accepted?

I have a client / server configured, and I want my client to find out if the server has accepted the connection. Otherwise, my client has no idea that he is still waiting to be received. I cannot rely on further communication (protocol specification) to verify this. So, for example, sending the line “Good to go” from the server to the client is not an option. Is there a flag or something that I can check to make sure the server is really receiving? The following is sample code:

/* Client */
...
getaddrinfo(ip, port, &hints, &servinfo);
connect(sockfd, info->ai_addr, info->ai_addrlen);

if (info == NULL) {
    printf("connection failure\n");
    exit(1);
}

inet_ntop(info->ai_family, get_in_addr((struct sockaddr *)info->ai_addr), ipstring, sizeof(ipstring));
printf("Connected to %s!\n", ipstring);
...

/* Server */
...
pause(); /* If don't accept the connection, how to make the client know? */ 
new_fd = accept(sockfd, (struct sockaddr *)&cli_addr, &addr_size);
...
+3
source share
2 answers

Due to a lag, the server may send a SYN-ACK before accepting the call. Thus, a client call connect()can return before server calls accept().

: "Good to go" . : "" . .

- TCP . ?

+4

connect(), errno, .

connect() -, connect() -1 errno ETIMEDOUT

  int ret = connect(sockfd, info->ai_addr, info->ai_addrlen);
  if (ret == -1) {
      /* connect failed */
      switch(errno) {
      case ETIMEDOUT:
             /* your server didn't accept the connection */
      case ECONNREFUSED:
             /* your server isn't listening yet, e.g. didn't start */
      default:
             /* any other error, see man 2 connect */
      }
  }
+2

All Articles