c#测试服务器和客户端应用程序之间的带宽
我是编程新手。对于我的第一个应用程序,我决定创建两个控制台应用程序—服务器应用程序和客户端应用程序。我想测试客户端和服务器应用程序之间的数据事务带(通过使用套接字的方式)。我的问题是,我需要测试连接速度,比方说10秒,然后在这10秒之后,我需要获得接收到的数据量(我在这个时间段内接收到的数据量)并计算速度……我该怎么做 客户端应用程序c#测试服务器和客户端应用程序之间的带宽,c#,sockets,tcp,timeout,bandwidth,C#,Sockets,Tcp,Timeout,Bandwidth,我是编程新手。对于我的第一个应用程序,我决定创建两个控制台应用程序—服务器应用程序和客户端应用程序。我想测试客户端和服务器应用程序之间的数据事务带(通过使用套接字的方式)。我的问题是,我需要测试连接速度,比方说10秒,然后在这10秒之后,我需要获得接收到的数据量(我在这个时间段内接收到的数据量)并计算速度……我该怎么做 客户端应用程序 namespace Example_01_Sockets_Client { class MainClass { public static void Ma
namespace Example_01_Sockets_Client
{
class MainClass
{
public static void Main(string[] args)
{
try
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ownAddress = IPAddress.Parse("127.0.0.1");
var ownEndpoint = new IPEndPoint(ownAddress, 4321);
socket.Bind(ownEndpoint);
Console.WriteLine();
Console.WriteLine("Trying to connect to server...");
var serverAddress = IPAddress.Parse("127.0.0.1");
socket.Connect(serverAddress, 2222);
Console.WriteLine("Connected to server");
var buffer = new byte[1024 * 150000];
socket.ReceiveTimeout = 100;
int receivedBytesLen = socket.Receive(buffer);
Console.WriteLine("Download speed: " + ((receivedBytesLen) / 100 + "kb/s ") );
Console.WriteLine("Press any key to exit");
Console.ReadKey();
socket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
class MainClass
{
public static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
var listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndpoint = new IPEndPoint(ipAddress, 2222);
listeningSocket.Bind(localEndpoint);
listeningSocket.Listen(1);
Console.WriteLine();
Console.WriteLine("Waiting for client...");
Socket connectedSocket = listeningSocket.Accept();
listeningSocket.Close();
string clientAddress = connectedSocket.RemoteEndPoint.ToString();
Console.WriteLine("Client connected (" + clientAddress + ")");
string fileName = "downTest.txt";
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
connectedSocket.Send(fileData);
//int bytesReceived = connectedSocket.Receive();
Console.WriteLine("Client connected (" + clientAddress + ")");
connectedSocket.Close();
}
}
}
服务器应用程序
namespace Example_01_Sockets_Client
{
class MainClass
{
public static void Main(string[] args)
{
try
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var ownAddress = IPAddress.Parse("127.0.0.1");
var ownEndpoint = new IPEndPoint(ownAddress, 4321);
socket.Bind(ownEndpoint);
Console.WriteLine();
Console.WriteLine("Trying to connect to server...");
var serverAddress = IPAddress.Parse("127.0.0.1");
socket.Connect(serverAddress, 2222);
Console.WriteLine("Connected to server");
var buffer = new byte[1024 * 150000];
socket.ReceiveTimeout = 100;
int receivedBytesLen = socket.Receive(buffer);
Console.WriteLine("Download speed: " + ((receivedBytesLen) / 100 + "kb/s ") );
Console.WriteLine("Press any key to exit");
Console.ReadKey();
socket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
class MainClass
{
public static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
var listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndpoint = new IPEndPoint(ipAddress, 2222);
listeningSocket.Bind(localEndpoint);
listeningSocket.Listen(1);
Console.WriteLine();
Console.WriteLine("Waiting for client...");
Socket connectedSocket = listeningSocket.Accept();
listeningSocket.Close();
string clientAddress = connectedSocket.RemoteEndPoint.ToString();
Console.WriteLine("Client connected (" + clientAddress + ")");
string fileName = "downTest.txt";
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
byte[] fileData = File.ReadAllBytes(fileName);
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
connectedSocket.Send(fileData);
//int bytesReceived = connectedSocket.Receive();
Console.WriteLine("Client connected (" + clientAddress + ")");
connectedSocket.Close();
}
}
}
谢谢你的帮助 为什么不使用一定数量的数据(例如4096KB)。在客户端开始下载数据之前,使用Stopwatch类启动计时器,然后在完成发送后立即停止计时器。然后,您可以使用“已用时间”属性来确定发送固定数量的数据所需的时间
Stopwatch timer = new Stopwatch();
timer.Start();
// Download data here
timer.Stop();
int elapsedTime = timer.Elapsed.TotalSeconds;
是的,我在想,这是一个最简单的解决方案。但我想知道有没有可能在设定的时间段内测试连接?例如,我有4kb的大文件,如果我有400mbit/s的下载速度,下载速度会很快。但是,例如,如果我有500mb的大文件和1mbit/s的快速连接->运行测试需要花费很长时间),请执行一个小样本(几兆字节)并获得大致的速度。然后在此基础上调整实际测试的大小。在任何情况下,通过导线发送GB数据以测试快速连接可能都不谨慎,因此另一种解决方案是提供一个良好的“中间地带”大小。使用套接字超时不是一个好的解决方案,因为当超时发生时,这将导致异常,并且您不知道收到了多少数据。最后,所有这些缓冲区都意味着将数据加载到内存中。如果您尝试发送/接收太多,您将测试磁盘I/O。