当客户端断开连接时,我的tcp/ip litsener发疯了/如何改进或修复它
本文关键字:疯了 何改进 发疯 litsener ip 连接 断开 客户端 tcp 我的 | 更新日期: 2023-09-27 18:09:45
你好,我有一个tcp/ip监听器,它工作完美,直到客户端断开连接或再次发送字符串,我不知道如何修复这个
TcpListener serverSocket = new TcpListener(****);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
TcpClient clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
int requestCount = 0;
while ((true))
{
try
{
//SQL SECTION OF CODE
MySqlDataReader rdr = null;
MySql.Data.MySqlClient.MySqlConnection conn = new MySqlConnection(*******);
conn.Open();
//TCP LISTENING SECtion of code
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[100025];
networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
//char[] delimiterChars = { ' ', '<', '>', ':', ''t' };
//string[] FinalData = dataFromClient.Split(delimiterChars);
//var DataLines = XDocument.Load(dataFromClient)
////PARSING THE XML STRING
//var dataLines = XDocument.Parse(dataFromClient)
// .Descendants("DataLine")
// .Select(n => new
// {
// Amount = n.Element("Amount").Value,
// TellNo = n.Element("TellNo").Value
// });
//foreach (var item in dataLines)
//{
// MySqlCommand cmd = new MySqlCommand("StringlistenerUpdate", conn);
// cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.Add(new MySqlParameter("@CustomerBalance", item.Amount));
// cmd.Parameters.Add(new MySqlParameter("@CustomerAccountID", item.TellNo));
// rdr = cmd.ExecuteReader();
// conn.Close();
// Console.WriteLine("{0}-{1}", item.Amount, item.TellNo);
//}
var Voicelines = XDocument.Parse(dataFromClient)
.Descendants("VoiceLine")
.Select(n => new
{
Amount = n.Element("Amount").Value,
TellNo = n.Element("TellNo").Value
});
foreach (var item in Voicelines)
{
MySqlCommand cmd = new MySqlCommand("StringlistenerUpdate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("CustomerBalance", item.Amount));
cmd.Parameters.Add(new MySqlParameter("CustomerAccountID", item.TellNo));
cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("{0}-{1}", item.Amount, item.TellNo);
}
/// Console.WriteLine(" >> Data from client : " + DataLines);
string serverResponse = "Last Message from client :" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
catch (OverflowException ex)
{
Console.WriteLine(ex.ToString());
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Suppressed exception: " + ex);
}
}
}
}
}
关于如何解决这个问题(解释为什么和如何)和改进它,如果这个问题是不可接受的或需要更多的信息,请让我知道我已经调试了它,除了客户端断开连接并重新发送(但一般来说,我发现这是一个非常原始的tcp/ip侦听器
thank you
使用Read
的返回值,不要使用ReceiveBufferSize
,因为它不像你想象的那样。
处理完一个连接后不要停止服务器套接字。服务器套接字负责监听。