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();
            } 
        }
    }
  • 相关阅读:
    AndroidStudio打开新项目后解决下载某版本gradle慢的问题
    GeoServer怎样修改线性地图的颜色样式
    GeoServer简介、下载、配置启动、发布shapefile全流程(图文实践)
    AndroidStudio中打开新项目提示:This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot o
    若依微服务版在Windows上通过jar包运行业务模块时提示:Failed to determine s suitable driver class
    若依微服务版后台服务通过jar包部署到Windows服务器
    腾讯云centos7安装MySQL
    使用Navicat for MySQL把本地数据库上传到服务器
    浏览器页面乱码
    事务的配置
  • 原文地址:https://www.cnblogs.com/Jax/p/1388202.html
Copyright © 2011-2022 走看看