日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux c select函数使用求解释

發(fā)布時(shí)間:2025/3/15 linux 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux c select函数使用求解释 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ? 代碼非常easy,就是發(fā)送c語(yǔ)言發(fā)送http請(qǐng)求。但?i= read(sockfd, buf, BUFSIZE-1); 能夠正常執(zhí)行,?

替換為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; }

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的linux c select函数使用求解释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。