linux

您所在的位置:网站首页 connect会阻塞吗 linux

linux

#linux| 来源: 网络整理| 查看: 265

~/cpp$ ./connect 192.168.1.234 1234 kkkk

block mode:  ubuntu 14.04 : time used:21.0.001053s

connect 超时时间是大约21秒!

注意:如果connect 127.x.x.x  xxx  kkkk 会立即返回因为127开头的是网卡自身,你可以ping一下,发现都是通的,且等同于127.0.0.1

#include #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 512 int 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 main( int argc, char* argv[] ) { if( argc = 0 ); int sendbuf = atoi( argv[3] ); int len = sizeof( sendbuf ); setsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof( sendbuf ) ); getsockopt( sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, ( socklen_t* )&len ); printf( "the tcp send buffer size after setting is %d\n", sendbuf ); int old_option = fcntl( sock, F_GETFL ); printf("noblock: %d\n", old_option & O_NONBLOCK); //0-->block mode //int oldopt = setnonblocking(sock); set nonblock mode! struct timeval tv1, tv2; gettimeofday(&tv1, NULL); int ret = connect( sock, ( struct sockaddr* )&server_address, sizeof( server_address ) ); printf("connect ret code is: %d\n", ret); if ( ret == 0 ) { // printf("call getsockname ...\n"); struct sockaddr_in local_address; socklen_t length; int ret = getpeername(sock, ( struct sockaddr* )&local_address, &length); assert(ret == 0); char local[INET_ADDRSTRLEN ]; printf( "local with ip: %s and port: %d\n", inet_ntop( AF_INET, &local_address.sin_addr, local, INET_ADDRSTRLEN ), ntohs( local_address.sin_port ) ); // char buffer[ BUFFER_SIZE ]; memset( buffer, 'a', BUFFER_SIZE ); send( sock, buffer, BUFFER_SIZE, 0 ); } else if (ret == -1) { gettimeofday(&tv2, NULL); suseconds_t msec = tv2.tv_usec - tv1.tv_usec; time_t sec = tv2.tv_sec - tv1.tv_sec; printf("time used:%d.%fs\n", sec, (double)msec / 1000000 ); printf("connect failed...\n"); if (errno == EINPROGRESS) { printf("unblock mode ret code...\n"); } } else { printf("ret code is: %d\n", ret); } printf("after connected!\n"); close( sock ); return 0; }

  

非阻塞模式:

#include #include #include #include #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 1023 int 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, &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 ) ); printf("connect ret code = %d\n", ret); if ( ret == 0 ) { printf( "connect with server immediately\n" ); fcntl( sockfd, F_SETFL, fdopt ); //set old optional back return sockfd; } //unblock mode --> connect return immediately! ret = -1 & errno=EINPROGRESS else if ( errno != EINPROGRESS ) { printf("ret = %d\n", ret); printf( "unblock connect failed!\n" ); return -1; } else if (errno == EINPROGRESS) { printf( "unblock mode socket is connecting...\n" ); } //use select to check write event, if the socket is writable, then //connect is complete successfully! fd_set readfds; fd_set writefds; struct timeval timeout; FD_ZERO( &readfds ); FD_SET( sockfd, &writefds ); timeout.tv_sec = time; //timeout is 10 minutes timeout.tv_usec = 0; ret = select( sockfd + 1, NULL, &writefds, NULL, &timeout ); if ( ret


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3