69,373
社区成员
发帖
与我相关
我的任务
分享
fd_set rset;
struct timeval tv;
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
tv.tv_sec = 10;//超时时间
tv.tv_usec = 0;
select(sockfd + 1, &rset, NULL, NULL, &tv)
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 non-
blocking. If the timeout is set to zero (the default) then the operation will never timeout.
/*客户端*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<netinet/in.h>
#include<unistd.h>
#include<sys/time.h>
#include <errno.h>
#define SERVERPORT 5556
int main(int argc,char *argv[])
{
int sockfd,connectfd=0,recvfd,sendfd ;
struct sockaddr_in serv_addr;
struct hostent *host;
FILE *fp;
char ch[1024];
int byte_send=0;
//int a=MSG_DONTWAIT;
//int opt=MSG_DONTWAIT;
fd_set rfds;
struct timeval tv;
int retval, maxfd;
if(argc!=3)
{
printf("format: %s IP file_name\n",argv[0]);
return 0;
}
if((host=(gethostbyname(argv[1])))==NULL)
{
printf("error in gethostby name. \n");
return 0;
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("error in socket create.\n");
return 0;
}
printf("创建socket成功,sockfd=%d\n",sockfd);
//printf("运行至此1\n");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port =htons(SERVERPORT);
serv_addr.sin_addr=*((struct in_addr*)host->h_addr);
bzero(&(serv_addr.sin_zero),sizeof(serv_addr.sin_zero));
//printf("运行至此2\n");
while(1)
{
if((connectfd=connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr )))==-1)
{
printf("error in connect.\n");
printf("连接失败:connectfd=%d\n",connectfd);
sleep(5);
continue;
}
printf("连接成功:connectfd=%d\n",connectfd);
break;
}
//printf("运行至此3\n");
fp=fopen(*(argv+2),"rb");
memset(ch,0,sizeof(ch));
while(1)
{
memset(ch,0,sizeof(ch));
sendfd=fread(ch,sizeof(char),1024,fp);
if(sendfd<0)
break;
sleep(1);
//printf("sendfd=%d\n",sendfd);
byte_send=send(sockfd,ch,sendfd,0);
//printf("byte_send=%d,%s\n",byte_send,strerror(errno));
//memset(&ch,0,sizeof(ch));
//printf("发送内容:%s\n",ch);
sleep(3);
while(1)
{
//void FD_ZERO (fd_set *fdset); // 初始化文件描述集合
//void FD_SET (int fd, fd_set *fdset); // 将描述符加入到集合中
//void FD_CLR (int fd, fd_set *fdset); // 将描述符中集合中删除
//int FD_ISSET (int fd, fd_set *fdset); // 检查描述符集合中指定的文件描述符是否可读写
/*把可读文件描述符的集合清空*/
FD_ZERO(&rfds);
/*把标准输入的文件描述符加入到集合中*/
FD_SET(0, &rfds);
maxfd = 0;
/*把当前连接的文件描述符加入到集合中*/
FD_SET(sockfd, &rfds);
/*找出文件描述符集合中最大的文件描述符*/
if(maxfd < sockfd)
maxfd = sockfd;
/*设置超时时间*/
tv.tv_sec = 5;
tv.tv_usec = 0;
/*等待聊天*/
retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
if(retval == -1)
{
printf("select出错,客户端程序退出\n");
break;
}
else if(retval == 0)
{
//printf("客户端没有任何输入信息,并且服务器也没有信息到来,waiting...\n");
//continue;
printf("客户端输出信息完毕,等待下一次输出\n");
break;
}
else
{
/*服务器发来了消息*/
if(FD_ISSET(sockfd,&rfds))// 检查描述符集合中指定的文件描述符是否可读写
{
/******接收消息*******/
memset(ch,0,sizeof(ch));
recvfd=recv(sockfd,ch,sizeof(ch),0);
if(recvfd==-1)
{
//printf("运行至此7\n");
printf("error in recv.\n");
continue;
}
//printf("recvfd=%d\n",recvfd);
printf("%s\n",ch);
}
}
}
}
printf("传送结束!\n");
fclose(fp);
close(connectfd);
return 0;
}