Linux C语言Select函数使用求解释
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;
}
Cheap Viagra From India Kamagra Jelly Test Cialis 20 Mg 30 St [url=http://drugslr.com]generic 5mg cialis best price[/url] Amoxicillin And Green Tea Amoxil Penicilline Viagra Farmacia Online
Generic Viagra No Precription Cialis Drogue Cialis 5 Mg Lilly Deutschland Gmbh [url=http://banzell.net]viagra[/url] Cialis Precio En Espana
Cephalexin Strenght Order Levothyroxine 125mcg Online Amoxicillin Dosage Tooth Infection [url=http://crdrugs.com]cialis price[/url] Cialis Professional Generique 20 Mg
Furosemide Without Prescription Viagra.Es Cephalexin And Mrsa [url=http://xbmeds.com][/url] Comprar Cialis De Forma Segura Viagra O Cialis Viagra Plus Alcohol
Levitra In Usa Cialis Per Posta Keflex For Gum Infection [url=http://buycheapciall.com]cialis overnight shipping from usa[/url] Achat Cialis Au Quebec Acheter Cialis Allemagne Mucinex And Antibiotics Keflex Together
No Prescription Foreign Pharmacies Viagra Online Nz Order Accutane Online Uk [url=http://mpphr.com]dapoxetine 60 mg[/url] Cialis Non Prescription Needed
Cialis 20 Mg Dosaggio Female Viagra Reviews [url=http://oc-35.com]finasteride 5 mg for sale[/url] Keflex How To Take