代码非常easy,就是发送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 * 2 int 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;
}

linux c select函数使用求解释的更多相关文章

  1. linux select函数:Linux下select函数的使用详解【转】

    本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...

  2. Linux下select函数的使用

    一.Select 函数详细介绍 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv ...

  3. Linux驱动 - select函数介绍

    一.select 函数介绍 select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:          #include & ...

  4. 转:linux中select()函数分析

    源地址:http://blog.csdn.net/zi_jin/article/details/4214359 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱 ...

  5. Linux C select函数详解

    select IO复用机制: http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html http://blog.csdn.ne ...

  6. linux select函数详解

    linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...

  7. socket通信时如何判断当前连接是否断开--select函数,心跳线程,QsocketNotifier监控socket

    client与server建立socket连接之后,如果突然关闭server,此时,如果不在客户端close(socket_fd),会有不好的影响: QsocketNotifier监控socket的槽 ...

  8. linux select函数详解【转】

    转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...

  9. linux c语言 select函数用法

    linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...

随机推荐

  1. Android 建立手机与手表数据同步机制总结

    Android Wear 数据同步机制总结 当手机与手表建立蓝牙连接之后.数据就能够通过Google Play Service进行传输. 同步数据对象Data Item DataItem提供手机与手表 ...

  2. Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  3. 【Linux】eclipse juno 边框过大的调整方法

    切换至eclipse目录: vi plugins/org.eclipse.platform_4.2.*/css/e4_default_gtk.css 找到.MPartStack,替换成如下代码: .M ...

  4. Selenium上传文件

    selenium自带了对应的API可以上传问题,如果这个上传文件的html code中显示的type是file那么你就可以使用下面的代码上传文件. /** * click the upload but ...

  5. Selenium Page object Pattern usage

    使用Selenium的framework,大家免不了要使用他的page object pattern来开发适合自己的framework,原因很简单,page object 可以将测试的对象抽象成一个个 ...

  6. github不能访问、加载css、js解决办法

    很奇怪,白天在公司还能正常访问github,晚上回来访问却有问题,表现症状是页面加载慢,并且页面样式明显错乱. 在chrome下用F12开发者工具一看,有2条css和2条js 404 了,猜想应该是g ...

  7. 无限极分类php实现—查子孙树、家谱树

    1.本文更新日期:2018/05/20 , 亲测可用,在原有基础上进行增强和 详细化 . 2.面包屑导航 和 子孙树 效果图如下: 3.代码: <?php // 无限级分类中,查家谱树(面包屑导 ...

  8. 【RS】RankMBPR:Rank-Aware Mutual Bayesian Personalized Ranking for Item Recommendation - RankMBPR:基于排序感知的相互贝叶斯个性化排序的项目推荐

    [论文标题]RankMBPR:Rank-Aware Mutual Bayesian Personalized Ranking for Item Recommendation ( WAIM 2016:  ...

  9. android studio中使用git版本管理

    转载请标注来源:http://blog.csdn.net/lsyz0021/article/details/51842774 AndroidStudio中使用Git-初级篇(一)——从github上传 ...

  10. java单双派机制理解

    引言:Java是一个支持双分派的单分派语言 知道java是支持后期动态绑定,也知道访问者模式,但是两者之间的关系,还真不知道,况且在这之间还有一个静态绑定. 1.动态绑定 class Father { ...