Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); byte[] data = Encoding.ASCII.GetBytes("Hello, world!"); socket.BeginSend(data, 0, data.Length, SocketFlags.None, SendCallback, null); static void SendCallback(IAsyncResult result) { // Handle completion of the send operation here }
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); FileStream fileStream = File.OpenRead("myfile.bin"); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { socket.BeginSend(buffer, 0, bytesRead, SocketFlags.None, SendCallback, null); } static void SendCallback(IAsyncResult result) { // Handle completion of the send operation here }In this example, we open a file and read it into a buffer in chunks of 4096 bytes. We then use Socket.BeginSend to send each chunk through the socket until we've sent the entire file. Again, we use a callback function to handle the completion of the operation. Package library: System.Net.Sockets