System.Net.Sockets.Socket.SendTo C# (CSharp) Méthode

SendTo() public méthode

public SendTo ( byte buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP ) : int
buffer byte
offset int
size int
socketFlags SocketFlags
remoteEP EndPoint
Résultat int
        public int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
            
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            // Validate input parameters.
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (remoteEP == null)
            {
                throw new ArgumentNullException(nameof(remoteEP));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            ValidateBlockingMode();
            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"SRC:{LocalEndPoint} size:{size} remoteEP:{remoteEP}");

            // CheckCacheRemote will check ConnectPermission for remoteEP.
            EndPoint endPointSnapshot = remoteEP;
            Internals.SocketAddress socketAddress = CheckCacheRemote(ref endPointSnapshot, false);

            // This can throw ObjectDisposedException.
            int bytesTransferred;
            SocketError errorCode = SocketPal.SendTo(_handle, buffer, offset, size, socketFlags, socketAddress.Buffer, socketAddress.Size, out bytesTransferred);

            // Throw an appropriate SocketException if the native call fails.
            if (errorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }

            if (_rightEndPoint == null)
            {
                // Save a copy of the EndPoint so we can use it for Create().
                _rightEndPoint = endPointSnapshot;
            }

            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.DumpBuffer(this, buffer, offset, size);
                NetEventSource.Exit(this, bytesTransferred);
            }
            return bytesTransferred;
        }

Same methods

Socket::SendTo ( byte buffer, EndPoint remoteEP ) : int
Socket::SendTo ( byte buffer, SocketFlags socketFlags, EndPoint remoteEP ) : int
Socket::SendTo ( byte buffer, int size, SocketFlags socketFlags, EndPoint remoteEP ) : int

Usage Example

 static void Main(string[] args)
 {
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9000);
     s.Bind(iep);
     EndPoint remote = (EndPoint)iep;
     byte[] data = new byte[1024];
     int k = s.ReceiveFrom(data, ref remote);
     Console.WriteLine("Loi chao tu Client:{0}",Encoding.ASCII.GetString(data,0,k));
     data = new byte[1024];
     data = Encoding.ASCII.GetBytes("Chao Client ket noi");
     s.SendTo(data, remote);
     while (true)
     {
         data = new byte[1024];
         string st;
         k=s.ReceiveFrom(data, ref remote);
         st = Encoding.ASCII.GetString(data, 0, k);
         Console.WriteLine("Du lieu tu Client Gui la:{0}", st);
         if (st.ToUpper().Equals("QUIT"))
             break;
         st = st.ToUpper();
         data = new byte[1024];
         data = Encoding.ASCII.GetBytes(st);
         s.SendTo(data, remote);
     }
     s.Close();
 }
All Usage Examples Of System.Net.Sockets.Socket::SendTo