首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

TCP/IP接收数据的超随时间设置

2012-10-17 
TCP/IP接收数据的超时时间设置timeout.tv_sec time_out//设置超时stimeout.tv_usec 0ret select(s

TCP/IP接收数据的超时时间设置
timeout.tv_sec = time_out;//设置超时s
timeout.tv_usec = 0;
ret = select(sock+1, &recv_mask, NULL, NULL, &timeout);
这个是linux下 可以设置超时时间的。

windows,VC下设置超时时间的是那个呢?
 recv(sock, szBuff, 1024, 0);这个函数吗?

[解决办法]
有两种方法:
1. 像你写的一样用 select.
2. 用setsockopt(soc, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
论坛中的源码:http://download.csdn.net/detail/geoff08zhang/4571358
解压后找DaytimeClnt。
[解决办法]
recv带超时。。。如果为0则阻塞接收

tcp本身各个平台没啥区别,只是参数名称在sun上面有区别
[解决办法]
NAME
getsockopt, setsockopt - get and set options on sockets

SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>

int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen);

int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen);

SO_RCVTIMEO and SO_SNDTIMEO
Specify the receiving or sending timeouts until reporting an error. The parameter is a struct timeval. If an input or output function blocks for
this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been
transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK just as if the socket was specified to be
nonblocking. If the timeout is set to zero (the default) then the operation will never timeout.

热点排行