用于多个异步发送的同一缓冲区

发布于 2024-10-11 13:29:10 字数 2193 浏览 5 评论 0原文

对于并行运行的多个 socket::send 使用相同的缓冲区是否正确?我没有发现我的 C# 测试应用程序有任何问题。但我还没有看到任何这方面的例子或文档。所以我正在寻找这方面的权威参考资料。

using System;
using System.Net;
using System.Net.Sockets;

namespace ReuseSocketBuffer
{
    class Program
    {
        static byte[] reciveBuffer = new byte[1024];
        static byte[] senderBuffer = new byte[1024];

        static void Main(string[] args)
        {
            var sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(new IPEndPoint(IPAddress.Any, 0));
            listener.Listen(1);
            var ar1 = listener.BeginAccept(null, null);

            sender.Connect(new IPEndPoint(IPAddress.Loopback, (listener.LocalEndPoint as IPEndPoint).Port));

            var receiver = listener.EndAccept(ar1);

            receiver.BeginReceive(reciveBuffer, 0, reciveBuffer.Length, SocketFlags.None, ReceiveCallback, receiver);

            var ee = new SocketAsyncEventArgs[1024];

            senderBuffer[0] = 1;
            senderBuffer[1023] = 2;

            for (; ; )
            {
                for (int i = 0; i < ee.Length; i++)
                {
                    ee[i] = new SocketAsyncEventArgs();
                    ee[i].SetBuffer(senderBuffer, 0, senderBuffer.Length);
                }

                for (int i = 0; i < ee.Length; i++)
                    sender.SendAsync(ee[i]);
            }
        }

        static int count = 0;

        static void ReceiveCallback(IAsyncResult ar)
        {
            var receiver = ar.AsyncState as Socket;

            int received = receiver.EndReceive(ar);
            while (received < 1024)
                received += receiver.Receive(reciveBuffer, received, reciveBuffer.Length - received, SocketFlags.None);

            Console.Write("\r{0}", count++);

            if (reciveBuffer[0] != 1 || reciveBuffer[1023] != 2)
                throw new Exception();

            receiver.BeginReceive(reciveBuffer, 0, reciveBuffer.Length, SocketFlags.None, ReceiveCallback, receiver);
        }
    }
}

Is it correct to use same buffer for multiple socket::send running in parallel? I do not see any issue with this my c# test application. But I have not see any examples or docs for this. So I am looking for authoritative references about this.

using System;
using System.Net;
using System.Net.Sockets;

namespace ReuseSocketBuffer
{
    class Program
    {
        static byte[] reciveBuffer = new byte[1024];
        static byte[] senderBuffer = new byte[1024];

        static void Main(string[] args)
        {
            var sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(new IPEndPoint(IPAddress.Any, 0));
            listener.Listen(1);
            var ar1 = listener.BeginAccept(null, null);

            sender.Connect(new IPEndPoint(IPAddress.Loopback, (listener.LocalEndPoint as IPEndPoint).Port));

            var receiver = listener.EndAccept(ar1);

            receiver.BeginReceive(reciveBuffer, 0, reciveBuffer.Length, SocketFlags.None, ReceiveCallback, receiver);

            var ee = new SocketAsyncEventArgs[1024];

            senderBuffer[0] = 1;
            senderBuffer[1023] = 2;

            for (; ; )
            {
                for (int i = 0; i < ee.Length; i++)
                {
                    ee[i] = new SocketAsyncEventArgs();
                    ee[i].SetBuffer(senderBuffer, 0, senderBuffer.Length);
                }

                for (int i = 0; i < ee.Length; i++)
                    sender.SendAsync(ee[i]);
            }
        }

        static int count = 0;

        static void ReceiveCallback(IAsyncResult ar)
        {
            var receiver = ar.AsyncState as Socket;

            int received = receiver.EndReceive(ar);
            while (received < 1024)
                received += receiver.Receive(reciveBuffer, received, reciveBuffer.Length - received, SocketFlags.None);

            Console.Write("\r{0}", count++);

            if (reciveBuffer[0] != 1 || reciveBuffer[1023] != 2)
                throw new Exception();

            receiver.BeginReceive(reciveBuffer, 0, reciveBuffer.Length, SocketFlags.None, ReceiveCallback, receiver);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-10-18 13:29:10

只要您不修改缓冲区中的数据,这应该没问题。

This should be fine as long as you don't modify the data in the buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文