您的位置:首页 > 其它

SO_SNDTIMEO和SO_RCVTIMEO

2017-08-23 16:24 316 查看
               SO_SNDTIMEO和SO_RCVTIMEO这两个套接字选项用来设置超时时间的,看代码吧。

[mapan@localhost sockOption]$ ls
client.cpp  makefile  server.cpp
[mapan@localhost sockOption]$ cat client.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096

int main(int argc,char **argv)
{
int connfd,ret;
char sendbuf[400000]={0};
struct sockaddr_in servaddr;

if(argc != 2)
{
printf("error\n");
}

connfd=socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(6666);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

connect(connfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

struct timeval stTimeValStruct;
stTimeValStruct.tv_sec=5;
stTimeValStruct.tv_usec=0;

setsockopt(connfd,SOL_SOCKET,SO_SNDTIMEO,&stTimeValStruct,sizeof(stTimeValStruct));
while(1)
{
ret= write(connfd,sendbuf,sizeof(sendbuf));
printf("ret=%d\n",ret);
}

close(connfd);
return 0;
}

[mapan@localhost sockOption]$ cat server.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096

int main()
{
int listenfd,acceptfd;
struct sockaddr_in servaddr;

listenfd=socket(AF_INET,SOCK_STREAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(6666);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,10);

acceptfd=accept(listenfd,(struct sockaddr *)NULL,NULL);

getchar();
close(acceptfd);
close(listenfd);
return 0;
}

[mapan@localhost sockOption]$ cat makefile
all:server client

server.o:server.cpp
g++ -c server.cpp
client.o:client.cpp
g++ -c client.cpp
server:server.o
g++ -o server server.o
client:client.o
g++ -o client client.o

clean:
rm -f server client *.o
[mapan@localhost sockOption]$
编译并运行,客户端需要新打开一个窗口执行。

[mapan@localhost sockOption]$ make
g++ -c server.cpp
g++ -o server server.o
g++ -c client.cpp
g++ -o client client.o
[mapan@localhost sockOption]$ ./server
[mapan@localhost sockOption]$ ./client 127.0.0.1
ret=400000
ret=400000
ret=400000
ret=254012
ret=-1
ret=-1
^C
[mapan@localhost sockOption]$


再看看看接收缓冲区和发送缓冲区:

[mapan@localhost ~]$ netstat -nap | grep 6666
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:6666                0.0.0.0:*                   LISTEN      6151/./server
tcp   138900      0 127.0.0.1:6666              127.0.0.1:59104             ESTABLISHED 6151/./server
tcp        0 1315113 127.0.0.1:59104             127.0.0.1:6666              FIN_WAIT1   -
[mapan@localhost ~]$


虽然我们没有获取到发送缓冲区的最大的上限值,但是发送缓冲区确实满了,write返回-1,证明write失败了。如果我们没有设置超时时间,write就会一直阻塞在那里。

发送缓冲区为什么会满?你首先要懂得write是干嘛的,它可不是发送数据。write是将用户进程中的数据拷贝到发送缓冲区中,如果发送缓冲区满了,write就会返回-1。发送数据是跟write没有关系的,那是协议做的事。由netstat可以看到对端接收缓冲区有数据,而且已经满了。所以数据堆积在发送缓冲区发不出去,导致发送缓冲区满了。

我们设置超时时间为5秒,write会阻塞在那里5秒,然后返回。

参考资料:unix网络编程卷一
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: