(a), the client (MCU) to run for a period of time (about 30 seconds) to transmit data to the server, as their power (without informing the server disconnect TCP connection),
Again after 1 minute, electricity, running for a period of time (about 30 seconds) to transmit data to the server, is his power, the client recirculation process above,
(2), as a result, I found that the server program contains a large number of TCP connections have not been released, the System hint: System. OutOfMemoryException, see below
2, the program is as follows:
//record the communication Socket
DictionaryDic=new Dictionary (a);
//start listening
Private void btnListen_Click (object sender, EventArgs e)
{
BtnListen. Enabled=false;
//IP address
IPAddress IP=IPAddress. Parse (txtIP. Text);
//port
IPEndPoint point=new IPEndPoint (IP, int. Parse (txtPort. Text));
TcpListener listen=new TcpListener (point);
Listen. The Start ();
Listen. Server. Listen (50);
//create listening Socket
Try
{
ShowMsg (" server to start listening to ");
Thread the Thread=new Thread (AcceptInfo);
Thread. IsBackground=true;
Thread. Start (listen);
}
The catch (Exception ex)
{
BtnListen. Enabled=true;
ShowMsg (ex. Message);
}
}
Void AcceptInfo (object o)
{
As TcpListener TcpListener socket="o.
While (true)
{
Try
{
//create the communication Socket
TcpClient tSocket=socket. AcceptTcpClient ();
String point=tSocket. Client. RemoteEndPoint. ToString ();
ShowMsg (point + connection success! "" );
Dic. The Add (point, tSocket. Client);//add new TCP connection to a dictionary dic
//receive messages
Thread th=new Thread (ReceiveMsg);
Th. IsBackground=true;
Th. Start (tSocket);
}
The catch (Exception ex)
{
ShowMsg (ex. Message);
break;
}
}
}
//receive messages
Void ReceiveMsg (object o)
{
As TcpClient TcpClient client="o.
While (true)
{
//receive the client sends data over
Try
{
//define deposit from the client receiving data byte array
Byte [] buffer=new byte (1024 * 1024),
//will receive data in the buffer, and returns the length of the actual data
Int n=client. The client. The Receive (buffer);
If (n==0)
{
//even empty data will not is 0, the client will not send empty data, can only have the possibility of 0 at the end of the
If (client!=null)
{
String cpoint=client. Client. RemoteEndPoint. ToString ();
If (dic) either ContainsKey (cpoint))
Dic. Remove (cpoint);
String MSG="client" + cpoint + "off!" ;
ShowMsg (MSG);
Client. The Close ();
}
return;
}
//converting byte string
String words=System. Text. Encoding. The Default. Get string (buffer, 0, n);
//the following code parsing TCP
The catch (Exception ex)
{
If (client!=null)
{
String cpoint=client. Client. RemoteEndPoint. ToString ();
If (dic) either ContainsKey (cpoint))
Dic. Remove (cpoint);
String MSG="client" + cpoint + "off!" ;
ShowMsg (MSG);
Client. The Close ();
}
break;
}
}
}
Four, I'm in void ReceiveMsg (object o) if (n==0) and the catch (Exception ex) 2 test,
Breaking point tracking found as a result, when the client to force your breakpoints, server don't detect client TCP connection fails,
Five, excuse me:
1, because there is no clear failure caused by TCP connection System. OutOfMemoryException?
2, when the client TCP connection failure, the use of the client. The Close (); Memory can wipe out a TCP connection?
3, how to completely clear the failure of a TCP connection memory?
CodePudding user response:
You have a problem the TCP, features simple, concise code,Use monitoring parameter is not TRUE, the client receives information, whether or not complete, close the connection are mandatory,
You his grasp this point in time
CodePudding user response:
I use a regular client software test1, when manually disconnect client connection, in void ReceiveMsg (object o) if (n==0) branch, break point tracking can be detected by the client disconnects
2, disconnect network, in the void ReceiveMsg (object o) of the catch (Exception ex) disposition, breaking point tracking can detect connection disappeared,
But when I single-chip computer and the server after the success of the TCP connection, single-chip computer suddenly loses power, server anyway undetectable TCP connection disappeared,
I'm most interested in the question is:
How to clean thoroughly eliminate disappear [TCP connection] the accumulation of too much memory space?
CodePudding user response:
To do the heart ah, no heartbeat is turned off,CodePudding user response: