linux c select函数使用求解释
代码非常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函数使用求解释的更多相关文章
- linux select函数:Linux下select函数的使用详解【转】
本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...
- Linux下select函数的使用
一.Select 函数详细介绍 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv ...
- Linux驱动 - select函数介绍
一.select 函数介绍 select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型: #include & ...
- 转:linux中select()函数分析
源地址:http://blog.csdn.net/zi_jin/article/details/4214359 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱 ...
- Linux C select函数详解
select IO复用机制: http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html http://blog.csdn.ne ...
- linux select函数详解
linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...
- socket通信时如何判断当前连接是否断开--select函数,心跳线程,QsocketNotifier监控socket
client与server建立socket连接之后,如果突然关闭server,此时,如果不在客户端close(socket_fd),会有不好的影响: QsocketNotifier监控socket的槽 ...
- linux select函数详解【转】
转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...
- linux c语言 select函数用法
linux c语言 select函数用法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<unis ...
随机推荐
- 腾讯云ubuntu安装Mysql并配置远程访问
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6378914.html 一:修改SSH配置 输入 su 进入root模式.修改ssh配置: sudo vi /e ...
- 在openerp撰写消息中增加图片
openerp的撰写消息中, 在文本输入框中, 具有设置文本字体,设置对齐方式 等多种功能, 就像像写这篇新浪blog一样, 可以输入富文本信息. 美中不足的是, 它不能插入图片. 我们如何才能让op ...
- leetcode笔记:Bulls and Cows
一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a numbe ...
- android手机导入.cer证书文件的方法
访问很多https协议的网站需要安装证书,手机也可以导入cer文件,你知道么?本文将通过简单的两步告诉你手机安装cer文件的方法. 步骤一:请先将数字证书文件“******.cer”文件复制到SD卡的 ...
- Android 仿知乎创意广告
代码地址如下:http://www.demodashi.com/demo/14904.html 一.概述 貌似前段时间刷知乎看到的一种非常有特色的广告展现方式,即在列表页,某一个Item显示背后部分广 ...
- Ubuntu 12.04安装VMware Workstation8.0.3
2012-06-18 12:52 Ubuntu安装VMware Workstation8.0.3 由于使用Ubuntu的人比较少,网上关于Ubuntu的资料也很少,笔者在安装VMware Wo ...
- ss is one another utility to investigate sockets(特适合大规模tcp链接)
原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: ss is one another utility to investigate sockets(特适合大规模tcp链接) 具体的可以 ...
- 多级菜单系统安装维护shell脚本实现企业级案例
演示效果: 1.一级菜单 2.二级菜单 3.执行操作 脚本参考: #!/bin/bash #author lic(oldboy linux student) #date 1304 DISK_NO=&q ...
- 【转】细说UI线程和Windows消息队列
在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...
- html5界面手机播放mp3
1把这段代码复制到htm5界面. <audio id="audio" src="2.mp3" style="opacity:0" pr ...