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

SendToAsync() public method

public SendToAsync ( SocketAsyncEventArgs e ) : bool
e SocketAsyncEventArgs
return bool
        public bool SendToAsync(SocketAsyncEventArgs e)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, e);
            bool retval;

            if (CleanedUp)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(RemoteEndPoint));
            }

            // Check permissions for connect and prepare SocketAddress
            EndPoint endPointSnapshot = e.RemoteEndPoint;
            e._socketAddress = CheckCacheRemote(ref endPointSnapshot, false);

            // Prepare for the native call.
            e.StartOperationCommon(this);
            e.StartOperationSendTo();

            // Make the native call.
            int bytesTransferred;
            SocketError socketError;

            // Wrap native methods with try/catch so event args object can be cleaned up.
            try
            {
                socketError = e.DoOperationSendTo(_handle, out bytesTransferred);
            }
            catch
            {
                // Clear in-use flag on event args object. 
                e.Complete();
                throw;
            }

            // Handle completion when completion port is not posted.
            if (socketError != SocketError.Success && socketError != SocketError.IOPending)
            {
                e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None);
                retval = false;
            }
            else
            {
                retval = true;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this, retval);
            return retval;
        }
        #endregion

Usage Example

示例#1
0
        public void SendState(X360State state)
        {
            byte[] data = state.GetState();

            if (null != this.netduinoPlusAddress)
            {
                Debug.WriteLine("Sending:" + state.ToString());
#if WINDOWS
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();

                socketArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(this.netduinoPlusAddress), 9999);
                socketArgs.SetBuffer(data, 0, data.Length);

                socketArgs.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { });

                socket.SendToAsync(socketArgs);
#endif
            }
            else
            {
#if WINDOWS
                try
                {
                    if ((false == IsTheSame(data)) || (null == this.port))
                    {
                        if (null == this.port)
                        {
                            if (this.framesToIgnore-- <= 0)
                            {
                             //   if (Array.Exists<String>(SerialPort.GetPortNames(), s => s == this.portName))
                                {
                                    this.port = new SerialPort(this.portName, 115200/* this.config.ArduinoPortBaud*/, Parity.None, 8, StopBits.One);
                                    this.port.DataReceived += this.DataReceived;
                                    this.port.Open();
                                    Thread.Sleep(2000); // wait for bloody arduino nano.
                                }
                            }
                        }

                        if (null != this.port)
                        {
                            this.port.Write(data, 0, data.Length);

                            this.lastState = data;
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.port = null;
                    Debug.WriteLine(ex.Message);
                    this.framesToIgnore = 60; // too lazy to do some tread sleep magic.
                }
#endif
            }
        }
All Usage Examples Of System.Net.Sockets.Socket::SendToAsync