zoukankan      html  css  js  c++  java
  • 会xiaotie的一个TCP问题

    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, 
    01024, 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, 
    01024, 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();
            } 
        }
    }
  • 相关阅读:
    硬核动图让你轻松弄懂递归,查找等概念
    Instagram API平台文档
    盲人都能编程,你有什么资格说自己不行
    10个经典智力推理题!据说答对7道,智力在140!
    我们该如何高效的学习
    从一碗小米粥谈装饰器模式,代理模式的区别
    html+css实现彩色渐变滑动条
    css实现彩色渐变滑动条
    [极客大挑战 2019]Knife 思路
    buuctf
  • 原文地址:https://www.cnblogs.com/Jax/p/1388202.html
Copyright © 2011-2022 走看看