xiaotie,你看我用红色标出来的两句话,这就是一旦掉线,Server就立刻会知道,并触发Disconnect事件。
Code
namespace Communication.Server
{
public class SocketHelper
{
public event EventHandler Disconnected;
public event EventHandler BuildText;
private TcpClient myClient;
private SplitBytes sb;
private byte[] recByte;
#region LoginStatus
private bool loginStatus;
public bool LoginStatus
{
set { loginStatus = value; }
}
#endregion
public SocketHelper(Object myClient)
{
this.myClient = (TcpClient)myClient;
sb = new SplitBytes();
recByte = new byte[1024];
loginStatus = false;
}
public void ReadStream()
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(myReadCallBack);
myClient.GetStream().BeginRead(recByte, 0, 1024, GetStreamMsgCallback, null);
}
private void myReadCallBack(IAsyncResult ar)
{
int numberOfBytesRead;
try
{
lock (myClient.GetStream())
{
numberOfBytesRead = myClient.GetStream().EndRead(ar);
if (numberOfBytesRead < 1)
{
//If a value less than 1 received that means that client disconnected
myClient.Close();
//raise the Disconnected Event
if (Disconnected != null)
{
EventArgs e = new EventArgs();
Disconnected(null, e);
}
return;
}
}
sb.AddBytes(recByte, numberOfBytesRead);
recByte = new byte[1024];
if (myClient.GetStream().DataAvailable)
{
myClient.GetStream().BeginRead(recByte, 0, recByte.Length, new AsyncCallback(myReadCallBack), myClient.GetStream());
}
else
{
if (BuildText != null)
{
Object obj = SerializationHelper.GetDeserializationObject(sb.ReceiveAllByte);
BuildText(obj, null);
}
sb.Dispose();
if (loginStatus)
{
lock (myClient.GetStream())
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(myReadCallBack);
myClient.GetStream().BeginRead(recByte, 0, 1024, GetStreamMsgCallback, null);
}
}
}
}
catch
{
myClient.Close();
if (Disconnected != null)
{
EventArgs e = new EventArgs();
Disconnected(null, e);
}
}
}
public void Close()
{
myClient.Close();
}
public void Send(object obj)
{
BinaryWriter writer = new BinaryWriter(myClient.GetStream()); ;
writer.Write(SerializationHelper.GetSerializationBytes(obj));
writer.Flush();
}
}
}
namespace Communication.Server
{
public class SocketHelper
{
public event EventHandler Disconnected;
public event EventHandler BuildText;
private TcpClient myClient;
private SplitBytes sb;
private byte[] recByte;
#region LoginStatus
private bool loginStatus;
public bool LoginStatus
{
set { loginStatus = value; }
}
#endregion
public SocketHelper(Object myClient)
{
this.myClient = (TcpClient)myClient;
sb = new SplitBytes();
recByte = new byte[1024];
loginStatus = false;
}
public void ReadStream()
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(myReadCallBack);
myClient.GetStream().BeginRead(recByte, 0, 1024, GetStreamMsgCallback, null);
}
private void myReadCallBack(IAsyncResult ar)
{
int numberOfBytesRead;
try
{
lock (myClient.GetStream())
{
numberOfBytesRead = myClient.GetStream().EndRead(ar);
if (numberOfBytesRead < 1)
{
//If a value less than 1 received that means that client disconnected
myClient.Close();
//raise the Disconnected Event
if (Disconnected != null)
{
EventArgs e = new EventArgs();
Disconnected(null, e);
}
return;
}
}
sb.AddBytes(recByte, numberOfBytesRead);
recByte = new byte[1024];
if (myClient.GetStream().DataAvailable)
{
myClient.GetStream().BeginRead(recByte, 0, recByte.Length, new AsyncCallback(myReadCallBack), myClient.GetStream());
}
else
{
if (BuildText != null)
{
Object obj = SerializationHelper.GetDeserializationObject(sb.ReceiveAllByte);
BuildText(obj, null);
}
sb.Dispose();
if (loginStatus)
{
lock (myClient.GetStream())
{
AsyncCallback GetStreamMsgCallback = new AsyncCallback(myReadCallBack);
myClient.GetStream().BeginRead(recByte, 0, 1024, GetStreamMsgCallback, null);
}
}
}
}
catch
{
myClient.Close();
if (Disconnected != null)
{
EventArgs e = new EventArgs();
Disconnected(null, e);
}
}
}
public void Close()
{
myClient.Close();
}
public void Send(object obj)
{
BinaryWriter writer = new BinaryWriter(myClient.GetStream()); ;
writer.Write(SerializationHelper.GetSerializationBytes(obj));
writer.Flush();
}
}
}