English 中文(简体)
等待网络连接 C# 控制台应用程序完全启动
原标题:Waiting for networking C# console application to fully start

我遇到了一个问题, C# 启动时间缓慢, 导致 UDP 软件包开始下降。 下面, 我是为了减轻启动延迟而做的。 我基本上在前两个软件包传输之间再等10米。 这至少可以修补机器上的初始滴落。 我担心的是, 慢一点的机器可能需要比这个更长时间的延迟 。

private void FlushPacketsToNetwork()
{
    MemoryStream packetStream = new MemoryStream();

    while (packetQ.Count != 0)
    {
        byte[] packetBytes = packetQ.Dequeue().ToArray();
        packetStream.Write(packetBytes, 0, packetBytes.Length);
    }

    byte[] txArray = packetStream.ToArray();
    udpSocket.Send(txArray);

    txCount++;

    ExecuteStartupDelay();
}

// socket takes too long to transmit unless I give it some time to "warm up"
private void ExecuteStartupDelay()
{
    if (txCount < 3)
    {
        timer.SpinWait(10e-3);
    }
}

因此,我想知道是否有更好的方法让 C# 充分装载其所有依赖性? 我真的不介意它需要几秒钟来完全装载; 我只是不想做任何高带宽传输,直到 C# 完全准备就绪。

Additional relevant details

这是一个控制台应用程序, 网络传输是从一条单独的线线运行的, 主线只是等待按键终止网络发报机 。

program.Main 方法中,我试图通过使用最高优先级的合理方法,从我的申请中获取最大性能:

public static void Main(string[] args)
{
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
    ...
    Thread workerThread = new Thread(new ThreadStart(worker.Run));
    workerThread.Priority = ThreadPriority.Highest;
    workerThread.Start();
    ...
    Console.WriteLine("Press any key to stop the stream...");
    WaitForKeyPress();

    worker.RequestStop = true;
    workerThread.Join();

此外,我目前使用的套接字设置如下:

udpSocket = new Socket(targetEndPoint.Address.AddressFamily,
                       SocketType.Dgram,
                       ProtocolType.Udp);
udpSocket.Ttl = ttl;
udpSocket.SendBufferSize = 1024 * 1024;
udpSocket.Blocking = true;

udpSocket.Connect(targetEndPoint);

默认的 < code> SendBofferSize 是 8192, 所以我把它移动到一个兆字节, 但这个设置似乎对开始时的投放包没有影响 。

问题回答

我从评论中得知TCP不适合你选择(因为传输的固有延误),

因此,你实际上需要执行TCP(再传输)中的一些功能,但需要更稳健、更轻巧的方式。我还认为,你控制着接收方。

我建议您发送一些预设的包数。 然后等待确认。 例如, 每个包可以有一个不断增长的代号 。 每个 N 包, 收到程序后会将最后收到的包数发送给发件人 。 收到此号码后, 发件人会知道是否有必要重复最后一个 N 包 。

这种方式不应该对带宽造成很大伤害,你会得到关于所收到数据的信息(尽管没有保证)。

否则最好切换到TCP。 顺便说一下, 您尝试过使用TCP吗? 您的带宽因TCP受伤了多少?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签