这是我的TCP侦听器,它读取收到的数据:
private void Update()
{
//Console.WriteLine("Call");
if (!serverStarted)
{
return;
}
foreach (ServerClient c in clients.ToList())
{
// Is the client still connected?
if (!IsConnected(c.tcp))
{
c.tcp.Close();
disconnectList.Add(c);
Console.WriteLine(c.connectionId + " has disconnected.");
CharacterLogout(c.connectionId);
continue;
//Console.WriteLine("Check for connection?\n");
}
else
{
// Check for message from Client.
NetworkStream s = c.tcp.GetStream();
if (s.DataAvailable)
{
StreamReader reader = new StreamReader(s, true);
string data = reader.ReadLine();
if (data != null)
{
OnIncomingData(c, data);
}
}
//continue;
}
}
for (int i = 0; i < disconnectList.Count - 1; i++)
{
clients.Remove(disconnectList[i]);
disconnectList.RemoveAt(i);
}
}
private bool IsConnected(TcpClient c)
{
try
{
if (c != null && c.Client != null && c.Client.Connected)
{
if (c.Client.Poll(0, SelectMode.SelectRead))
{
return !(c.Client.Receive(new byte[1], SocketFlags.Peek) == 0);
}
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
private void StartListening()
{
server.BeginAcceptTcpClient(OnConnection, server);
}
通常情况下这很好。 例如,当我每2秒,3秒或更长的时间间隔发送一个请求时,一切正常。
然而,当我发送示例2的请求时,每1秒甚至更少的时间发送3个请求,在开始时它工作正常,一段时间后它只是剪切了收到的数据。
如果我经常发送请求,我会收到问题。以下是收到数据的示例。
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"257.8289","position.z":"251.5683","position.y":"2.798551"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"257.8895","position.z":"251.6447","position.y":"2.796823"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"257.9507","position.z":"251.7218","position.y":"2.795079"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.0536","position.z":"251.8516","position.y":"2.792141"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.2184","position.z":"252.0594","position.y":"2.793153"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.2699","position.z":"252.1243","position.y":"2.795335"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.3738","position.z":"252.2554","position.y":"2.799742"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.5936","position.z":"252.5325","position.y":"2.80906"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.7353","position.z":"252.7112","position.y":"2.815068"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.7883","position.z":"252.778","position.y":"2.817239"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"258.9598","position.z":"252.9942","position.y":"2.809892"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.0203","position.z":"253.0705","position.y":"2.806783"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.0894","position.z":"253.1577","position.y":"2.803234"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.2439","position.z":"253.3524","position.y":"2.795195"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.4146","position.z":"253.5677","position.y":"2.784776"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.5829","position.z":"253.7799","position.y":"2.772557"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.7452","position.z":"253.9845","position.y":"2.762063"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"259.8754","position.z":"254.1487","position.y":"2.760789"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"260.0437","position.z":"254.3609","position.y":"2.759143"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"260.2104","position.z":"254.5711","position.y":"2.757511"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"260.5002","position.z":"254.8992","position.y":"2.756335"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"260.9822","position.z":"255.1375","position.y":"2.765062"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"261.5056","position.z":"255.2772","position.y":"2.787397"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"261.5968","position.z":"255.3015","position.y":"2.791161"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"261.7819","position.z":"255.3509","position.y":"2.797171"},"connectionId":1}
Character position Updated
{"header":"1x008","data":{"characterId":"1","position.x":"261.8941","position.z":"255.3809","position.y":"2.799694"},"connectionId":1}
Character position Updated
ion.z":"255.5631","position.y":"2.815049"},"connectionId":1}
正如您在最后一行中看到的那样,JSON被破坏了。数据无故缩减,它会使我的代码失效。
我可以确认客户端为服务器/侦听器端显示的最后一行发送有效的JSON字符串。
我认为它可能与缓冲区大小相关,但我在这个TCP通信中非常新。
这是来自客户端的发送功能:
public void Send(string header, Dictionary<string, string> data)
{
if (stream.CanRead)
{
socketReady = true;
}
if (!socketReady)
{
return;
}
JsonData SendData = new JsonData();
SendData.header = "1x" + header;
foreach (var item in data)
{
SendData.data.Add(item.Key.ToString(), item.Value.ToString());
}
SendData.connectionId = connectionId;
string json = JsonConvert.SerializeObject(SendData);
var howManyBytes = json.Length * sizeof(Char);
writer.WriteLine(json);
writer.Flush();
Debug.Log("Client World:" + json);
}
我的错误在哪里,我该如何解决?
答案 0 :(得分:1)
A StreamReader
包含两个内部缓冲区 - 一个已解码但尚未消耗的字符,以及一个已接收但尚未解码的字节(可能是用于多字节编码的不完整字符)。当它需要数据时,它首先检查缓冲区,如果它没有它需要的东西,那么它会读取&#34;一些&#34;来自流的数据,直到它可以完成当前请求(ReadLine()
或其他)。在某些情况下,使用稀疏消息可能意味着它会完整地读取单个消息,这可能会错误地表明您的代码正在运行。但是,当多个消息靠近在一起时,它可以从套接字 over-read - 接收所有一条消息和另一条消息的一部分。这意味着每个套接字保留 StreamReader
非常非常重要 - 否则当您将其放在地板上时,您将丢失套接字消耗的任何数据但未在之前的ReadLine()
中阅读。这会使您看到的结果完全。所以:作为一个简单的修复:不是存储套接字,而是使用StreamReader
存储套接字的元组 - 并且每个套接字只创建一个读取器。
或者,你可以写得更好&#34;框架&#34;代码并手动缓冲数据(某处),直到您的框架完成(在您的情况下为行尾)。然后处理后缓冲区,小心保留帧后剩下的任何额外字节。如果您不熟悉网络代码,第一个选项可能更简单。但是第二个选项更好 - 它可以避免在收到部分帧时阻塞。目前,单个客户端可以通过发送一些没有换行的文本来杀死你的服务器,只是保持套接字打开(可能每隔30秒运行一次额外的字符)。这将导致您的服务器永远停留在ReadLine()
- 或直到您的服务器OOM,因为如果传入文本。哎呀,发送一大块没有换行的文本!
这个可能就像(使用C#vCurrent)
一样简单List<(Socket socket, StreamReader reader)> sockets = ...
...
// (adding)
var newSocket = ...
var reader = new StreamReader(...); // on newSocket
sockets.Add((newSocket, reader));
// ...
foreach(var pair in sockets) {
// ...
var socket = pair.socket;
var line = pair.reader.ReadLine();
if(line != null) {...}
// ...
}