Chinaunix
标题:
UNP上的 非阻塞connect Web客户程序,connect()函数出错!!
[打印本页]
作者:
Zhc1993
时间:
2012-10-14 15:43
标题:
UNP上的 非阻塞connect Web客户程序,connect()函数出错!!
我是按照书上的代码写的,
在 home_page() 函数中的 tcp_connect()里,程序终止了
是 connect() 出现 Invalid arguments;
tcp_connect()就是 UNP P255页的代码。int tcp(const char *host,cosnt char * serv);
host 我写的是 "www.baidu.com"
serv "80"
connect() 无法成功,怎么解决??懂的人帮一下忙,感激不尽!!
作者:
linux_c_py_php
时间:
2012-10-14 19:45
tcp_connect的代码你贴一下.
作者:
Zhc1993
时间:
2012-10-14 21:53
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)
{
perror("get addrinfo error\n");
exit(0);
}
ressave = res;
if(ressave == NULL)
{
printf("can't get the addrinfo\n");
}
do
{
sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
if(sockfd < 0)
continue;
if(connect(sockfd,res->ai_addr,res->ai_addrlen) == 0)
break;
close(sockfd);
}while((res = res->ai_next) != NULL);
if(res == NULL)
{
perror("tcp connect error\n");
exit(0);
}
freeaddrinfo(ressave);
return sockfd;
}
复制代码
作者:
Zhc1993
时间:
2012-10-14 21:59
真巧,我在csdn上也问了这个问题,第一个回答的人的头像也是这条蛇!
1..jpg
(26.12 KB, 下载次数: 37)
下载附件
2012-10-14 21:57 上传
作者:
linux_c_py_php
时间:
2012-10-14 22:26
还真没发现错误, 而且我试验也是可以正常连接的.
tcp 0 1 119.254.35.221:41980 220.181.111.147:80 FIN_WAIT1 -
无效参数很费解, 不行你在hint里指定一下ai_family = AF_INET;吧。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2