Play with the connect function with timeout in Linux

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts under Windows. In this article, we will play with Linux. I encountered this question in an interview, which is interesting.

Directly on the client code:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
int main(int argc, char *argv[]) // Pay attention to the input parameters, bring ip and port
{
  int sockClient = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addrSrv;
  addrSrv.sin_addr.s_addr = inet_addr(argv[1]);
  addrSrv.sin_family = AF_INET;
  addrSrv.sin_port = htons(atoi(argv[2]));
 fcntl(sockClient, F_SETFL, fcntl(sockClient, F_GETFL, 0)|O_NONBLOCK); 
  int iRet = connect(sockClient, (const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
 printf("connect iRet is %d, errmsg:%s\n", iRet, strerror(errno)); // Returning -1 is not necessarily an exception if (iRet != 0) 
 { 
   if (errno != EINPROGRESS)
 {
  printf("connect error:%s\n", strerror(errno)); 
 }
   else 
 {
  struct timeval tm = {5, 0}; 
  fd_set wset, rset; 
  FD_ZERO(&wset); 
  FD_ZERO(&rset); 
  FD_SET(sockClient, &wset); 
  FD_SET(sockClient, &rset); 
  int time1 = time(NULL);
  int n = select(sockClient + 1, &rset, &wset, NULL, &tm); 
  int time2 = time(NULL);
  printf("time gap is %d\n", time2 - time1);
  if(n < 0) 
  { 
   printf("select error, n is %d\n", n); 
  } 
  else if(n == 0) 
  { 
   printf("connect time out\n"); 
  } 
  else if (n == 1) 
  {
   if(FD_ISSET(sockClient, &wset)) 
   { 
    printf("connect ok!\n"); 
    fcntl(sockClient, F_SETFL, fcntl(sockClient, F_GETFL, 0) & ~O_NONBLOCK); 
   } 
   else 
   { 
    printf("unknow error:%s\n", strerror(errno)); 
   } 
  }
  else
  {
  printf("oh, not care now, n is %d\n", n);
  }
 } 
 } 
 printf("I am here!\n");
  getchar();
  close(sockClient);
  return 0;
}

We have written the server code many times, so we will not write it in this article.

After testing, the above program is OK. You can also learn a lot by using tcpdump to capture packets, such as SYN packet retransmission, RST packet, etc. Quite interesting.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Detailed explanation of the solution to npm ls errors caused by fsevents module under Linux
  • Explanation of installation and configuration of building go environment under linux
  • Summary of Linux cut command usage
  • Detailed explanation of Linux system input and output management and common functions of vim
  • Linux shell - Example of how to test file system attributes by identification
  • Various judgments of if in linux shell
  • Linux shell pushd, popd and dirs usage explanation
  • How to print various color fonts and backgrounds in the Linux shell console
  • View the dependent libraries of so or executable programs under linux
  • Example explanation of alarm function in Linux

<<:  Explanation of the basic syntax of Mysql database stored procedures

>>:  Configuring MySQL and Squel Pro on Mac

Recommend

MySQL 8.0.21 installation tutorial with pictures and text

1. Download the download link Click download. You...

How to implement web stress testing through Apache Bench

1. Introduction to Apache Bench ApacheBench is a ...

How to implement the jQuery carousel function

This article shares the implementation code of jQ...

Summary of commonly used operators and functions in MySQL

Let’s build the data table first. use test; creat...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

A brief discussion on the role of Vue3 defineComponent

Table of contents defineComponent overload functi...

Sharing several methods to disable page caching

Today, when developing, I encountered a method wh...

How to install MySQL 5.7 from source code in CentOS 7 environment

This article describes how to install MySQL 5.7 f...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

Analysis of MySQL example DTID master-slave principle

Table of contents 1. Basic Concepts of GTID 2. GT...

MySQL statement summary

Table of contents 1. Select database USE 2. Displ...

Mysql solution to improve the efficiency of copying large data tables

Preface This article mainly introduces the releva...