【发布时间】:2015-09-01 19:38:29
【问题描述】:
我在使用 C# 中的 BeginReceive / BeginSend 读取从 TcpClient 发送到 TcpListener 的大消息时遇到问题。
我尝试在我的消息中附加一个四字节长度的标头,但有时我不会将它作为第一个导致问题的数据包接收。
例如,我发送了一个序列化对象,其中包含值 [204, 22, 0, 0] 作为 BeginSend 中字节数组的前四个字节。我在 BeginReceive 的服务器上收到的是 [17, 0, 0, 0]。我在发送简单字符串并且消息通过时检查了 Wireshark,但是我的代码有问题。另一个例子,当我发送“A b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0”进行测试时,我不断收到“p q r ... 8 9 0”。
我认为如果数据包被乱序接收或丢失,TCP 将处理丢失数据包的重新提交和/或在发送之前重新排序它们。这意味着我的前四个字节应始终在标题中包含我的消息大小。但是,看看上面的例子,情况并非如此,或者是我的代码在某个地方搞砸了。
在下面的代码之后,我只看它是否发送特定命令或对象类型,然后根据收到的内容进行响应。
我的核心功能已经到位,并且对我可以开始重构的问题有了更好的理解,但这确实让我停滞不前。
几天来,我一直在用头撞墙,试图调试它。我已经阅读了几篇关于类似问题的文章和问题,但我还没有找到将建议的修复应用到这个特定实例的方法。
提前感谢您对此提供的帮助。
以下代码中的 YahtzeeClient 只是 TcpClient 与玩家信息的包装。
private void ReceiveMessageCallback(IAsyncResult AR)
{
byte[] response = new byte[0];
byte[] header = new byte[4];
YahtzeeClient c = (YahtzeeClient)AR.AsyncState;
try
{
// If the client is connected
if (c.ClientSocket.Client.Connected)
{
int received = c.ClientSocket.Client.EndReceive(AR);
// If we didn't receive a message or a message has finished sending
// reset the messageSize to zero to prepare for the next message.
if (received == 0)
{
messageSize = 0;
// Do we need to do anything else here to prepare for a new message?
// Clean up buffers?
}
else
{
// Temporary buffer to trim any blanks from the message received.
byte[] tempBuff;
// Hacky way to track if this is the first message in a series of messages.
// If messageSize is currently set to 0 then get the new message size.
if (messageSize == 0)
{
tempBuff = new byte[received - 4];
// This will store the body of the message on the *first run only*.
byte[] body = new byte[received - 4];
// Only copy the first four bytes to get the length of the message.
Array.Copy(_buffer, header, 4);
// Copy the remainder of the message into the body.
Array.Copy(_buffer, 4, body, 0, received - 4);
messageSize = BitConverter.ToInt32(header, 0);
Array.Copy(body, tempBuff, body.Length);
}
else
{
// Since this isn't the first message containing the header packet
// we want to copy the entire contents of the byte array.
tempBuff = new byte[received];
Array.Copy(_buffer, tempBuff, received);
}
// MessageReceived will store the entire contents of all messages in this tranmission.
// If it is an new message then initialize the array.
if (messageReceived == null || messageReceived.Length == 0)
{
Array.Resize(ref messageReceived, 0);
}
// Store the message in the array.
messageReceived = AppendToArray(tempBuff, messageReceived);
if (messageReceived.Length < messageSize)
{
// Begin receiving again to get the rest of the packets for this stream.
c.ClientSocket.Client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceiveMessageCallback, c);
// Break out of the function. We do not want to proceed until we have a complete transmission.
return;
}
// Send it to the console
string message = Encoding.UTF8.GetString(messageReceived);
**标记为已解决。解决方案是将消息包装在消息头和消息结束符中,然后修改代码以查找这些指示符。
使用套接字背后的原因是由于项目限制,无法选择 Web 服务。
【问题讨论】:
标签: c# sockets tcpclient tcplistener