tcp_connect函数
Posted soldierback
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tcp_connect函数相关的知识,希望对你有一定的参考价值。
#include <netdb.h> #include <stddef.h> #include <unistd.h> #include <strings.h> #include <sys/socket.h> int tcp_connect(const char *host, const char *serv) { int sockfd, n; struct addrinfo hints, *res, *ressave; bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) { err_quit( "tcp_connect error for %s, %s: %s", host, serv, gai_strerror(n) ); } ressave = res; do { sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd < 0) { continue; /* ignore this one */ } if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0) { break; /* success */ } close(sockfd); /* ignore this one */ } while ( (res = res->ai_next) != NULL); if (res == NULL) { /* errno set from final connect() */ err_sys("tcp_connect error for %s, %s", host, serv); } freeaddrinfo(ressave); return(sockfd); }
以上是关于tcp_connect函数的主要内容,如果未能解决你的问题,请参考以下文章
在 Visual Studio 中创建构造函数的代码片段或快捷方式