Non-blocking connect under Linux

Source: Internet
Author: User

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #include <time.h> #include < errno.h> #include <fcntl.h> #include <sys/ioctl.h> #include <unistd.h> #include <string.h>#    Define Buffer_size 1023int setnonblocking (int fd) {int old_option=fcntl (FD,F_GETFL); int New_option=old_option |    O_nonblock;    Fcntl (fd,f_setfl,new_option); return old_option;}    int unblock_connect (const char *ip,int port,int time) {int ret=0;    struct SOCKADDR_IN address;    Bzero (&address,sizeof (address));    Address.sin_family=af_inet;    Inet_pton (AF_INET,IP,&AMP;ADDRESS.SIN_ADDR);    Address.sin_port=htons (port);    int Sockfd=socket (pf_inet,sock_stream,0);    int fdopt=setnonblocking (SOCKFD);    Ret=connect (SOCKFD, (struct sockaddr *) &address,sizeof (address)); if (ret==0) {printf ("Connect with server immediately\n");        Fcntl (sockfd,f_setfl,fdopt);    return SOCKFD;        } else if (errno!=einprogress) {printf ("Unblock connect not support\n");    return-1;    } fd_set Readfds;    Fd_set Writefds;    struct Timeval timeout;    Timeout.tv_sec=time;    timeout.tv_usec=0;    Ret=select (sockfd+1,null,&writefds,null,&timeout);        if (ret<=0) {printf ("Connection time out\n");        Close (SOCKFD);    return-1; } if (!        Fd_isset (Sockfd,&writefds)) {printf ("No events on SOCKFD found\n");        Close (SOCKFD);    return-1;    } int error=0;    socklen_t length=sizeof (Error);        if (getsockopt (sockfd,sol_socket,so_error,&error,&length) <0) {printf ("Get SOCKET option failed");        Close (SOCKFD);    return-1;        } if (error!=0) {printf ("Connection failed after select with the error:% d \ n", error);        Close (SOCKFD);    return-1; } printf ("Connection ready after select with the"Cket:%d \ n ", SOCKFD);    Fcntl (sockfd,f_setfl,fdopt); return SOCKFD;}    int main () {const char *ip= "127.0.0.1";    int port=12345;    int Sockfd=unblock_connect (ip,port,10);    if (sockfd<0) {return 1;    } close (SOCKFD); return 0;}

Non-blocking connect under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.