SO_SNDTIMEO和SO_RCVTIMEO

               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网络编程卷一

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值