怎么实现带超时功能的 connect 函数
来源:互联网 发布:淘宝为啥登不上去 编辑:程序博客网 时间:2024/04/30 04:44
怎么实现带超时功能的 connect 函数
原理:
今天仔细看了 man connect,明白了错误的原因:
EINPROGRESS
The socket is non-blocking and the connection
cannot be completed immediately. It is possible
to select(2) or poll(2) for completion by
selecting the socket for writing. After
select(2) indicates writability, use getsock-
opt(2) to read the SO_ERROR option at level
SOL_SOCKET to determine whether connect() com-
pleted successfully (SO_ERROR is zero) or unsuc-
cessfully (SO_ERROR is one of the usual error
codes listed here, explaining the reason for the
failure).
实现:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <time.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int fd, retval;
struct sockaddr_in addr;
struct timeval timeo = {3, 0};
socklen_t len = sizeof(timeo);
fd_set set;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (argc == 4) timeo.tv_sec = atoi(argv[3]);
int savefl = fcntl(fd,F_GETFL);
fcntl(fd, F_SETFL, savefl | O_NONBLOCK);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
printf("%d/n", time(NULL));
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0)
{
close(fd);
printf("connected ... 1/n");
return 0;
}
if (errno != EINPROGRESS)
{
close(fd);
perror("connect ... 2");
return -1;
}
FD_ZERO(&set);
FD_SET(fd, &set);
retval = select(fd + 1, NULL, &set, NULL, &timeo);
if (retval == -1)
{
close(fd);
perror("select");
return -1;
}
else if(retval == 0)
{
close(fd);
fprintf(stderr, "timeout/n");
printf("%d/n", time(NULL));
return 0;
}
if(FD_ISSET (fd,&set)) //|| FD_ISSET(SockFd,&wset))
{
int error = 0;
socklen_t len = sizeof (error);
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
{
close(fd);
printf ("getsockopt fail,connected fail/n");
return -1;
}
if(error == ETIMEDOUT)
{
printf ("connected timeout/n");
}
if(error == ECONNREFUSED)
{
close(fd);
printf("No one listening on the remote address./n");
return -1;
}
}
printf ("connected ... 3/n");
fcntl(fd, F_SETFL, savefl);
close (fd);
return 0;
}
- 怎么实现带超时功能的 connect 函数
- 用asio的定时器实现带超时的connect,备忘
- TcpClient.Connect函数连接超时的问题
- TcpClient.Connect函数连接超时的问题
- TCP解决connect函数的超时问题
- 实现可以指定超时的connect
- connect函数超时设置
- 封装read、write、accept、connect函数,实现超时操作。
- C语言实现带自定义超时时间的telnet端口连通性检测功能
- connect的超时设置
- socket connect 函数设置超时
- socket connect 函数设置超时
- setsockopt函数--设置connect超时
- Linux下实现connect超时
- select实现connect超时连接
- 通过select模型实现具有超时设定的connect
- JAVA SOCKET connect超时设置是如何实现的?
- 在C#中带连接超时功能的TcpClient类
- 存储过程实现无限级分类
- [面试英语] 面试时如何自我介绍
- Windows系统多重启动的重要文件
- C++动态分配二维/三维数组
- 在DropDownList中显示树形结构
- 怎么实现带超时功能的 connect 函数
- 网站建设的相关技巧
- Linux rm 命令祥解
- C#3.0之隐含类型局部变量
- flas的块有大小之分
- 使用route命令解决一机双网访问
- 《卓有成效的管理者》读书笔记
- 存折一面分两页合理吗?
- 英语面试汇集