using System.IO; // Open a binary file for reading FileStream fileStream = new FileStream("binaryfile.bin", FileMode.Open); // Read the first 100 bytes from the file into a byte array byte[] buffer = new byte[100]; int bytesRead = fileStream.Read(buffer, 0, 100); // Close the file fileStream.Close();
using System.IO; using System.Net.Sockets; // Create a TcpClient and connect to a remote host TcpClient client = new TcpClient(); client.Connect("remotehost.example.com", 1234); // Get the network stream from the client NetworkStream networkStream = client.GetStream(); // Read the first 100 bytes from the network stream into a byte array byte[] buffer = new byte[100]; int bytesRead = networkStream.Read(buffer, 0, 100); // Disconnect from the remote host client.Close();In this example, we create a TcpClient object and connect to a remote host. We then get the network stream from the client and use the Read method to read the first 100 bytes of data from the stream into a byte array. Finally, we close the TcpClient object to disconnect from the remote host. Package/Library: System.dll