Cómo programar en C# para detectar el tráfico de un puerto
Simplemente agregue un criterio a la oración currentSpeed?=?reeciveSpan?*?1000?/?span.Millisegundos?/?1024;
Cambiar currentSpeed?=?reeciveSpan *? 1000?/?span.Millisegundos?/?1024;Cambiado a
if?(span.Millisegundos?!=?0)
{
velocidad actual? =?reeciveSpan?*?1000?/?span.Millisegundos?/?1024;
}
else
{
currentSpeed ?=?0;
}?
Simplemente escríbalo de esta manera. Si usa una barra de progreso, establezca los valores máximo y mínimo en 100 y 0, ¿Valor?=. ?e.Porcentaje de progreso?Solo
privado?void?button1_Click(object?sender,?EventArgs?e)
{
webClint?=?new?WebClient () ;
start?=?DateTime.Now;
lastTime?=?DateTime.Now;
webClint.DownloadProgressChanged?+=?new?DownloadProgressChangedEventHandler (webClint_DownloadProgressChanged );
webClint.DownloadFileAsync(new?Uri(".sg/music/firefly.mp3"),?"e:\\Fire?Fly.mp3");
}
WebClient?webClint;
float?maxSpeed?=?0;
float?currentSpeed?=?0;
flotante ?lastSpeed?=?0;
largo?lastReceive?=?0;
largo?reeciveSpan;
TimeSpan?span;
FechaHora?inicio;
FechaHora?última Hora;
TimeSpan?useHora;
void?webClint_DownloadProgressChanged(objeto?remitente,?DownloadProgressChangedEventArgs?e)
{
span?=?DateTime.Now?-?lastTime;
lastTime?=?DateTime.Now;
useTime? =? lastTime?-?start;
reeciveSpan?=?e.BytesReceived?-?lastReceive;
currentSpeed?=?reeciveSpan?*?1000?/?span.Millisegundos? /? 1024;
maxSpeed?=?currentSpeed?>?maxSpeedcurrentSpeed?:?maxSpeed;
últimaVelocidad?=?currentSpeed;
últimaRecepción?=?e .Byte
sReceived;
label1.Text?=?"Descargado:?"?+?e.BytesReceived?/?1024?+?"KB\n"?+
"Tamaño total :?"?+?e.TotalBytesToReceive?/?1024?+?"KB\n"?+
"Porcentaje:?"?+?e.ProgressPercentage?+?"%\n" ? +
"Velocidad actual:?"?+?currentSpeed.ToString("F2")?+?"KB/s\n"?+
"Velocidad máxima:? " ?+?maxSpeed.ToString("F2")?+?"KB/s\n"?+
"Tiempo:?"?+?(useTime.Minutes?*?60?+? useTime .Segundos)?+?"s";
si?(e.BytesReceived?==?e.TotalBytesToReceive)
{
webClint.DownloadProgressChanged ? -=?webClint_DownloadProgressChanged;
webClint.Dispose();
MessageBox.Show("¡Tarea completada!");
}
}