简易的网速测试

本文介绍了一个基于C++的网络客户端程序,该程序使用CURL库进行网络请求,并实现了一个简单的测速功能。通过多次下载操作,程序能够统计并显示平均速度、最大速度和最小速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本原理是访问稳定的网页来测试速度,或者利用稳定的下载地址来测速,比如QQ.EXE

网页粒度小 测试没有 下载地址 稳定准确

界面MFC完成,网络API使用CURL

201933_G7gc_1391394.png

HttpClient.cpp

#include "stdafx.h"

#include"HttpClient.h"
#include "HttpRequest.h"
#include "HttpRespone.h"
#include "curl.h"
#include <stdio.h>
#include <iostream>

using namespace std;

static int write_data(void *buffer, size_t size, size_t buffer_size, void *_respone)
{

	HttpRespone *respone = (HttpRespone*)_respone;


	HttpRequest*request = respone->getHttpRequest();

	respone->writeData(buffer, buffer_size);

	//request->release();

		const HttpCallBack & x = request->getHttpCallback();

		x(request, respone);
	return buffer_size;
}


HttpClient* HttpClient::getInstance()
{
	static HttpClient*ins = nullptr;
	if (ins == nullptr)
	{
		ins = new HttpClient;
	}
	return ins;

}

void HttpClient::doRequest_curl(HttpRequest*request)
{

	HttpRespone *respone = HttpRespone::create();
	CURL*easy_handle = curl_easy_init();
	curl_easy_setopt(easy_handle, CURLOPT_URL, request->getUrl().c_str());
	curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
	curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, respone);

	respone->setHttpRequest(request);
	request->setCURLhandle(easy_handle);

	// perform
	auto succ = curl_easy_perform(easy_handle);


}


void HttpClient::workFunc()
{
	while (true)
	{
		HttpRequest* request;

		_mutex.lock();

		if (_queue_request.size() <= 0)
		{
			_condition.wait(_mutex);
		}
		request = _queue_request.front();
		_queue_request.pop();
		_mutex.unlock();

		//	log("net thread  %d", _queue_request.size());

		this->doRequest_curl(request);


	}

}

void  HttpClient::setTimeoutForConnect(int t)
{
	this->_time_connect = t;
}

void  HttpClient::setTimeoutForRead(int t)
{
	this->_time_read = t;
}


HttpClient::HttpClient()
{
	this->_time_connect = 5;
	this->_time_read = 10;
	curl_global_init(CURL_GLOBAL_WIN32);

	/*auto t = std::thread([=]
	{
		this->workFunc();
	});
	t.detach();
	*/
}



void HttpClient::send(HttpRequest*request)
{
//	request->retain();
/*	_mutex.lock();
	_queue_request.push(request);
	_mutex.unlock();

	_condition.notify_one();
	*/
	this->doRequest_curl(request);
}

HttpClient::~HttpClient()
{
	_condition.notify_all();
}

 

按钮响应事件



	int total = 0;
	int max = 0;
	int min = 0xffffff;

	char s[500];

	for (int i = 0; i < 10; i++)
	{
		int sp = download();

		total += sp;
		sprintf(s, "%d KB/s", sp);

		_txt_current.SetWindowTextW(toUnicode(s));
		_size_total = 0;

		sprintf(s, "%d KB/s", total / (i + 1));

		_txt_total.SetWindowTextW(toUnicode(s));

		if (sp > max)
		{
			max = sp;
			sprintf(s, "%d KB/s", max);
			_txt_max.SetWindowTextW(toUnicode(s));
		}

		if (sp < min)
		{
			min = sp;
			sprintf(s, "%d KB/s", min);
			_txt_min.SetWindowTextW(toUnicode(s));
		}
	}

	sprintf(s, "0 KB/s");
	_txt_current.SetWindowTextW(toUnicode(s));
	MessageBox(_T("测速完成"), _T("完成"), MB_OK);

 

download函数



static int download()
{
	CURL *curl;
	CURLcode res;
	struct FtpFile ftpfile = {
		"1.exe", 
		NULL
	};

	curl = curl_easy_init(); 

	int t = clock();

	if (curl) { 

		//http://www.winrar.com.cn/download/wrar531scp.exe

		curl_easy_setopt(curl, CURLOPT_URL, "http://www.qq.com");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);,
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

		//	curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN");

		t1 = clock();
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}

	if (ftpfile.stream)
	{
		fclose(ftpfile.stream);
	}

	//	log("1.txt", "     %d S   %d KB/S    %d KB", (clock() - t), _size_total/(clock() - t), _size_total / 1024);

	return _size_total / (clock() - t);

}

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/739006

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值