linux C程式如何確定一個網域名稱所對應的網站是否可以開啟!

來源:互聯網
上載者:User

第一種方法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define BUFSIZE 1024
#define DestIp "xx.xx.xx.xx"  //伺服器IP
#define DestPort 80
#define Req "HEAD /index.php
HTTP/1.1/r/nHost: 1xlzx.com/r/nConnection: Close/r/n/r/n" 
#define ReqLen sizeof(Req)

int main(int argc, char *argv[]){
                ssize_t i;
                int nRequestLen;

                char strResponse[BUFSIZE]={0};
                char strRequest[BUFSIZE]={0};

                int sockfd, numbytes;
                struct sockaddr_in dest_addr; /* connector's address information */

                if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                                perror("socket");
                                exit(1);
                }

                dest_addr.sin_family = AF_INET; /* host byte order */
                dest_addr.sin_port = htons(DestPort); /* short, network byte order */
                dest_addr.sin_addr.s_addr = inet_addr(DestIp);

                /* Create and setup the connection */
                if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(struct sockaddr)) == -1) {
                                perror("connect");
                                exit(1);
               /* Send the request */
                strncpy(strRequest, Req,ReqLen);
                nRequestLen = ReqLen;
                if (write(sockfd,strRequest,nRequestLen) == -1) {
                        perror("write");
                        exit(1);
                }

                /* Read in the response */
                while(1) {
                                i = read(sockfd,strResponse,BUFSIZE-1);
                                if(0 >= i){
                                                break;
                                }
                                strResponse[i]='/0';
                                printf(strResponse);

                }

                /* Close the connection */
                close(sockfd);
}

這篇是來源於網上,對單個網域名稱是可以的,對於很多網域名稱,該種方法是不好的,可以實現功能,但是繁瑣,需要程式控制URI即可。因為URI可以是.html、.php、圖片、視屏等格式的。紅色部分index.php就是本程式的URI。

 

 

第二種方法:自己原創

思路:既然shell cmd有curl,何不採用這種方法呢,然後尋找c、shell的介面,最終確定採用system函數,根據system函數的傳回值來確定該網站是否可以開啟(0——可以開啟,其他的不可以開啟),如果程式龐大的話,將curl的結果顯示在螢幕上很不美觀,故將結果重新導向到/dev/null。代碼如下:

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>

int main(int argc, char **argv)
{
   int i = 2;
   char VARIABLE[] = "www.google.cn
";  //很容易的通過控制,實現不同的網域名稱的判斷
   i = system("curl -I www.google.cn
> /dev/null 2>&1");
   if(i == 0)

   {

       //do somegthing ...

   }

   else

   {

       //do something

   }

   printf("%d/n", i);
   return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.