【问题标题】:c# test bandwidth between server and client applicationsc# 测试服务器和客户端应用程序之间的带宽
【发布时间】:2016-01-02 14:18:37
【问题描述】:

我是编程新手。对于我的第一个应用程序,我决定创建两个控制台应用程序 - 服务器和客户端应用程序。我想测试客户端和服务器应用程序之间的数据事务带宽(通过我使用套接字的方式)。我遇到的问题是我需要测试连接速度,比如说 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();
        }
    }
}

感谢您的帮助!!!

【问题讨论】:

    标签: c# sockets tcp timeout bandwidth


    【解决方案1】:

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

                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。
    猜你喜欢
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多