System.Net.Sockets.NetworkStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int size ) : int
buffer byte
offset int
size int
return int
        public override int Read(byte[] buffer, int offset, int size)
        {
#if DEBUG
            using (DebugThreadTracking.SetThreadKind(ThreadKinds.User | ThreadKinds.Sync))
            {
#endif
                bool canRead = CanRead;  // Prevent race with Dispose.
                if (_cleanedUp)
                {
                    throw new ObjectDisposedException(this.GetType().FullName);
                }
                if (!canRead)
                {
                    throw new InvalidOperationException(SR.net_writeonlystream);
                }

                // Validate input parameters.
                if (buffer == null)
                {
                    throw new ArgumentNullException(nameof(buffer));
                }
                if (offset < 0 || offset > buffer.Length)
                {
                    throw new ArgumentOutOfRangeException(nameof(offset));
                }
                if (size < 0 || size > buffer.Length - offset)
                {
                    throw new ArgumentOutOfRangeException(nameof(size));
                }

                Socket chkStreamSocket = _streamSocket;
                if (chkStreamSocket == null)
                {
                    throw new IOException(SR.Format(SR.net_io_readfailure, SR.net_io_connectionclosed));
                }

                try
                {
                    int bytesTransferred = chkStreamSocket.Receive(buffer, offset, size, 0);
                    return bytesTransferred;
                }
                catch (Exception exception)
                {
                    if (exception is OutOfMemoryException)
                    {
                        throw;
                    }

                    // Some sort of error occurred on the socket call,
                    // set the SocketException as InnerException and throw.
                    throw new IOException(SR.Format(SR.net_io_readfailure, exception.Message), exception);
                }
#if DEBUG
            }
#endif
        }

Usage Example

示例#1
0
        /// <summary>
        /// 初始化实例并开启侦测
        /// </summary>
        /// <param name="inputClient"></param>
        /// <param name="replay"></param>
        public GameData(ref TcpClient inputClient, Action<string, byte[]> replay)
        {
            client = inputClient;
            stream = inputClient.GetStream();
            ReplyNewMessage = new Action<string, byte[]>(replay);
            stopReadFlag = false;

            Task.Run(() =>
            {

                while (true) {
                    if (stopReadFlag == true) { break; }

                    //读取数据长度
                    byte[] buffer = new byte[4];
                    stream.Read(buffer, 0, 4);

                    int length = BitConverter.ToInt32(buffer, 0);

                    //读取正文
                    buffer = new byte[length];
                    stream.Read(buffer, 0, length);

                    string head;
                    byte[] contant;

                    BallanceOnline.CombineAndSplitSign.Split(buffer, out head, out contant);

                    //调用委托处理
                    ReplyNewMessage(head, contant);
                }

            });
        }
All Usage Examples Of System.Net.Sockets.NetworkStream::Read