Socket.Send Method
Sends data to a connected Socket.
Overload List
Sends data to a connected Socket.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Send(Byte()) As Integer
[C#] public int Send(byte[]);
[C++] public: int Send(unsigned char __gc[]);
[JScript] public function Send(Byte[]) : int;
Sends data to a connected Socket using the specified SocketFlags.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Send(Byte(), SocketFlags) As Integer
[C#] public int Send(byte[], SocketFlags);
[C++] public: int Send(unsigned char __gc[], SocketFlags);
[JScript] public function Send(Byte[], SocketFlags) : int;
Sends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Send(Byte(), Integer, SocketFlags) As Integer
[C#] public int Send(byte[], int, SocketFlags);
[C++] public: int Send(unsigned char __gc[], int, SocketFlags);
[JScript] public function Send(Byte[], int, SocketFlags) : int;
Sends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Send(Byte(), Integer, Integer, SocketFlags) As Integer
[C#] public int Send(byte[], int, int, SocketFlags);
[C++] public: int Send(unsigned char __gc[], int, int, SocketFlags);
[JScript] public function Send(Byte[], int, int, SocketFlags) : int;
Example
[Visual Basic, C#, C++] The following example specifies the data buffer, an offset, a size, and SocketFlags for sending data to a connected Socket.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Send. For other examples that might be available, see the individual overload topics.
[Visual Basic] ' Displays sending with a connected socket ' using the overload that takes a buffer, offset, message size, and socket flags. Public Shared Function SendReceiveTest4(server As Socket) As Integer Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test") Dim bytes(255) As Byte Try ' Blocks until send returns. Dim i As Integer = server.Send(msg, 0, msg.Length, SocketFlags.None) Console.WriteLine("Sent {0} bytes.", i) ' Get reply from the server. server.Receive(bytes, 0, server.Available, SocketFlags.None) Console.WriteLine(Encoding.UTF8.GetString(bytes)) Catch e As SocketException Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode) Return e.ErrorCode End Try Return 0 End Function 'SendReceiveTest4 [C#] // Displays sending with a connected socket // using the overload that takes a buffer, offset, message size, and socket flags. public static int SendReceiveTest4(Socket server) { byte[] msg = Encoding.UTF8.GetBytes("This is a test"); byte[] bytes = new byte[256]; try { // Blocks until send returns. int i = server.Send(msg, 0, msg.Length, SocketFlags.None); Console.WriteLine("Sent {0} bytes.", i); // Get reply from the server. server.Receive(bytes, 0, server.Available, SocketFlags.None); Console.WriteLine(Encoding.UTF8.GetString(bytes)); } catch (SocketException e) { Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode); return (e.ErrorCode); } return 0; } [C++] // Displays sending with a connected socket // using the overload that takes a buffer, offset, message size, and socket flags. int SendReceiveTest4(Socket* server) { Byte msg[]= Encoding::UTF8->GetBytes(S"This is a test"); Byte bytes[] = new Byte[256]; try { // Blocks until send returns. int i = server->Send(msg, 0, msg.Length, SocketFlags::None); Console::WriteLine(S"Sent {0} bytes.", i.ToString()); // Get reply from the server. server->Receive(bytes, 0, server->Available, SocketFlags::None); Console::WriteLine(Encoding::UTF8->GetString(bytes)); } catch (SocketException* e) { Console::WriteLine("{0} Error code: {1}.", e->Message, e->ErrorCode.ToString()); return ( e->ErrorCode); } return 0; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
Socket Class | Socket Members | System.Net.Sockets Namespace