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 ...
随机推荐
- Mysql Left Join Where On
select t1.id,t2.idfrom t1left join t2 on t1.id = t2.id and t1.id>1 and t2.id<>3在mysql的left ...
- 〖Linux〗git push orgin master不能解析域名的解决方法
错误信息: $ git push origin master ssh: Could not resolve hostname bitbucket.org: Name or service not kn ...
- Lotus Domino开发心得(一)
—- Lotus Domino 是当今办公自动化系统的主流开发平台之一,目前大部分企业和机构都在使用Lotus Domino 开发自己的无纸办公系统.在开发过程中,我积累了一些小技巧,现在公布出来,希 ...
- nginx 代理静态资源报 403
用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R ...
- Highcharts X轴纵向显示
xAxis: { categories: ['苹果', '橘子', '梨', '葡萄', '香蕉'], labels:{ rotation: 90, style:{ fontSize: '13px', ...
- Cocos2d-x执行时错误:Cocos2d: Get data from file(xxx.xxx) failed!
取资源图片时遇到执行时错误: Cocos2d: Get data from file(xxx/xxx.xxx) failed! 原因是我加入资源目录的方式不正确,例如以下图,我选择的是在Resourc ...
- CentOS设置开机自动启动某服务
CentOS设置开机自动启动某服务 这里以启动sshd服务为例: 查看sshd是否已经是系统服务: # chkconfig --list |grep sshd 会显示: sshd ...
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...
- Solr删除managedschema
一.创建solr核心 solr createcore 二.删除managedschema managedschema是solr自动生成的,里面包含大量无用配置. solr是检查用户定义的schema. ...
- java 结束程序进程 代码
结束firefox的进程,一句代码就够了,如下: Runtime.getRuntime().exec("taskkill /F /IM firefox.exe"); 结束qq: R ...