select_socket 网络超时编程实例
来源:互联网 发布:樱井知香2016最新作品 编辑:程序博客网 时间:2024/04/30 02:54
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/in.h>
#define N 100
/*
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
*/
int main(void)
{
int sokfd;
int connfd;
struct sockaddr_in sev_sockaddr;
struct sockaddr_in client_sockaddr;
fd_set readfds;
fd_set readfdstemp;
int nfds;
int i=0;
int recvn=0;
char buf[N];
int client_sockaddr_len=sizeof(client_sockaddr);
if(( sokfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("scoket");
exit(-1);
}
bzero(&sev_sockaddr,sizeof(sev_sockaddr));
sev_sockaddr.sin_family=AF_INET;
sev_sockaddr.sin_port=htons(9000);
sev_sockaddr.sin_addr.s_addr=inet_addr("192.168.1.100");
if(bind(sokfd,(struct sockaddr*)&sev_sockaddr,sizeof(sev_sockaddr))==-1)
{
perror("bind");
exit(-1);
}
listen(sokfd,5);
struct timeval timeo={5,0};
FD_ZERO(&readfds);
FD_SET(sokfd,&readfds);
nfds=sokfd;
while(1)
{
//FD_ZERO(&readfds);
//FD_SET(sokfd,&readfds);
readfdstemp=readfds;
//timeo={5,0};
timeo.tv_sec=5;
timeo.tv_usec=0;
/*************************************************************************
timeout is an upper bound on the amount of time elapsed before select()
returns. If both fields of the timeval stucture are zero, then
select() returns immediately. (This is useful for polling.) If time鈥? out is NULL (no timeout), select() can block indefinitely.
************************************************************************/
/*************************************************************************
On success, select() and pselect() return the number of file descrip鈥? tors contained in the three returned descriptor sets (that is, the
total number of bits that are set in readfds, writefds, exceptfds)
which may be zero if the timeout expires before anything interesting
happens. On error, -1 is returned, and errno is set appropriately; the
sets and timeout become undefined, so do not rely on their contents
after an error.
*********************************************************************/
int ret=100000;
if((ret=select(nfds+1,&readfdstemp,NULL,NULL,&(timeo)))==-1)
{
perror("select");
exit(-1);
}
printf("time :%d\n",ret);
if(ret>0)
{
for(i=0;i<nfds+1;i++)
{
if(FD_ISSET(i,&readfdstemp))
{
if(i==sokfd)
{
connfd=accept(sokfd,(struct sockaddr*)&client_sockaddr,&client_sockaddr_len);
nfds=(nfds>connfd)?nfds:connfd;
FD_SET(connfd,&readfds);
}
else
{
bzero(buf,sizeof(buf));
recvn=recv(i,buf,10,0);
if(recvn==0)
{
close(i);
FD_CLR(i,&readfds);
break;
}
printf("%s\n",buf);
}
}
}
}
else
{
printf("the net is timeout...\n");
}
}
}
- select_socket 网络超时编程实例
- 网络编程中的超时检测
- 网络编程中的超时检测
- 网络编程中的超时检测
- 网络编程中的超时检测
- 网络编程中的超时检测
- 网络编程中的超时检测
- 网络编程设置时间超时
- 网络编程中的超时检测
- 【Linux网络编程】超时检测
- 网络编程中的超时检测
- android网络编程及网络超时处理
- UNIX网络编程--设置I/O超时
- 网络编程中如何设置连接超时
- 网络编程学习笔记(套接口超时)
- socket网络编程的超时设置
- VC网络编程实例
- 网络编程实例
- flash如何同时访问本地文件系统和网络文件
- 【面试】面试经典问答和回答思路
- Hibernate中悲观锁和乐观锁
- GNU ARM汇编--(三)ARM处理器的基本原则
- 测试转开发,一个女孩子短短的工作心得
- select_socket 网络超时编程实例
- 程序员笔试题--2012百度实习生笔试题
- S3C6410裸机驱动5'TFT LCD
- yuv 422 1/4降采样应该注意的地方
- sql 树形累加函数的实现
- dedecms 织梦cms系统 显示父级栏目名称
- Google code jam Round 1B
- 正十七边形画法
- HOWTO: How to Provide Your Own DllMain in an MFC Regular DLL