Главная » 2009 » Ноябрь » 6 » Формулы передачи данных для начинающих
08:55
Формулы передачи данных для начинающих

Данным примером я попытаюсь дать ответы на следующие вопросы:
Каково различие между KBps и Kbps? В чём заключается отличие битов, байтов и бодов? Как определить скорость передачи данных? Как выяснить, насколько долго будет загружаться файл с определённой скоростью? Как посчитать время, оставшее до окончания загрузки?

Для начала хотельсы навести порядок с некоторой неразберихой по поводу KBps и Kbps (буква b в нижнем регистре). KBps это обозначение для килобайт в секунду, в то время как Kbps обозначает килобиты в секунду. 1 килобайт (KB) = 8 килобитам (Kb).

Когда речь идёт о скорости передачи, то применяется Kbps. Таким образом модем со скорость передачи 33.6K (33600 bps) передаёт данные со скоростью 4.2 KBps (4.2 килобайта в секунду). Как мы видим, разница между KB и Kb довольно ощутима. В этом кроется причина того, что некоторые пользователи модемов по своему незнанию не могут понять, почему данные передаются так медленно. На самом деле данные объёмом 33.6K передаются не за 1 секунду, а за 8, соответственно за одну секунду будет передано 33.6 Kb / 8 = 4.2.

Так же хотелось бы дать некоторые разъяснения по поводу слова "бод" (baud). Обычно для модема "боды" расшифровываются как бит в секунду. На самом деле это не так. Бод (Baud) означает частоту звука в телефонной линии. Т. е. в зависимости от модема, который Вы используете, количество бит, которые могут быть переданы зависит от частоты звука, необходимой для обеспечения нужной скорости передачи.

Обратите внимание: Приведённый ниже пример, использует компонент NetMasters TNMHTTP. Однако, если Вы "прикипели" к какому-то другому компоненту TCP/IP, то переделать пример под этот компонент не составит большого труда.

Используемые обозначения:
bps = байт, переданных за 1 секунду
KBps (KB/Sec) = bps / 1024
Kbps (Kb/Sec) = KBps x 8

Краткий алгоритм приведённого ниже примера:
1. Сохраняем в переменной время начала загрузки: nStartTime := GetTickCount;
2. Сохраняем в переменной размер файла (KB): nFileSize := "File Size";
3. Начало передачи данных.
4. Обновляем количество переданных байт: Inc(nBytesTransferred, nNewBytes);
5. Получаем оставшееся время: nTimeElapsed := (GetTickCount - nStartTime) / 1000;
6. Вычисляем bps: nBps := BytesTransferred / nTimeElapsed;
7. Вычисляем KBps: nKBps := nBps / 1024;

Используемые данные:
Общее время скачивания (секунд) := nFileSize / nKBps;
bps := FloatToStr(nBps);
KB/Sec (KBps) := FloatToStr(nKBps);
Осталось секунд := FloatToStr(((nFileSize - BytesTransferred) / 1024) / KBps);

Рабочий пример:
view source

print
?

unit Main;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, Gauges, Psock, NMHttp;

type

TfMain = class(TForm)

Label1: TLabel;

eURL: TEdit;

bGet: TButton;

lbMessages: TListBox;

gbDetails: TGroupBox;

lEstimate: TLabel;

lKBps: TLabel;

lReceived: TLabel;

lRemaining: TLabel;

gProgress: TGauge;

NMHTTP1: TNMHTTP;

lbps: TLabel;

bCancel: TButton;

procedure NMHTTP1PacketRecvd(Sender: TObject);

procedure bGetClick(Sender: TObject);

procedure bCancelClick(Sender: TObject);

procedure NMHTTP1Connect(Sender: TObject);

procedure NMHTTP1ConnectionFailed(Sender: TObject);

procedure NMHTTP1Disconnect(Sender: TObject);

procedure NMHTTP1Failure(Cmd: CmdType);

procedure NMHTTP1HostResolved(Sender: TComponent);

procedure NMHTTP1InvalidHost(var Handled: Boolean);

procedure NMHTTP1Status(Sender: TComponent; Status: String);

procedure NMHTTP1Success(Cmd: CmdType);

private

{ Private declarations }

function ss2nn(Seconds: Integer): String;

public

{ Public declarations }

end;

var

fMain: TfMain;

nFileSize: Double;

nStartTime: DWord;



implementation


{$R *.DFM}


{Цель этой функции состоит в том, чтобы определить, сколько минут и секунд там находятся в данном количестве секунд}

function TfMain.ss2nn(Seconds: Integer): String;

var

nMin, nSec: Integer;

begin

{Проверяем, меньше чем 1/Min}

if Seconds < 60 then Result := '0 minutes ' + IntToStr(Seconds) + ' seconds'

else begin

{Определяем минуты}

nMin := Seconds div 60;

{Определяем секунды}

nSec := Seconds - (nMin * 60);

{Возвращаем результат}

Result := IntToStr(nMin) + ' minutes ' + IntToStr(nSec) + ' seconds';

end;

end;

procedure TfMain.NMHTTP1PacketRecvd(Sender: TObject);

var

nBytesReceived, nTimeElapsed, nBps, nKBps: Double;

begin

{Следующий код выполняется только однажды, при приёме первого пакета}

if nFileSize <> NMHTTP1.BytesTotal then

begin

{Получаем размер файла}

nFileSize := NMHTTP1.BytesTotal;

{Вычисляем время передачи, исходя из скорости соединения 33.6 Kbps}

lEstimate.Caption := 'Estimated download time at 33.6 Kbps: ' + ss2nn(Round(

(nFileSize / 1024) / 4.2));

{Получаем время начала}

nStartTime := GetTickCount;

end;

{Обновляем nBytesReceived}

nBytesReceived := NMHTTP1.BytesRecvd;

{Вычисляем количество секунд прошедших с момента начала передачи}

nTimeElapsed := (GetTickCount - nStartTime) / 1000;

{Проверяем на 0/Sec, если так, то устанавливаем 1, чтобы предотвратить деления на ноль}

if nTimeElapsed = 0 then nTimeElapsed := 1;

{Вычисляем байт в секунду}

nBps := nBytesReceived / nTimeElapsed;

{Вычисляем килобайт в секунду}

nKBps := nBps / 1024;

{Обновляем контролы}

gProgress.Progress := Round((nBytesReceived * 100) / nFileSize);

lbps.Caption := IntToStr(Round(nBps * 8)) + ' bits per second';

lKBps.Caption := IntToStr(Round(nKBps)) + ' KB/Sec (KBps)';

lReceived.Caption := FloatToStr(nBytesReceived) + ' of ' + FloatToStr(

nFileSize) + ' bytes received';

lRemaining.Caption := ss2nn(Round(((nFileSize - nBytesReceived) / 1024) /

nKBps)) + ' remaining';

end;

procedure TfMain.bGetClick(Sender: TObject);

begin

{Сбрасываем переменные}

nFileSize := 0;

{Обнуляем контролы}

lbMessages.Clear;

gProgress.Progress := 0;

lEstimate.Caption := 'Estimated download time at 33.6 Kbps: 0 minutes 0 ' +

'seconds';

lbps.Caption := '0 bits per second';

lKBps.Caption := '0 KB/Sec (KBps)';

lReceived.Caption := '0 of 0 bytes received';

lRemaining.Caption := '0 minutes 0 seconds remaining';

{Получаем файл}

NMHTTP1.Get(eURL.Text);

end;

procedure TfMain.bCancelClick(Sender: TObject);

begin

{Разрываем соединение с сервером}

NMHTTP1.Disconnect;

{Обновляем lbMessages}

lbMessages.Items.Append('Get Canceled');

lbMessages.Items.Append('Disconnected');

end;

procedure TfMain.NMHTTP1Connect(Sender: TObject);

begin

{Запрещаем/Разрешаем контролы}

bGet.Enabled := False;

bCancel.Enabled := True;

{Работаем с lbMessages}

with lbMessages.Items do

begin

Append('Connected');

Append('Local Address: ' + NMHTTP1.LocalIP);

Append('Remote Address: ' + NMHTTP1.RemoteIP);

end;

end;

procedure TfMain.NMHTTP1ConnectionFailed(Sender: TObject);

begin

ShowMessage('Connection Failed.');

end;

procedure TfMain.NMHTTP1Disconnect(Sender: TObject);

begin

{Запрещаем/Разрешаем контролы}

bCancel.Enabled := False;

bGet.Enabled := True;

{Обновляем lbMessages}

if NMHTTP1.Connected then lbMessages.Items.Append('Disconnected');

end;

procedure TfMain.NMHTTP1Failure(Cmd: CmdType);

begin

case Cmd of

CmdGET : lbMessages.Items.Append('Get Failed');

CmdOPTIONS: lbMessages.Items.Append('Options Failed');

CmdHEAD : lbMessages.Items.Append('Head Failed');

CmdPOST : lbMessages.Items.Append('Post Failed');

CmdPUT : lbMessages.Items.Append('Put Failed');

CmdPATCH : lbMessages.Items.Append('Patch Failed');

CmdCOPY : lbMessages.Items.Append('Copy Failed');

CmdMOVE : lbMessages.Items.Append('Move Failed');

CmdDELETE : lbMessages.Items.Append('Delete Failed');

CmdLINK : lbMessages.Items.Append('Link Failed');

CmdUNLINK : lbMessages.Items.Append('UnLink Failed');

CmdTRACE : lbMessages.Items.Append('Trace Failed');

CmdWRAPPED: lbMessages.Items.Append('Wrapped Failed');

end;

end;

procedure TfMain.NMHTTP1HostResolved(Sender: TComponent);

begin

lbMessages.Items.Append('Host Resolved');

end;

procedure TfMain.NMHTTP1InvalidHost(var Handled: Boolean);

begin

ShowMessage('Invalid Host. Please specify a new URL.');

end;

procedure TfMain.NMHTTP1Status(Sender: TComponent; Status: String);

begin

if NMHTTP1.ReplyNumber = 404 then ShowMessage('Object Not Found.');

end;

procedure TfMain.NMHTTP1Success(Cmd: CmdType);

begin

case Cmd of

{Удостоверяемся, что процедура получения не была прервана}

CmdGET: if NMHTTP1.Connected then lbMessages.Items.Append('Get Succeeded');

CmdOPTIONS: lbMessages.Items.Append('Options Succeeded');

CmdHEAD : lbMessages.Items.Append('Head Succeeded');

CmdPOST : lbMessages.Items.Append('Post Succeeded');

CmdPUT : lbMessages.Items.Append('Put Succeeded');

CmdPATCH : lbMessages.Items.Append('Patch Succeeded');

CmdCOPY : lbMessages.Items.Append('Copy Succeeded');

CmdMOVE : lbMessages.Items.Append('Move Succeeded');

CmdDELETE : lbMessages.Items.Append('Delete Succeeded');

CmdLINK : lbMessages.Items.Append('Link Succeeded');

CmdUNLINK : lbMessages.Items.Append('UnLink Succeeded');

CmdTRACE : lbMessages.Items.Append('Trace Succeeded');

CmdWRAPPED: lbMessages.Items.Append('Wrapped Succeeded');

end;

end;

end.


Автор: Jason Pierce

Категория: Программирование | Просмотров: 1516 | Добавил: admin | Рейтинг: 5.0/1
Всего комментариев: 3
3 Unloadypoeday  
After getting more than 10000 visitors/day to my website I thought your jober.do.am website also need unstoppable flow of traffic...

Use this BRAND NEW software and get all the traffic for your website you will ever need ...

= = > > http://auto-massive-traffic.net

In testing phase it generated 867,981 visitors and $540,340.

Then another $86,299.13 in 90 days to be exact. That's $958.88 a
day!!

And all it took was 10 minutes to set up and run.

But how does it work??

You just configure the system, click the mouse button a few
times, activate the software, copy and paste a few links and
you're done!!

Click the link BELOW as you're about to witness a software that
could be a MAJOR turning point to your success.

= = > > http://auto-massive-traffic.net

2 rupinjuff  
Make $1,000's Weekly with a Health Internet Business of Your Very Own

Now get a complete fully-operational "Health eBiz" in a box!

This amazing site:

* Closes sales automatically for you!

* Has a complete electronic sales manager that makes all upsells for you!

* Collects subscribers and leads automatically!

* Contains a complete "health e-Mall!"

* Contains up to 90 additional income streams!

* Contains several powerful videos!

Has a "live" spokesmodel that walks out onto your visitors' screens and closes up to 396% MORE sales for you!

Includes complete professional set-up by Expert Web Development & Programming Team!

This NEW "Health Biz In a Box" complete and fully-operational website allows you to make all the cash you want from a fully-operational automatic cash-generating web business!

Read how it works here:

=> http://www.home-businessreviews.com/Turnkey-Affiliate-Websites.html

But rumor has it there may be a ceiling on the number of these Internet "health-biz" sites being given out in order to avoid everyone having one and risking market saturation.

Join the ranks of these people above and read how it works by going to:

=> http://www.home-businessreviews.com/Turnkey-Affiliate-Websites.html

1 sturrencunc  
Earn up to $3500/month just by taking simple surveys online!

I tried one of those online survey sites about 4 months ago that
say all you have to do is spend a couple of minutes filling out
some surveys and you will be making hundreds a day..... YEAH RIGHT, I didn't make anything.

I joined 7 of these stupid websites and I actually tried filling
out a couple of surveys on each site and they took forever then at
the end they wanted me to buy things or I couldn't complete the
survey.

These are complete scams so be aware!!! I couldn't believe they
were even allowed to sell such bogus products.

Then about 2 weeks ago I was watching the news and they had some
vice presidents and marketing managers of some major multi million
dollar companies talking about a site that they personally work
with to pay consumers for their opinion, A site that actually just
wants the "average Joe's" honest opinion on products and services
and is willing to pay big money to get them. for more information
clik here: http://take-survey-and-get-money.info

Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]