int main(int argc, char **argv){ struct sockaddr_in *remote; int sock; int tmpres; char *ip: char *get; char buf[BUFSIZ+1]; char *host; char *page; /* program is called without option */ if(argc==1){ usage(); exit(2); } /* program is called with host and page */ if(argc > 2){ page = argv[2]; } /* program is called without page - using default page */ else{ page=PAGE; } /* create client socket */ if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0){ perror("Error creating client socket\n"); exit(1); } ip=get_ip(host); fprintf(stderr,"IP address is %s\n", ip); remote=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)); /* setting the address family */ remote->sin_famiily=AF_INET; tmpres=inet_pton(AF_INET,ip,(void *)(&(remote->sin_addr.s_addr))); if(tmpres < 0){ perror("Can't set sin_addr.s_addr \n"); exit(1); } else if(tmpres==0){ fprintf("%s is not a valid IP address\n",ip); exit(1); } remote->sin_port=htons(PORT); /* connecting to the webserver */ if(connect(sock,(struct sockaddr *)remote,sizeof(struct sockaddr)) <0){ perror("Can't connect to the webserver\n"); exit(1); } get=build_get_query(host,page); }
int main() { char url[200]; char buf[BUFSIZ+1]; int port = 80; struct sockaddr_in serverAddress; struct hostent* dnsResolved; printf("Enter the url to acces the images from\n"); printf("Ex: http://www-archive.mozilla.org/quality/networking/testing/datatests.html\n"); scanf("%s",url); printf("length of url is %d\n",strlen(url)); char domainname[strlen(url)]; char *page = (char *)malloc(strlen(url)); //puts(url); getDomainName(url,domainname,page); printf("The domain name is %s\n",domainname); char *ip = (char *)malloc(strlen(url)); char *isDomainName = strstr(domainname, "www"); if(isDomainName != NULL) { getHostIP(domainname,&dnsResolved); ip = (char *)inet_ntoa(dnsResolved->h_addr_list[0]);//"127.0.0.1"; printf("the Ip is %s\n",inet_ntoa(dnsResolved->h_addr_list[0])); } else { ip = domainname; } int sockid = createTcpSocket(); assignAddressToSocket(sockid,&serverAddress,port,"127.0.0.1"); connectSocket(sockid,(struct sockaddr *) &serverAddress,(int)sizeof(serverAddress)); char *getQuery = build_get_query(ip,page); sendQuery(sockid,getQuery); fetchHtmlPage(sockid,buf); int status = closeSocket(sockid); return 0; }
char* http_get(char *host, int port, char *page) { struct sockaddr_in *remote; int sock; int tmpres; char *ip; char *get; char buf[BUFSIZ + 1]; sock = create_tcp_socket(); ip = get_ip(host); fprintf(stderr, "IP is %s\n", ip); remote = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in *)); remote->sin_family = AF_INET; tmpres = inet_pton(AF_INET, ip, (void *) (&(remote->sin_addr.s_addr))); if (tmpres < 0) { perror("Can't set remote->sin_addr.s_addr"); exit(1); } else if (tmpres == 0) { fprintf(stderr, "%s is not a valid IP address\n", ip); exit(1); } remote->sin_port = htons(port); if (connect(sock, (struct sockaddr *) remote, sizeof(struct sockaddr)) < 0) { perror("Could not connect"); exit(1); } get = build_get_query(host, page); fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get); //Send the query to the server int sent = 0; while (sent < strlen(get)) { tmpres = send(sock, get + sent, strlen(get) - sent, 0); if (tmpres == -1) { perror("Can't send query"); exit(1); } sent += tmpres; } //now it is time to receive the page memset(buf, 0, sizeof(buf)); int htmlstart = 0; char * htmlcontent; while ((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0) { if (htmlstart == 0) { /* Under certain conditions this will not work. * If the \r\n\r\n part is splitted into two messages * it will fail to detect the beginning of HTML content */ htmlcontent = strstr(buf, "\r\n\r\n"); if (htmlcontent != NULL) { htmlstart = 1; htmlcontent += 4; } } else { htmlcontent = buf; } if (htmlstart) { fprintf(stdout, htmlcontent); } memset(buf, 0, tmpres); } if (tmpres < 0) { perror("Error receiving data"); } free(get); free(remote); free(ip); close(sock); return htmlcontent; }
int *get_page_thread(struct host_page *hp){ struct sockaddr_in *remote; int sock; int tmpres; char *ip; char *host; char *page; host=hp->host; page=hp->page; char *get; char buf[BUFSIZ+1]; if (!(sock=tcp_connect(host))) { perror("Could not connect"); exit(1); } get = build_get_query(host, page); fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get); //Send the query to the server int sent = 0; while(sent < strlen(get)) { tmpres = send(sock, get+sent, strlen(get)-sent, 0); if(tmpres == -1){ perror("Can't send query"); exit(1); } sent += tmpres; } //now it is time to receive the page memset(buf, 0, sizeof(buf)); int httpstart = 0; char * httpcontent; while((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0){ if(httpstart == 0) { /* Under certain conditions this will not work. * If the \r\n\r\n part is splitted into two messages * it will fail to detect the beginning of HTML content */ httpcontent = strstr(buf, "\r\n\r\n"); if(httpcontent != NULL){ httpstart = 1; httpcontent += 4; } }else{ httpcontent = buf; } if(httpstart){ /* fprintf(stdout, httpcontent); */ fprintf(stdout, "%s", httpcontent); } memset(buf, 0, tmpres); } if(tmpres < 0) { perror("Error receiving data"); } free(get); free(remote); free(ip); close(sock); return 0; }
int getHTML(char *domainName, char *webPageFolder, char* sourceOutput){ struct sockaddr_in *remote; int sock, tmpres; char *ip, *get, buf[BUFSIZ+1], *host, *page; host = domainName; page = (webPageFolder !=NULL)?webPageFolder:PAGE; sock = create_tcp_socket(); ip = get_ip(host); fprintf(stderr, "IP is %s\n", ip); remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)); remote->sin_family = AF_INET; tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr))); if( tmpres < 0) { perror("Can't set remote->sin_addr.s_addr"); exit(1); } else if(tmpres == 0) { fprintf(stderr, "%s is not a valid IP address\n", ip); exit(1); } remote->sin_port = htons(PORT); if(connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ perror("Could not connect"); exit(1); } get = build_get_query(host, page); fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get); //Send the query to the server int sent = 0; while(sent < strlen(get)) { tmpres = send(sock, get+sent, strlen(get)-sent, 0); if(tmpres == -1){ perror("Can't send query"); exit(1); } sent += tmpres; } //Now read in the source code returned memset(buf, 0, sizeof(buf)); int n, buflen = BUFSZ; char *pbuf = sourceOutput; while ((n = recv(sock, pbuf, buflen, 0)) > 0){ pbuf += n; buflen -= n; } if(n < 0) { perror("Error receiving data"); } free(get); free(remote); free(ip); close(sock); return 0; }
//Two mode: //1: download file index cua folder goc hoac down 1 file le //2: download cac files linh tinh con lai //Tra ve file name!! char* download_file(char *path, char *folder_name, int mode) { //Get host, page char *host, *page, *filename; char *html_query; char ip[INET_ADDRSTRLEN]; int DSock; int retcode; //return code struct addrinfo hints; struct addrinfo *info; //pointer to results host = get_host(path); page = get_page(path, host); filename = get_filename(path); printf("Get file name: %s\n",filename); //***Neu khong phai la folder goc thi ko down!!!!!! //Kiem tra co phai downfoler ko, neu la folder thi skip, return 1!! //Folder co dau '/' if(mode==2 && filename[strlen(filename) - 1] == '/') { printf("%s is a sub folder, skip this folder!\n",filename); printf("***NOTE: Only download main folder!!!\n"); free(host); free(page); free(filename); return NULL; } //else, down file nay ^^ //Make the struct empty! memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; //IPv4 hay IPv6 gi cung dc hints.ai_socktype = SOCK_STREAM; //TCP for html connections //hints.ai_flags = AI_PASSIVE; //Auto fill IP //Lay thong tin tu host retcode = getaddrinfo(host,"http",&hints,&info); if(retcode != 0) { fprintf(stderr,"Failed at getaddrinfo(): %s\n", gai_strerror(retcode)); printf("Host: %s\n",host); printf("Page: %s\n",page); return NULL; } //Convert ip stored in info->ai_addr->sin_addr to char* ip with length INET_ADDRSTRLEN inet_ntop(info->ai_family, &(((struct sockaddr_in *)info->ai_addr)->sin_addr),ip,INET_ADDRSTRLEN); DSock = socket(info->ai_family, info->ai_socktype, info->ai_protocol); if(DSock == -1) { //Failed! printf("Cannot create socket!\n"); //clear up struct addrinfo info! freeaddrinfo(info); free(html_query); free(host); free(page); return NULL; } if(connect(DSock,info->ai_addr,info->ai_addrlen) < 0) { printf("Cannot connect to %s - %s\n",host,ip); //clear up struct addrinfo info! freeaddrinfo(info); free(html_query); free(host); free(page); return NULL; } printf("Connected to %s - %s\n",host,ip); html_query = build_get_query(host, page); printf("Query content:\ \n<<BEGIN>>\n%s\n<<END!>>\n",html_query); //###Send html request### //Loop toi khi goi dc het goi tin qua ben server //retcode = 0; int bytes_sent; retcode = 0; do { bytes_sent = send(DSock,html_query,strlen(html_query),0); if(bytes_sent == -1) { printf("Failed to send HTML-request\n"); //clear up struct addrinfo info! freeaddrinfo(info); free(html_query); free(host); free(page); return NULL; } retcode += bytes_sent; }while(retcode < strlen(html_query)); printf("Send HTML-REQUEST succeed\n"); //clear up struct addrinfo info! freeaddrinfo(info); free(html_query); free(host); free(page); //################################## //###Receive response from server### //########## A.K.A - DOWNLOAD ###### FILE *fp; char *temp_name; if(folder_name != NULL) { printf("**Folder name: %s\n",folder_name); temp_name = (char*)malloc(strlen(filename) + strlen(MSSV) + strlen(folder_name) + 10); strcpy(temp_name,folder_name); //strcat(temp_name,"/"); } else { //Chi down 1 file temp_name = (char*)malloc(strlen(filename) + strlen(MSSV) + 2); strcpy(temp_name,MSSV); strcat(temp_name,"_"); if(filename[strlen(filename)-1] == '/') { printf("This is a folder!\n"); filename[strlen(filename)-1] = '\0'; } } strcat(temp_name,filename); printf("File name sau khi cat: %s\n",temp_name); //printf("File name: %s\n",temp_name); /* strcat(temp_name,"_"); printf("File name: %s\n",filename); if(filename[strlen(filename)-1] == '/') { printf("This is a folder!\n"); filename[strlen(filename)-1] = '\0'; } else printf("This is a file!\n"); */ //Xu ly tempname don gian //bool save = false; if(temp_name[strlen(temp_name)-1] == '/') temp_name[strlen(temp_name)-1] = '\0'; fp = fopen(temp_name,"w"); if (fp == NULL) { printf("Failed to write file: %s\n",temp_name); return NULL; //exit(EXIT_FAILURE); } char buffer[BUFFSIZE]; memset(buffer, 0, sizeof(buffer)); int bytes_recv; long total = 0; long total_write = 0; bool htmlstart = 0; char *htmlcontent; printf("HTML content:\n"); while((bytes_recv = recv(DSock,buffer,BUFFSIZE,0)) > 0) { total+=bytes_recv; //printf("First Packet: \n%s\n",buffer); if(htmlstart == 0) { //Skip header!!! htmlcontent = strstr(buffer,"\r\n\r\n"); int header_size = strlen(buffer) - strlen(htmlcontent) + 4; if(htmlcontent != NULL) { htmlstart = 1; htmlcontent += 4; //Skip \r\n\r\n int real_bytes = bytes_recv - header_size; //printf("Header!!\nHTML content: %s\n",htmlcontent); total_write += fwrite (htmlcontent , sizeof(char), real_bytes, fp); //total_write += real_bytes; //printf("%s\n",htmlcontent); memset(buffer,0,bytes_recv); continue; } } else { htmlcontent = buffer; } if(htmlstart == 1) { //Ghi file! total_write += fwrite (buffer, sizeof(char), bytes_recv, fp); printf("%s\n",htmlcontent); } memset(buffer,0,bytes_recv); } if(bytes_recv < 0) { printf("Error when receiving data...\n"); } fclose(fp); printf("TOTAL: %ld bytes\n",total); printf("WROTE: %ld bytes\n",total_write); //################################## //close socket close(DSock); return temp_name; }
void *throw_requests(struct req_struct *req){ char *host = req->host; int port = req->port; int connections=req->connections; int num_of_requests=req->num_of_requests; char *get; //get request to send; int sock[connections]; int i=0;int flags=0; //epoll declarations int epfd; epfd=epoll_create(connections+1); struct epoll_event *events; events = malloc (sizeof (struct epoll_event)*connections); int ret;int nr_events; //end of epoll declarations //remote address block struct sockaddr_in *remote; //remote address structure remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)); //allocating remote address remote->sin_family = AF_INET; inet_pton(AF_INET, get_ip(host), (void *)(&(remote->sin_addr.s_addr))); //setting ip remote->sin_port = htons(port); //setting port for (i = 0; i < connections; i++) { if((sock[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ //creating tcp socket perror("Can't create TCP socket"); exit(1); } // flags = fcntl(sock[i], F_GETFL, 0); // fcntl(sock[i], F_SETFL, flags | O_NONBLOCK); int optval=1; socklen_t optlen = sizeof(optval); if(setsockopt(sock[i], SOL_SOCKET, SO_REUSEADDR|SO_REUSEPORT, &optval, optlen) < 0) { perror("setsockopt()"); exit(EXIT_FAILURE); } if(setsockopt(sock[i], SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { perror("setsockopt()"); exit(EXIT_FAILURE); } events[i].data.fd = sock[i]; events[i].events = EPOLLIN; ret = epoll_ctl (epfd, EPOLL_CTL_ADD , sock[i], &events[i]); } get = build_get_query(host, user_given_filename); //get request to send to remote char *tempget;int sent = 0; ret=0; tempget=get;int len=strlen(get); int progress=num_of_requests/10; gettimeofday(&rt_threads[req->threadID].tv1, NULL); rt_threads[req->threadID].thread_start = (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ; for (i = 0; i < connections; i++) { if(connect(sock[i], (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ //connecting to remote address and ataching to socket printf("%d\n",i ); for (i = 0; i < connections; i++) { close(sock[i]); } exit(1); } //following block sends get string headers to remote address sent = 0;ret=0; while(sent < len) { ret = send(sock[i], get+sent, len-sent, 0); if(ret == -1){ perror("Can't send query"); exit(1); } sent += ret; } get=tempget; // end of send block } gettimeofday(&rt_threads[req->threadID].tv1, NULL); double tm=((rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000); rt_threads[req->threadID].thread_time=rt_threads[req->threadID].thread_time+(tm-rt_threads[req->threadID].thread_start); get=tempget;int tmpsock; char *buf; buf = (char *)malloc(sizeof(char)*1024); int j=0; gettimeofday(&rt_threads[req->threadID].tv1, NULL); rt_threads[req->threadID].thread_start = (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ; // convert tv_sec & tv_usec to mill for (; i < num_of_requests;i=i) { nr_events = epoll_wait (epfd, events,connections, -1); for (j=0;j < nr_events && i < num_of_requests; i++,j++) { // if(i%progress==0 && i!=0)printf("%d completed\n",i ); recv(events[j].data.fd, buf, 1024*1024, 0); if(keep_alive==0) { ret = epoll_ctl (epfd, EPOLL_CTL_DEL,events[j].data.fd, &events[j]); } gettimeofday(&rt_threads[req->threadID].tv1, NULL); double tm=((rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000); rt_threads[req->threadID].thread_time=rt_threads[req->threadID].thread_time+(tm-rt_threads[req->threadID].thread_start); success++; if(success>temp){ temp=temp+(user_given_requests/10); printf("%d finished\n",temp); } //printf("%d %s\n",success ,buf); if(keep_alive==0) { close(events[j].data.fd); /////////////////////////// if((tmpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ //creating tcp socket perror("Can't create TCP socket"); exit(1); } // flags = fcntl(sock[i], F_GETFL, 0); // fcntl(sock[i], F_SETFL, flags | O_NONBLOCK); int optval=1; socklen_t optlen = sizeof(optval); if(setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR|SO_REUSEPORT|SO_KEEPALIVE, (char*)&optval, sizeof(optval)) < 0) { perror("setsockopt()"); exit(EXIT_FAILURE); } events[j].data.fd = tmpsock; events[j].events = EPOLLIN; ret = epoll_ctl (epfd, EPOLL_CTL_ADD, tmpsock, &events[j]); } /////////////////////////// gettimeofday(&rt_threads[req->threadID].tv1, NULL); rt_threads[req->threadID].thread_start = (rt_threads[req->threadID].tv1.tv_sec) * 1000 + (rt_threads[req->threadID].tv1.tv_usec) / 1000 ; // convert tv_sec & tv_usec to mill if(keep_alive==0) { if(connect(events[j].data.fd, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ //connecting to remote address and ataching to socket perror("Could not connect"); exit(1); } } // following block sends get string headers to remote address sent = 0;ret=0; while(sent < len) { ret = send(events[j].data.fd, get+sent, len-sent, 0); if(ret == -1){ perror("Can't send query"); exit(1); } sent += ret; } get=tempget; // end of send block } } // printf("finished %d\n", success); // printf("%d thread finished.\n",(int)pthread_self() ); close(epfd); free(buf); free(events); free(host); free(remote); }
void send_request_on_time(void * vp) { while (sentImageCount < maxImageCount) { struct timespec currentTime; clock_gettime(CLOCK_MONOTONIC, ¤tTime); uint64_t secondsElapsed = currentTime.tv_sec - lastSendTime.tv_sec; // Only send if timer says so. if (secondsElapsed > times[sentImageCount]) { clock_gettime(CLOCK_MONOTONIC, &lastSendTime); sentImageCount++; // Build the query char *get = build_get_query(host, page); fprintf(stderr, "Send Query:\n<<START>>\n%s<<END>>\n", get); //Send the query to the server int sent = 0; int tmpres; while (sent < strlen(get)) { // use a algorithm similar to TTL estimation. if (averageSendTime < 0) { averageSendTime = secondsElapsed * 1000; } else { // the average should be weighted towards the newest information. averageSendTime = ((secondsElapsed * 1000* 0.75) + (averageSendTime*0.25)); } fprintf(stderr, "averageSendTime is now : %d \n", averageSendTime); tmpres = send(sock, get + sent, strlen(get) - sent, 0); if (tmpres == -1) { perror("Can't send query"); exit(1); } sent += tmpres; } free(get); } } // SEND CLOSE ON end char *close = generate_close_message(); fprintf(stderr, "Send Query:\n<<START>>\n%s<<END>>\n", close); //Send the query to the server int sent = 0; int tmpres; while (sent < strlen(close)) { tmpres = send(sock, close + sent, strlen(close) - sent, 0); if (tmpres == -1) { perror("Can't send close query"); exit(1); } sent += tmpres; } }
int main(int argc, char **argv) { struct sockaddr_in *remote; int sock; int tmpres; char *ip; char *get; char buf[BUFSIZ+1]; char *host; char page[URL_MAX_SIZE]; char json_res[RES_MAX_SIZE]; if(argc != 2){ usage(); exit(2); } host = HOST; if(argc == 2){ snprintf(page, URL_MAX_SIZE,"%s%s", SEARCH_URL,argv[1]); } sock = create_tcp_socket(); ip = get_ip(host); fprintf(stderr, "IP is %s\n", ip); remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)); remote->sin_family = AF_INET; tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr))); if( tmpres < 0) { perror("Can't set remote->sin_addr.s_addr"); exit(1); }else if(tmpres == 0) { fprintf(stderr, "%s is not a valid IP address\n", ip); exit(1); } remote->sin_port = htons(PORT); if(connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){ perror("Could not connect"); exit(1); } get = build_get_query(host, page); fprintf(stderr, "Query is:\n<<START>>\n%s<<END>>\n", get); //Send the query to the server int sent = 0; while(sent < strlen(get)) { tmpres = send(sock, get+sent, strlen(get)-sent, 0); if(tmpres == -1){ perror("Can't send query"); exit(1); } sent += tmpres; } //now it is time to receive the page memset(buf, 0, sizeof(buf)); // Buffer for page chunks aggregation memset(json_res, 0, sizeof(json_res)); int htmlstart = 0; char * htmlcontent; while((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0){ if(htmlstart == 0) { /* Under certain conditions this will not work. * If the \r\n\r\n part is splitted into two messages * it will fail to detect the beginning of HTML content */ htmlcontent = strstr(buf, "\r\n\r\n"); if(htmlcontent != NULL){ htmlstart = 1; htmlcontent += 4; } }else{ htmlcontent = buf; } if(htmlstart){ /* fprintf(stdout, "%s", htmlcontent);*/ // fprintf(stdout,"\n\njson2: %s, \n\nhtml2: %s\n\n",json_res, htmlcontent); strncat(json_res, htmlcontent, RES_MAX_SIZE); } memset(buf, 0, tmpres); } // fprintf(stdout, "%s", json_res); /* Parse JSON result */ parseJSON(json_res); if(tmpres < 0) { perror("Error receiving data"); return -1; } free(get); free(remote); free(ip); close(sock); return 0; }
void get_config_from_midware(const char *host, const char *page, char *buf, size_t len) { if (host == NULL || page == NULL) { perror("Could not connect"); exit(1); } int sock = create_tcp_socket(); char ip[16] = {0}; get_ip(host, ip, sizeof(ip)-1); fprintf(stdout, "IP is %s\n", ip); struct sockaddr_in remote; remote.sin_family = AF_INET; int tmpres = inet_pton(AF_INET, ip, (void*)(&(remote.sin_addr.s_addr))); if (tmpres < 0) { perror("Can't set remote->sin_addr.s_addr"); exit(1); } else if (tmpres == 0) { fprintf(stderr, "%s is not a valid IP address\n", ip); exit(1); } remote.sin_port = htons(80); // http_port if (connect(sock, (const struct sockaddr*)&remote, sizeof(struct sockaddr)) < 0) { perror("Could not connect"); exit(1); } char get_query[BUFSIZ] = {0}; // should be enough build_get_query(host, page, get_query, sizeof(get_query)); fprintf(stderr, "Query <<START>>\n%sQuery <<END>>\n", get_query); int sent = 0; // send the query to the server while (sent < strlen(get_query)) { tmpres = send(sock, get_query+sent, strlen(get_query)-sent, 0); if (tmpres == -1){ perror("Can't send query"); exit(1); } sent += tmpres; } int htmlstart = 0; char *htmlcontent, ret[BUFSIZ] = {0}; while ((tmpres = recv(sock, ret, BUFSIZ, 0)) > 0) { if (htmlstart == 0) { /* Under certain conditions this will not work. * If the \r\n\r\n part is splitted into two messages * it will fail to detect the beginning of HTML content */ htmlcontent = strstr(ret, "\r\n\r\n"); if (htmlcontent != NULL){ htmlstart = 1; htmlcontent += 4; } } else { htmlcontent = ret; } if (htmlstart) { printf("one more buf\n"); fprintf(stdout, htmlcontent); // output strncpy(buf, htmlcontent, tmpres); } memset(ret, 0, tmpres); } if (tmpres < 0) { perror("Error receiving data"); } close(sock); }