用于多个异步发送的同一缓冲区
对于并行运行的多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您不修改缓冲区中的数据,这应该没问题。
This should be fine as long as you don't modify the data in the buffer.