void fetchtask(void *v) { int fd, n; char buf[512]; struct timeval stTv; //fprintf(stderr, "starting...\n"); for(;;){ if((fd = netdial(TCP, server, 80)) < 0){ fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate()); continue; } stTv.tv_sec = 60; stTv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&stTv, sizeof(stTv)); snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(fd, buf, strlen(buf)); while((n = fdread(fd, buf, sizeof buf)) > 0) ; if (n < 0) { fail++; }else{ sucess++; } close(fd); } }
void fetchtask(ltctx *lt, void *v) { int fd, n; char buf[512]; fprintf(stderr, "starting...\n"); for(;;) { if((fd = netdial(lt, TCP, server, 80)) < 0) { fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate(lt)); continue; } snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(lt, fd, buf, strlen(buf)); while((n = fdread(lt, fd, buf, sizeof buf)) > 0) ; close(fd); write(1, ".", 1); } }
void fetchtask(void *v) { int fd, n; char buf[512]; fprintf(stderr, "starting...\n"); for (; ;) { if ((fd = netdial(TCP, server, 80)) < 0) { // 异步连接服务器, 会造成协程切换 fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate()); continue; } snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(&fd, buf, strlen(buf)); // 异步数据读写, 这里可能会造成协程切换, 因为一定有阻塞操作 while ((n = fdread(&fd, buf, sizeof buf)) > 0) { // 异步读取 buf[n] = '\0'; printf("buf:%s", buf); } close(fd); write(1, ".", 1); } }