linux c select函数使用求解释
来源:互联网 发布:centos查看防火墙端口 编辑:程序博客网 时间:2024/06/06 14:05
代码很简单,就是发送c语言发送http请求,但 i= read(sockfd, buf, BUFSIZE-1); 可以正常运行,
替换为i= Read(sockfd, buf, BUFSIZE-1);后程序退出,不知什么原因,求解答。
#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h>#include <errno.h>#include <signal.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/wait.h>#include <sys/time.h>#include <netinet/in.h>#include <arpa/inet.h> #define IPSTR "180.97.33.107"#define PORT 80#define BUFSIZE 1024 * 1024 * 2int Read(int fd, char *buf, int count) { int nread, totlen = 0; while (totlen != count) { nread = read(fd, buf, count - totlen); if (nread == 0) return totlen; if (nread == -1) return -1; totlen += nread; buf += nread; } return totlen;} int main(int argc, char **argv){ int sockfd, ret, i, h; struct sockaddr_in servaddr; char str1[4096], buf[BUFSIZE]; socklen_t len; fd_set t_set1; struct timeval tv; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf("---socket error!\n"); exit(0); }; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ printf("--inet_pton error!\n"); exit(0); }; if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ printf("connect error!\n"); exit(0); } memset(str1, 0, 4096); strcat(str1, "GET / HTTP/1.1\n"); strcat(str1, "Host: www.baidu.com\n"); strcat(str1, "\n\n"); printf("%s",str1); ret = write(sockfd,str1,strlen(str1)); if (ret < 0) {printf("errno = %d strerror = %s\n",errno, strerror(errno)); exit(0); }else{printf("send %d size\n", ret); } FD_ZERO(&t_set1); FD_SET(sockfd, &t_set1); while(1){ tv.tv_sec= 2; tv.tv_usec= 0; h= 0; printf("--------------->1\n"); h= select(sockfd +1, &t_set1, NULL, NULL, &tv); printf("--------------->2\n"); printf("h =================== %d\n", h); //if (h == 0) continue; if (h < 0) { close(sockfd);printf("select error \n"); return -1; } if (h > 0){ memset(buf, 0, BUFSIZE); i= read(sockfd, buf, BUFSIZE-1); //i= Read(sockfd, buf, BUFSIZE-1);printf("i ========================== %d", i); if (i==0){ close(sockfd);printf("stop .................\n"); return -1; } printf("%s\n", buf); } } close(sockfd); return 0;}
0 0
- linux c select函数使用求解释
- Linux C的select函数的使用
- linux C select函数学习
- C语言 select函数使用
- C语言 select函数使用
- SOCKET select函数解释
- linux select 函数使用Demo
- linux select 解释
- select函数详细解释(Linux下网络编程)
- socket通信中select函数的使用和解释
- socket通信中select函数的使用和解释
- socket通信中select函数的使用和解释
- linux c语言 select函数用法
- linux下c语言select函数用法
- linux下c语言select函数用法
- linux下c语言select函数用法
- linux下c语言select函数用法
- 【转】linux c语言 select函数用法
- ios一些关键字
- MFC:判断点(POINT)是否在某一区域(CRect)
- google访问
- 梯度下降法-线性拟合
- java获取随机数之一
- linux c select函数使用求解释
- P122.39
- Mysql 配置主从
- 对const和static的理解
- _BIN 二进制排序
- 项目引用了DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称
- 解析url中search
- POJ 3253 Fence Repair
- GCD网络多线程---同步执行,异步执行,串行队列,并行队列