代码非常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. go语言生成uuid

    操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 golang没有提供生成uuid的接口,但开发中确实需要uuid. 这里把看到的代码记录下,也方便我以后查阅. 解决方案 ...

  2. J2EE开发之三种项目架构

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6285069.html 在我们开发项目时,一般都要先划分好哪些是与用户交互的,哪些用来处理请求/数据等等,这些过 ...

  3. Java项目持续集成检查项

    1)   检查项:使用Super POM. 2)   检查项:GroupID应该以特定名称开头.3)   检查项:版本号符合规范.版本号必须是三段数字,之后加或不加-SNAPSHOT.4)   检查项 ...

  4. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

  5. 〖Linux〗Kubuntu KDE开机后总是提示“system program problem detected”的解决方法

    自从从Ubuntu切换到了Kubuntu之后,就经常在开机的时候提示“system program problem detected”: 查看 /var/crash/ 发现都是一些无关痛痒的程序在关机 ...

  6. spring 配置bean的方法及依赖注入发方式

    Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...

  7. sql如何通过当前日期获取上周,上上周,上上上周的起始日期(周一_周七)

    当前时间周的起始日期(以周一为例)select DATEADD(week,DATEDIFF(week,0,getdate()),0)上周起始:select dateadd(week,-1,DATEAD ...

  8. JVM常见面试题

    1. 内存模型以及分区,需要详细到每个区放什么. 栈区: 栈分为java虚拟机栈和本地方法栈 重点是Java虚拟机栈,它是线程私有的,生命周期与线程相同. 每个方法执行都会创建一个栈帧,用于存放局部变 ...

  9. Redis五大数据类型常用命令脑图

  10. (原)luarocks更新某个模块

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6424398.html 参考网址: https://github.com/torch/nn/issues ...