byte[] buffer = new byte[1024]; Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), clientSocket); private void OnReceive(IAsyncResult result) { Socket clientSocket = (Socket)result.AsyncState; int bytesRead = clientSocket.EndReceive(result); // process data received }
WebClient webClient = new WebClient(); webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnDownloadComplete); webClient.DownloadStringAsync(new Uri("http://example.com")); private void OnDownloadComplete(object sender, DownloadStringCompletedEventArgs e) { if (e.Error == null) { string result = e.Result; // process downloaded data } }In this example, we use the WebClient class to download a string asynchronously. When the download is complete, the OnDownloadComplete method is called and the downloaded data is processed. Package: System.Net