Method 1:
# 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" // server IP Address
# 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 );
}
This article is from the Internet. It can be used for a single domain name. For many domain names, this method is not good and can be used to implement functions, but it is cumbersome and requires a program to control the URI. Because urican be in the format of .html,. php, image, and video. The red index. php is the URI of the program.
Method 2: self-built
Idea: Since shell cmd has curl, why not use this method? Then, find the C and shell interfaces, and finally decide to use the system function, determine whether the website can be opened based on the return value of the system function (0 -- yes, others cannot). If the program is large, the curl result is not beautiful on the screen, so the result is redirected to/dev/null. The Code is as follows:
# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>
Int main (INT argc, char ** argv)
{
Int I = 2;
Char variable [] = "www.google.cn
"; // You can easily determine different domain names through control.
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;
}