您好。我正在从NetworkStream读取数据,我将这些数据存储在数据库中。它 工作正常,过去2个月一旦出现任何错误,它只显示它。这次我无法存储该数据,当我重新启动应用程序时,它工作正常。有时它会像这样打破,我该如何解决这个问题?我的代码是:
tcpClient = (TcpClient)obj;
String remoteIP = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();
String remotePort = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port.ToString();
String tmp; // = "Connected to " + remoteIP + " : " + remotePort + "\r\n";
tmp = "";
//Add in list and end close the client
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[88];
int bytesRead;
double prt;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 88);
if (bytesRead > 10)
{
tmp = Encoding.ASCII.GetString(message).Substring(0, bytesRead);
// here am inserting tmp that message data to database.
}
}
catch (Exception ex)
{
//a socket error has occured
//Error
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
}
tcpClient.Close();