客户端异常退出
服务器端怎么判断客户端是否异常退出(比如:断电)我采用的如下方式:
TcpListener tl;
private void BeginListen()
{
bool done=false;
ASCIIEncoding ascii=new ASCIIEncoding();
tl=new TcpListener(System.Net.IPAddress.Any,8000);
tl.Start();
while(!done)
{
Thread thread=new Thread(new ThreadStart(threadLis));
thread.Start();
}
}
public void threadLis()//接收客户端发来的信息,并定时向客户端发送信号
{
//定时向客户端发送信息
System.Windows.Forms.Timer vTimer = new System.Windows.Forms.Timer();
vTimer.Interval = (60000);
vTimer.Enabled = true;
vTimer.Tick += new EventHandler(sendToClient);
}
public void sendToClient(object sender,System.EventArgs e)//定时发送程序
{
byte[] b1=System.Text.Encoding.UTF8.GetBytes( "ask ");
byte[] b2=new byte[100];
ns.Write(b1,0,b1.Length);
int i=ns.Read(b2,0,b2.Length);
MessageBox.Show(System.Text.Encoding.UTF8.GetString(b2));
if(i==0)//客户端异常退出
{
}
}
运行时有System.outofMemoryException异常,请问是为什么?应该怎么做才行?
------解决方案-------------------- protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
直接写这里面