插座低传输速度 - Win7 .Net4 - CentOSx64单声道2.10

时间:2012-03-16 13:51:20

标签: sockets c#-4.0 mono client-server centos

我创建了一个简单的客户端/服务器应用程序,当我在本地或在Lan网络下运行时,它运行良好(1MB / s,这是我为服务器设置的最大速度)。但是当我尝试在我的Dedicate Server / VPS中运行它时,我的下载速度会变慢。 我在服务器上使用CentOS,在运行它时使用Mono,Mono版本是2.10.2,CentOs是64位版本。使用框架4创建。

速度测试:

本地:1MB

Lan:1MB

在CentOS上运行服务器:0~10~20 KB

我的连接速度是2Mb或~250KB。它会让我全速前进。但非常罕见,我不知道为什么它有时给我全速,有时甚至没有速度或为什么有时只有10KB,其他时间只有20KB。另外,我在Win7桌面上运行客户端部分。这是服务器和客户端部分的代码:

服务器:

class Program
{
    private static BackgroundWorker _ListeningWorker;
    private static BackgroundWorker _QWorker;
    private static System.Net.Sockets.Socket _Server;
    private static List<System.Net.Sockets.Socket> ConnectedClients = new List<System.Net.Sockets.Socket>();
    static void Main(string[] args)
    {
        Program._ListeningWorker = new BackgroundWorker();
        Program._ListeningWorker.WorkerSupportsCancellation = true;
        Program._ListeningWorker.DoWork += _ListeningWorker_DoWork;
        Program._QWorker = new BackgroundWorker();
        Program._QWorker.WorkerSupportsCancellation = true;
        Program._QWorker.DoWork += _QWorker_DoWork;
        Program.Start();
        while (true)
        {
            Console.Clear();
            Console.WriteLine("1.0.0.1");
            Console.WriteLine(Program.ConnectedClients.Count.ToString());
            System.Threading.Thread.Sleep(1000);
        }
    }
    public static bool Start()
    {
        if (!Program._ListeningWorker.IsBusy)
        {
            Program._Server = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8081);
            Program._Server.Bind(ipLocal);
            Program._Server.Listen(10);

            Program._ListeningWorker.RunWorkerAsync();
            Program._QWorker.RunWorkerAsync();
        }

        return true;
    }
    private static void _ListeningWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        while (!Program._ListeningWorker.CancellationPending)
        {
            if (Program._Server.Poll(10, SelectMode.SelectRead))
            {
                lock (ConnectedClients)
                {
                    Program.ConnectedClients.Add(Program._Server.Accept());
                }
            }
            System.Threading.Thread.Sleep(1);
        }
        Program._Server.Close();
    }
    private static void _QWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        byte[] array = new byte[1024];
        Random random = new Random();
        while (!Program._QWorker.CancellationPending)
        {
            if (ConnectedClients.Count > 0)
            {
                System.Net.Sockets.Socket[] st;
                lock (ConnectedClients)
                {
                    st = new System.Net.Sockets.Socket[Program.ConnectedClients.Count];
                    ConnectedClients.CopyTo(st);
                }

                foreach (System.Net.Sockets.Socket ser in st)
                {

                    random.NextBytes(array);
                    try
                    {
                        ser.BeginSend(array, 0, array.Length, SocketFlags.None, (AsyncCallback)delegate(IAsyncResult ar)
                        {
                            try
                            {
                                ser.EndSend(ar);
                            }
                            catch (Exception)
                            {
                                iDissconnected(ser);
                            }
                        }, null);
                    }
                    catch (Exception)
                    {
                        iDissconnected(ser);
                    }
                }
            }
            System.Threading.Thread.Sleep(1);
        }
        Program._Server.Close();
    }
    internal static void iDissconnected(System.Net.Sockets.Socket client)
    {
        lock (ConnectedClients)
            for (int i = 0; i < ConnectedClients.Count; i++)
                if (ConnectedClients[i].Equals(client))
                {
                    ConnectedClients.RemoveAt(i);
                    i--;
                }
    }
}

客户端:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter IP Address: ");
        string Address = Console.ReadLine();
        Console.WriteLine();
        ushort Port = 8081;
        System.Net.Sockets.Socket Client;
        Client = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        Client.Connect(Address, (int)Port);
        Console.WriteLine("Connected");
        int p = 0;
        int t = Environment.TickCount;
        while (true)
        {
            if (Client.Available > 0)
            {
                byte[] z = new byte[1024];
                int r = Client.Receive(z); ;
                p += r;
            }
            else
            {
                System.Threading.Thread.Sleep(10);
            }
            if (Environment.TickCount - t >= 1000)
            {
                t = Environment.TickCount;
                Console.WriteLine(Program.FormatFileSizeAsString(p) + " Readed,");
                p = 0;
            }
        }
    }
    public static string FormatFileSizeAsString(int len)
    {
        if (len < 750 && len > 0)
            return "0.0 B ~";
        string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
        int i;
        double dblSByte = len;
        for (i = 0; (int)(len / 1024) > 0; i++, len /= 1024)
            dblSByte = len / 1024.0;
        return String.Format("{0:0.0} {1}", dblSByte, Suffix[i]);
    }
}

谢谢大家,请告诉我您对可能出现的问题的看法。

1 个答案:

答案 0 :(得分:0)

每个连接都有ISP的某种限制。 解决很有趣。我曾经在我的请求之前发送HTTP标头,因此ISP增加了我的速度。