c#测试服务器和客户端应用程序之间的带宽



我是编程新手。对于我的第一个应用程序,我决定创建两个控制台应用程序-服务器和客户端应用程序。我想测试客户端和服务器应用程序之间的数据交易带宽(顺便说一下,我使用套接字)。我遇到的问题是,我需要测试连接速度,让我们说10秒,在这10秒之后,我需要获得接收的数据量(我在这段时间内收到的)并计算速度……我该怎么做呢?

客户端应用

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();
        }
    }
}

谢谢你的帮助!

为什么不使用一定数量的数据(例如:4096 kb)。使用Stopwatch类在开始在客户端下载数据之前启动计时器,然后在完成发送后立即停止计时器。然后,您可以使用elapsed time属性来确定发送固定数量的数据所花费的时间。

            Stopwatch timer = new Stopwatch();
            timer.Start();
            // Download data here
            timer.Stop();
            int elapsedTime = timer.Elapsed.TotalSeconds;

相关内容

  • 没有找到相关文章