c/c++利用socket判断web服务器是否能访问

最近用到相关方面知识,所以写了这个。可以测网站时延和带宽


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  

#define BUFSIZE 0xF000
void geturl(char* url,char* port)
{
	int cfd;
	struct sockaddr_in cadd;
	struct hostent *pURL = NULL;
	char myurl[BUFSIZE];
	char *pHost = 0;
	char host[BUFSIZE],GET[BUFSIZE];
	char request[BUFSIZE];
	static char text[BUFSIZE];
	int i,j;

	struct timeval start,start_tz;
	struct timeval middle,middle_tz;
	struct timeval finish,finish_tz;
	
	float timegap = 0;
	float timegap_middle = 0;
	float bandwidth = 0; 

    	//分离主机中的主机地址和相对路径
    	memset(myurl,0,BUFSIZE);
    	memset(host,0,BUFSIZE);
    	memset(GET,0,BUFSIZE);
    	strcpy(myurl,url);
    	for(pHost = myurl;*pHost != '/' && *pHost != '\0';++pHost);
 

     	//获取相对路径保存到GET中
     	if((int)(pHost-myurl) == strlen(myurl))
     	{
		strcpy(GET,"/");//即url中没有给出相对路径,需要自己手动的在url尾部加上/

     	}
     	else
     	{
        	strcpy(GET,pHost);//地址段pHost到strlen(myurl)保存的是相对路径
     	}
 
        //将主机信息保存到host中
        //此处将它置零,即它所指向的内容里面已经分离出了相对路径,剩下的为host信
 //息(从myurl到pHost地址段存放的是HOST)
        *pHost = '\0';
 	
        strcpy(host,myurl);
        //设置socket参数
        if(-1 == (cfd = socket(AF_INET,SOCK_STREAM,0)))
        {
                printf("create socket failed of client!\n");
                exit(-1);
        }
 
        pURL = gethostbyname(host);//将上面获得的主机信息通过域名解析函数获得域名信息
 
        //设置IP地址结构
        bzero(&cadd,sizeof(struct sockaddr_in));
        cadd.sin_family = AF_INET;
        cadd.sin_addr.s_addr = *((unsigned long*)pURL->h_addr_list[0]);  //
        cadd.sin_port = htons(atoi(port));


        //向WEB服务器发送URL信息
        memset(request,0,BUFSIZE);
        strcat(request,"GET ");
        strcat(request,GET);
        strcat(request," HTTP/1.1\r\n");//至此为http请求行的信息
        strcat(request,"HOST: ");
        strcat(request,host);
        strcat(request,"\r\n");
        strcat(request,"Cache-Control: no-cache\r\n\r\n");
        //连接服务器
 
//	printf("%s \n",request);
	int cc;
	gettimeofday(&start,&start_tz);
        if(-1 == (cc = connect(cfd,(struct sockaddr*)&cadd,(socklen_t)sizeof(cadd))))
        {
                fprintf(stderr,"invaild ! \ndomain name:%s \nport:%s \n",url,port);
                exit(1);
        }
   //     printf("connect success!\n");
 
	gettimeofday(&middle,&middle_tz);
        //向服务器发送url请求的request
        int cs;
        if(-1 == (cs = send(cfd,request,strlen(request),0)))
        {
                printf("向服务器发送请求的request失败!\n");
                exit(1);
        }
        //printf("发送成功,发送的字节数:%d\n",cs);
 
        //客户端接收服务器的返回信息
        memset(text,0,BUFSIZE);
        int recv_bytes;
        if(-1 == (recv_bytes = recv(cfd,text,BUFSIZE,0)))
        {
                fprintf(stderr,"invaild ! \ndomain name:%s \nport:%s \n",url,port);
                exit(1);
        }
        else
        {
		printf("%s\n",text);
		printf("recv %d byte \n",recv_bytes);
		gettimeofday(&finish,&finish_tz);
		printf("start.tv_sec %d start.tv_usec %d \n",start.tv_sec,start.tv_usec);
		printf("middle.tv_sec %d middle.tv_usec %d \n",middle.tv_sec,middle.tv_usec);
		printf("finish.tv_sec %d finish.tv_usec %d \n",finish.tv_sec,finish.tv_usec);
                int sec = finish.tv_sec - start.tv_sec;
		int usec = finish.tv_usec - start.tv_usec ;
		int sec_middle = finish.tv_sec - middle.tv_sec;
		int usec_middle = finish.tv_usec - middle.tv_usec ;
		//printf(" sec %d usec %d \n",sec,usec);
		timegap = sec*1000 + (float)usec/1000;
		timegap_middle = sec_middle*1000 + (float)usec_middle/1000;

		float total_kb = (float)recv_bytes/1024;
		float timegap_sec = (float)timegap/1000;
		float timegap_sec_middle = (float)timegap_middle/1000;
		//printf("total_kb :%f kb ! \n",total_kb);
		//printf("timegap_sec:%f s ! \n",timegap_sec);
		bandwidth = total_kb/timegap_sec;
                printf("normal ! \ndomain name:%s \nport:%s \n",url,port);
		printf("Time delay:%f ms ! \n",timegap);
		printf("Time delay(middle):%f ms ! \n",timegap_middle);
		printf("Read bandwidth :%f Kb/s ! \n",bandwidth);
        }
        close(cfd);
 }
 
int main(int argc,char* argv[])
{
        if(argc != 3)
        {
                printf("useage:%s url port\n",argv[0]);
                exit(1);
        }
        geturl(argv[1],argv[2]);
        return 0;
}


你可能感兴趣的:(Linux,C/C++,c++,socket,linux)