public Read ( byte buffer, int offset, int size ) : int | ||
buffer | byte | |
offset | int | |
size | int | |
return | int |
using System; using System.Net.Sockets; class Example { static void Main(string[] args) { TcpClient client = new TcpClient("localhost", 8080); NetworkStream stream = client.GetStream(); byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string message = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received message: {0}", message); stream.Close(); client.Close(); } }
using System; using System.Net; using System.Net.Sockets; class Example { static void Main(string[] args) { IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8080); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(remoteEP); byte[] buffer = new byte[1024]; int bytesRead = socket.Receive(buffer); string message = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received message: {0}", message); socket.Shutdown(SocketShutdown.Both); socket.Close(); } }This example creates a socket and connects to a server running on the localhost at port 8080. It then receives data from the socket using the Receive method and converts the bytes to a string using ASCII encoding. Package library: This class is part of the .NET Framework Class Library.