网络流似乎停止阅读?
Network stream seems to stop reading?
我有一个程序使用 TCPClient 和网络流从 ip 和端口读取数据。消息不断地快速发送到程序。该程序旨在读取这些消息并翻译消息,以便它们以更易读的格式显示发送给用户的信息。
这似乎工作正常 - 大约 60 秒。由于某种原因,它似乎停止阅读消息。没有显示错误消息,只是没有新消息通过。当我停止并启动程序时,它再次正常工作,直到一分钟左右过去。
下面是我用过的代码。希望您能看到我可能出错的地方,这可能会导致流停止接收新消息。主要是:
public MainWindow()
{
InitializeComponent();
client.Connect(address, port); //client, address and port are declared as public and static
connected = true;
if (connected == true)
{
readInTxtBox.Text = "Connected";
}
Thread t = new Thread(ReadInandOutputToTextBoxIfInvoke);
t.IsBackground = true;
t.Start(); //the thread is where the messages are read in
}
这是方法:
private void ReadInandOutputToTextBoxIfInvoke()
{
while (true)
{
string message = "";
int x = 0;
int start = 35;
int messageLength;
int numberOfMessages;
NetworkStream nwStream = client.GetStream();
try
{
while ((x = nwStream.ReadByte()) != start) { if (x == -1) { continue; } } //if it doesnt begin with # or has gone over then break
//reads in message header which is length then number of messages
messageLength = nwStream.ReadByte();
numberOfMessages = nwStream.ReadByte();
string messageRecieved = new string(readMessage(nwStream, messageLength - 1));
string[] messages = messageRecieved.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < numberOfMessages; i++)
{
string messageToProcess = messages[i];
char messageType = messageToProcess[0];
switch (messageType)
{
case 'h':
try
{
heightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 'l':
try
{
longWarpHeightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 's':
try
{
status.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 'r':
try
{
calibrateMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
default:
break;
}
}
case 语句只是翻译消息,因此可能不相关。
Update - I have added a console.writeline to my catch statements to output any errors and no errors seem to occur. Please see below the edit I have made:
switch (messageType)
{
case 'h':
try
{
heightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch (Exception e)
{ Console.WriteLine(e.ToString()); }
break;
case 'l':
try
{
longWarpHeightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
case 's':
try
{
status.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
case 'r':
try
{
calibrateMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
default:
break;
}
问题原来是我正在通话的设备必须在一定时间内收到一条保持活动消息,否则它会关闭连接。它现在可以工作了——我每 8 秒向它发送一条保持活动状态的消息。
我有一个程序使用 TCPClient 和网络流从 ip 和端口读取数据。消息不断地快速发送到程序。该程序旨在读取这些消息并翻译消息,以便它们以更易读的格式显示发送给用户的信息。
这似乎工作正常 - 大约 60 秒。由于某种原因,它似乎停止阅读消息。没有显示错误消息,只是没有新消息通过。当我停止并启动程序时,它再次正常工作,直到一分钟左右过去。
下面是我用过的代码。希望您能看到我可能出错的地方,这可能会导致流停止接收新消息。主要是:
public MainWindow()
{
InitializeComponent();
client.Connect(address, port); //client, address and port are declared as public and static
connected = true;
if (connected == true)
{
readInTxtBox.Text = "Connected";
}
Thread t = new Thread(ReadInandOutputToTextBoxIfInvoke);
t.IsBackground = true;
t.Start(); //the thread is where the messages are read in
}
这是方法:
private void ReadInandOutputToTextBoxIfInvoke()
{
while (true)
{
string message = "";
int x = 0;
int start = 35;
int messageLength;
int numberOfMessages;
NetworkStream nwStream = client.GetStream();
try
{
while ((x = nwStream.ReadByte()) != start) { if (x == -1) { continue; } } //if it doesnt begin with # or has gone over then break
//reads in message header which is length then number of messages
messageLength = nwStream.ReadByte();
numberOfMessages = nwStream.ReadByte();
string messageRecieved = new string(readMessage(nwStream, messageLength - 1));
string[] messages = messageRecieved.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < numberOfMessages; i++)
{
string messageToProcess = messages[i];
char messageType = messageToProcess[0];
switch (messageType)
{
case 'h':
try
{
heightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 'l':
try
{
longWarpHeightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 's':
try
{
status.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
case 'r':
try
{
calibrateMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch { }
break;
default:
break;
}
}
case 语句只是翻译消息,因此可能不相关。
Update - I have added a console.writeline to my catch statements to output any errors and no errors seem to occur. Please see below the edit I have made:
switch (messageType)
{
case 'h':
try
{
heightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch (Exception e)
{ Console.WriteLine(e.ToString()); }
break;
case 'l':
try
{
longWarpHeightMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
case 's':
try
{
status.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
case 'r':
try
{
calibrateMsg.Translate(Utilities.ConvertHexStringToByteArray(messageToProcess.Substring(1)));
}
catch(Exception e) { Console.WriteLine(e.ToString()); }
break;
default:
break;
}
问题原来是我正在通话的设备必须在一定时间内收到一条保持活动消息,否则它会关闭连接。它现在可以工作了——我每 8 秒向它发送一条保持活动状态的消息。