System.Net.Sockets.Socket.EndSend C# (CSharp) Method

EndSend() public method

public EndSend ( IAsyncResult asyncResult, SocketError &errorCode ) : int
asyncResult IAsyncResult
errorCode SocketError
return int
        public int EndSend(IAsyncResult asyncResult, out SocketError errorCode)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            OverlappedAsyncResult castedAsyncResult = asyncResult as OverlappedAsyncResult;
            if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
            {
                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
            }
            if (castedAsyncResult.EndCalled)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndSend"));
            }

            int bytesTransferred = castedAsyncResult.InternalWaitForCompletionInt32Result();
            castedAsyncResult.EndCalled = true;

            if (s_perfCountersEnabled)
            {
                if (bytesTransferred > 0)
                {
                    SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketBytesSent, bytesTransferred);
                    if (Transport == TransportType.Udp)
                    {
                        SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketDatagramsSent);
                    }
                }
            }

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"bytesTransffered:{bytesTransferred}");

            // Throw an appropriate SocketException if the native call failed asynchronously.
            errorCode = (SocketError)castedAsyncResult.ErrorCode;
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                UpdateStatusAfterSocketError(errorCode);
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error(this, new SocketException((int)errorCode));
                    NetEventSource.Exit(this, 0);
                }
                return 0;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this, bytesTransferred);
            return bytesTransferred;
        }

Same methods

Socket::EndSend ( IAsyncResult asyncResult ) : int

Usage Example

Example #1
0
        protected void SendCommand(Socket socket, string command, CommandSentEventHandler callback, bool closeConnection = false)
        {
            try
            {
                Wait();
                var byteData = DongleEncoding.Default.GetBytes(command);
                socket.BeginSend(byteData, 0, byteData.Length, 0, asyncResult =>
                {
                    int bytesSent;
                    try
                    {
                        bytesSent = socket.EndSend(asyncResult);
                    }
                    catch (Exception exception)
                    {
                        if (OnErrorOcurred != null)
                        {
                            OnErrorOcurred(exception);
                        }
                        if (OnSocketDisconnected != null)
                        {
                            OnSocketDisconnected(socket);
                        }
                        return;
                    }

                    IsSending = false;

                    if (callback != null)
                    {
                        callback(command, bytesSent);
                    }
                    if (closeConnection)
                    {
                        try
                        {
                            socket.Disconnect(false);
                            if (OnSocketDisconnected != null)
                            {
                                OnSocketDisconnected(socket);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (OnErrorOcurred != null)
                            {
                                OnErrorOcurred(exception);
                            }
                        }                        
                    }
                }, null);
            }
            catch (Exception)
            {                
                if (OnSocketDisconnected != null)
                {
                    OnSocketDisconnected(socket);
                }
            }            
        }
All Usage Examples Of System.Net.Sockets.Socket::EndSend