我试图让标准示例“echo”客户端/服务器应用程序尽快运行,我确信网络是一个限制因素。我有一个 1 Gb 的网卡,当我使用资源监视器时,我只能从客户端获得 7 mb。
我了解套接字、消息帧和长度指示器的基础知识,接收长度指示器指示的所有字节。保持活动数据包,半开连接等。
我开始使用标准套接字操作,然后切换到使用异步。(我没有将发送更改为异步,因为有人[似乎知识渊博的人说它不应该有任何影响])我有完全相同的表现,我只能认为所有材料都假设我有其他工作可以在同一个线程上完成。但是在我的快速测试中,我有一个线程专门用于循环发送和另一个完全不同的线程来接收。
我已经尝试了所有方法,但完全不知道在哪里可以获得更多性能。我使用了 IPerf,它报告的返回速度为每秒 1 吉比特,资源监视器也显示它正在消耗带宽。
即使有人可以为我指出一个更完整的例子。我遇到的大多数都是微不足道的或不完整的。
这是通用代码。
class Program
{
private static Socket sock;
private static BlockingCollection<string> queue;
private static int bytesReceived;
private static byte[] dataBuffer;
private static readonly byte[] lengthBuffer = new byte[4];
private static byte[] PrependLengthIndicator(byte[] data)
{
return BitConverter.GetBytes(data.Length).Concat(data).ToArray();
}
private static void Receive()
{
if (dataBuffer == null)
{
sock.BeginReceive(lengthBuffer, 0, 4, SocketFlags.None, ReceiveCallback, null);
}
else
{
sock.BeginReceive(dataBuffer, 0, bytesReceived, SocketFlags.None, ReceiveCallback, null);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
bytesReceived += sock.EndReceive(ar);
if (dataBuffer == null)
{
// Currently receiving length indicator
if (bytesReceived >= 4)
{
var length = BitConverter.ToInt32(lengthBuffer, 0);
dataBuffer = new byte[length];
bytesReceived = 0;
}
}
else
{
if (bytesReceived == dataBuffer.Length)
{
// Finished reading
var request = Encoding.ASCII.GetString(dataBuffer);
dataBuffer = null;
bytesReceived = 0;
queue.Add(request);
}
}
ContinueReading();
}
private static void ContinueReading()
{
// Read into the appropriate buffer: length or data
if (dataBuffer != null)
{
sock.BeginReceive(dataBuffer, bytesReceived, dataBuffer.Length - bytesReceived, SocketFlags.None, ReceiveCallback, null);
}
else
{
sock.BeginReceive(lengthBuffer, bytesReceived, lengthBuffer.Length - bytesReceived, SocketFlags.None, ReceiveCallback, null);
}
}
}
这是服务器部分:
static void Main(string[] args)
{
var listenSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSock.Bind(new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["LocalIp"]), 3333));
listenSock.Listen(10);
Console.WriteLine("Server started...");
sock = listenSock.Accept();
Console.WriteLine("Connection accepted.");
queue = new BlockingCollection<string>();
Receive();
var count = 0;
var sender = new Thread(() =>
{
while (true)
{
var bar = queue.Take() + "Resp";
count++;
var resp = Encoding.ASCII.GetBytes(bar);
var toSend = PrependLengthIndicator(resp);
if (count % 10000 == 0)
{
Console.WriteLine(bar);
}
sock.Send(toSend);
}
});
sender.Start();
}
这是客户端部分:
static void Main(string[] args)
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Connecting...");
sock.Connect(IPAddress.Parse(ConfigurationManager.AppSettings["EndPointIp"]), 3333);
Console.WriteLine("Connected.");
Receive();
var count = 0;
while(true)
{
count++;
var foo = "Echo-" + count;
var data = Encoding.ASCII.GetBytes(foo);
var toSend = PrependLengthIndicator(data);
sock.Send(toSend);
}
}