Export (0) Print
Expand All

Socket.SendTo Method

Sends data to a specific endpoint.

Overload List

Sends data to the specified endpoint.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function SendTo(Byte(), EndPoint) As Integer
[C#] public int SendTo(byte[], EndPoint);
[C++] public: int SendTo(unsigned char __gc[], EndPoint*);
[JScript] public function SendTo(Byte[], EndPoint) : int;

Sends data to a specific endpoint using the specified SocketFlags.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function SendTo(Byte(), SocketFlags, EndPoint) As Integer
[C#] public int SendTo(byte[], SocketFlags, EndPoint);
[C++] public: int SendTo(unsigned char __gc[], SocketFlags, EndPoint*);
[JScript] public function SendTo(Byte[], SocketFlags, EndPoint) : int;

Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function SendTo(Byte(), Integer, SocketFlags, EndPoint) As Integer
[C#] public int SendTo(byte[], int, SocketFlags, EndPoint);
[C++] public: int SendTo(unsigned char __gc[], int, SocketFlags, EndPoint*);
[JScript] public function SendTo(Byte[], int, SocketFlags, EndPoint) : int;

Sends the specified number of bytes of data to the specified endpoint, starting at the specified location in the buffer, and using the specified SocketFlags.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function SendTo(Byte(), Integer, Integer, SocketFlags, EndPoint) As Integer
[C#] public int SendTo(byte[], int, int, SocketFlags, EndPoint);
[C++] public: int SendTo(unsigned char __gc[], int, int, SocketFlags, EndPoint*);
[JScript] public function SendTo(Byte[], int, int, SocketFlags, EndPoint) : int;

Example

[Visual Basic, C#, C++] The following example sends a connectionless datagram to the specified remote host. The offset, size, and SocketFlags are passed to the SendTo method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SendTo. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Shared Sub SendTo4()
    Dim hostEntry As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint)
    s.Close()
End Sub 'SendTo4



[C#] 
public static void SendTo4()
{
    IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint);
    s.Close();
}


[C++] 
static void SendTo4()
{
    IPHostEntry* hostEntry = Dns::Resolve(Dns::GetHostName());
    IPEndPoint* endPoint = new IPEndPoint(hostEntry->AddressList[0], 11000);

    Socket* s = new Socket(endPoint->Address->AddressFamily,
        SocketType::Dgram,
        ProtocolType::Udp);
  
    Byte msg[]= Encoding::ASCII->GetBytes(S"This is a test");
    Console::WriteLine(S"Sending data.");
    // This call blocks. 
    s->SendTo(msg, 0, msg.Length, SocketFlags::None, endPoint);
    s->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace

Show:
© 2014 Microsoft