zoukankan      html  css  js  c++  java
  • Common.TcpLib _Bgz_ConnectionState.cs

    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace Common.TcpLib
    {

        public class _Bgz_ConnectionState
        {
            #region var
            public Socket _conn = null;    //Socket
            public byte[] _buffer;  //缓冲区
            public int _count = 0;  //当前接收次数
            public bool _getonceall = false; //是否一次性得到接收的内容

            public Stream _dataStream = new MemoryStream();//数据流

            #endregion

            #region ctor
            public _Bgz_ConnectionState()
            {
                Init();
            }

            public _Bgz_ConnectionState(Socket socket)
            {
                _buffer = new byte[0];
                _getonceall = false;
                _count = 0;
                _dataStream = new MemoryStream();
                _conn = socket;
            }

            #endregion

            #region property

            public EndPoint RemoteEndPoint
            {
                get { return _conn.RemoteEndPoint; }
            }

            public EndPoint LocalEndPoint
            {
                get { return _conn.LocalEndPoint; }
            }

            public string StrRemoteEndPoint
            {
                get
                {
                    IPEndPoint info = (IPEndPoint)_conn.RemoteEndPoint;
                    return info.Address.ToString() + ":" + info.Port.ToString();
                }
            }

            public string StrLocalEndPoint
            {
                get
                {
                    IPEndPoint info = (IPEndPoint)_conn.LocalEndPoint;
                    return info.Address.ToString() + ":" + info.Port.ToString();
                }
            }

            public int AvailableData
            {
                get { return _conn.Available; }
            }

            public bool Connected
            {
                get { return _conn.Connected; }
            }

            #endregion

            public void Init()
            {
                _conn = null;
                _buffer = new byte[0];
                _getonceall = false;
                _count = 0;
                _dataStream = new MemoryStream();
            }

            public int Read(byte[] buffer, int offset, int count)
            {
                try
                {
                    if (_conn.Available > 0)
                        return _conn.Receive(buffer, offset, count, SocketFlags.None);
                    else return 0;
                }
                catch
                {
                    return 0;
                }
            }

            public bool Write(byte[] buffer, int offset, int count)
            {
                try
                {
                    _conn.Send(buffer, offset, count, SocketFlags.None);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

    }

  • 相关阅读:
    JavaWeb项目报错:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
    Linux+Nginx+Tomcat+Redis实现负载均衡,应用集群及session共享
    CENTOS7下安装REDIS
    centOS7访问nginx失败解决-.0:80 failed (98: Address already in use)解决
    Nginx 配置反向代理后,页面中取绝对URL地址的问题显示代理端口
    linux中安装jdk
    在linux系统防火墙中放开对8080端口的限制
    Removing obsolete files from server... Could not clean server of obsolete files
    js关于小数点失精算法修正0.07*100竟然=7.000000000000001
    jquery ajax后台向前台传list 前台用jquery $.each遍历list
  • 原文地址:https://www.cnblogs.com/bigmouthz/p/946186.html
Copyright © 2011-2022 走看看