程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> c說話獲得直播吧比來一周nba競賽信息

c說話獲得直播吧比來一周nba競賽信息

編輯:關於C++

c說話獲得直播吧比來一周nba競賽信息。本站提示廣大學習愛好者:(c說話獲得直播吧比來一周nba競賽信息)文章只能為提供參考,不一定能成為您想要的結果。以下是c說話獲得直播吧比來一周nba競賽信息正文


就是用c說話的寫的一個簡略http要求,並剖析其呼應,道理很簡略,重要是剖析http呼應,費事的是提取個中的競賽信息


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/mman.h>
#include <netdb.h>    //struct hostent構造的頭文件
#include <time.h>

#define USERAGENT "Wget/1.10.2"
#define ACCEPT "*/*"
#define ACCEPTLANGUAGE "zh-cn,zh;q=0.5"
#define ACCEPTENCODING "gzip,deflate"
#define ACCEPTCHARSET "gb2312,utf-8;q=0.7,*;q=0.7"
#define KEEPALIVE "300"
#define CONNECTION "keep-alive"
#define CONTENTTYPE "application/x-www-form-urlencoded"

#define HOSTNAME "www.zhibo8.cc"
#define DEBUG 1
char Request[409600]="";
char Recive[409600]="";
struct hostent *host;
int sockfd=0;
FILE *fp;
struct sockaddr_in server_addr;
int year;
int mon;
int day;
int wday;
int count=0;

void HandLine(char *l);
void SaveResult();
void GetDayInfo();
void GetTime();
int GetLocalAgent(char * UserAgent, char * Accept, char * AcceptLanguage, char * AcceptEncoding, char * AcceptCharset, char * KeepAlive, char * Connection, char * ContentType)
{
  memcpy(UserAgent, USERAGENT, strlen(USERAGENT));
  memcpy(Accept, ACCEPT, strlen(ACCEPT));
  memcpy(AcceptLanguage, ACCEPTLANGUAGE, strlen(ACCEPTLANGUAGE));
  memcpy(AcceptEncoding, ACCEPTENCODING, strlen(ACCEPTENCODING));
  memcpy(AcceptCharset, ACCEPTCHARSET, strlen(ACCEPTCHARSET));
  memcpy(KeepAlive, KEEPALIVE, strlen(KEEPALIVE));
  memcpy(Connection, CONNECTION, strlen(CONNECTION));
  memcpy(ContentType, CONTENTTYPE, strlen(CONTENTTYPE));
  return 0;
}


void CreatRequest()
{
    char UserAgent[1024] = "", Accept[1024] = "", AcceptLanguage[1024] = "", AcceptEncoding[1024] = "", AcceptCharset[1024] = "", KeepAlive[1024] = "", Connection[1024] = "", ContentType[1024] = "";
    if((host=gethostbyname("www.zhibo8.cc"))==NULL) /* get ip address by domain */
     {
           if(DEBUG)  fprintf(stderr,"\tGethostname '%s' error, %s\n", HOSTNAME, strerror(errno));
           exit(1);
     }
    GetLocalAgent(UserAgent, Accept, AcceptLanguage, AcceptEncoding, AcceptCharset, KeepAlive, Connection, ContentType); /* Get client browser information */
    sprintf(Request, "GET /%s/%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nConnection: %s\r\n\r\n", "/", "", HOSTNAME, UserAgent, Accept, Connection);
}


void ConnectWeb()
{
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
       fprintf(stderr,"\t SOCKET ERROR:%s\a\n",strerror(errno));
       exit(1);
    }
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port   = htons(80);   
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    if(connect(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr))==-1)
    {
       fprintf(stderr,"\t CONNECT ERROR:%s\a\n",strerror(errno));
       exit(1);
    }
}

void SendRequest()
{
    int wn=0;
    wn=write(sockfd,Request,strlen(Request));
    if(wn==-1)
    {
     fprintf(stderr,"\t SEND ERROR:%s\a\n",strerror(errno));
     exit(1);
    }
     fprintf(stdout, "\n\tRequest.  send OK!\n" );
}
void SaveResult()
{
    fd_set readfds;
   struct timeval tival;

  
   int i,j,ret;
   i=j=ret=0;
  int n=0;
   RECIVE:
   FD_ZERO(&readfds);
   tival.tv_sec=10;
   tival.tv_usec=0;
   if(sockfd>0)
     FD_SET(sockfd,&readfds);
   ret=select(sockfd+1,&readfds,NULL,NULL,&tival);
   if(ret==0)
   {
     if(n++<2)
      goto RECIVE;

   }
   if(FD_ISSET(sockfd,&readfds))
   {
     /*讀取全體響應內容到當地文件*/
     while(read(sockfd,Recive,sizeof(Recive)))
     {       
        fwrite(Recive,sizeof(Recive),1,fp);
        memset(Recive,0,sizeof(Recive));
     }

}
}


void EchoResult()
{
   fd_set readfds;
   struct timeval tival;

  
   int i,j,ret;
   i=j=ret=0;
  int n=0;
   RECIVE:
   FD_ZERO(&readfds);
   tival.tv_sec=2;
   tival.tv_usec=0;
   if(sockfd>0)
     FD_SET(sockfd,&readfds);
   ret=select(sockfd+1,&readfds,NULL,NULL,&tival);
   if(ret==0)
   {
     if(n++<2)     
      goto RECIVE;

   }
   if(FD_ISSET(sockfd,&readfds))
   {     
      char c; 
      while(read(sockfd,&c,1))
     {       
         if(c=='d')
         {
            read(sockfd,&c,1);
            if(c=='i')
     {              
  read(sockfd,&c,1);
         if(c=='v')
  {
     read(sockfd,&c,1);
            if(c==' ')
     {
        read(sockfd,&c,1);
               if(c=='c')
        {      
          read(sockfd,&c,1);
          if(c=='l')
          {
     read(sockfd,&c,1);
     if(c=='a')
     {
               read(sockfd,&c,1);
                      if(c=='s')
               {
    read(sockfd,&c,1);
           if(c=='s')
    {
              read(sockfd,&c,1);
                     if(c=='=')
              {
         read(sockfd,&c,1);
                if(c=='"')
         {
                                        read(sockfd,&c,1);
            if(c=='t')
     {
               read(sockfd,&c,1);
                      if(c=='i')
        {
           read(sockfd,&c,1);
                  if(c=='t')
           {                                                 
         read(sockfd,&c,1);
         if(c=='l')
         {
           read(sockfd,&c,1);
                  if(c=='e')
           {
        read(sockfd,&c,1);
               if(c=='b')
        {
                  read(sockfd,&c,1);
           if(c=='a')
           { 
                                                                read(sockfd,&c,1);  
        if(c=='r');
        {
           read(sockfd,&c,1);
                  if(c=='"')
                                                                     GetDayInfo();
               } 
           }
        } 
           }     
         }  
           }      
        }
     }
         }     
       }    
    } 
        }      
     }  
          }
        }     
     }   
  }  
            }
         }

     }
   }

}


void GetDayInfo()
{
  //  int f=open("./s.txt",O_WRONLY|O_CREAT);   
    char t;
    char Line[1024]="";
    int sday=wday+count;
    if(count++>=7)
    return;
    printf("\n***********%d年 %d月 %d日, 禮拜 %d  **********************\n\n",year,mon,day+count-1,sday>7?sday%7:sday);
    while(read(sockfd,&t,1)) /*處置天天的競賽信息即<ul></ul>裡的內容*/
    {

 if(t=='l')
 {
   read(sockfd,&t,1);
          if(t=='i');
          {
             int ln=0;
             memset(Line,0,sizeof(Line));    
      while(read(sockfd,Line+(ln++),1))/*處置每行的信息<li></li>裡的內容*/
      {

                char *s=strstr(Line,"</li>");
                if(s!=NULL)
                  break;
      }
      Line[ln]=0;                      
             HandLine(Line);
   }  
 }
       else if(t=='/')
 {
          read(sockfd,&t,1);
   if(t=='u')
   {
      read(sockfd,&t,1);
             if(t=='l');
              return;     
   }
        }
    }
}
void HandLine(char *l)
{
   l++;  
   char *NBA="NBA慣例賽";
   char *p=strstr(l,NBA);
   if(p==NULL)
 return;
   char *t=strstr(l,"<b");
   int lt=strlen(l)-strlen(t);
   char *time=(char*)malloc(sizeof(char)*lt);
   memcpy(time,l,lt);
   char *p1=strstr(p,"/b>");
   char *p2=strstr(p,"<a");
   int n=strlen(p1)-strlen(p2)-3;
   char *TeamVsTeam=(char*)malloc(n*sizeof(char));
   memcpy(TeamVsTeam,p1+3,n);
   char *p3=strstr(p2,"_blank\">");
   char *p4=strstr(p3,"</a>");
   int m=strlen(p3)-strlen(p4)-8;
   char *Live=(char*)malloc(sizeof(char)*m);
   memcpy(Live,p3+8,m);
   printf("time:%s The Teams:%s TV Live:%s\n",time,TeamVsTeam,Live);
}
void GetTime()
{
    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime);
    timeinfo=localtime(&rawtime);
    year=timeinfo->tm_year+1900;
    mon =timeinfo->tm_mon+1;
    day =timeinfo->tm_mday;
    wday=timeinfo->tm_wday;
   if(wday==0)
     wday=7;
}

int main()
{
    fp=fopen("Info.txt","w");
    if(fp==NULL)
    {
      fprintf(stderr,"creat file is error \n");
      exit(1);
    }
    CreatRequest();
    ConnectWeb();
    SendRequest();   
    EchoResult();
    printf("IS END !!\n");
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved