NetworkStream Class
Provides a stream for sending and receiving data over a network stream. This class is used to send and receive data over a network connection.
Syntax
public sealed class NetworkStream : System.IO.Stream
Inheritance
System.Object
System.IO.Stream
System.Net.NetworkStream
Remarks
Use the NetworkStream class to send and receive data over a network connection. You typically obtain a NetworkStream object from a Socket or an TcpClient object. The stream can be read from and written to, and it supports blocking and non-blocking operations.
When you are finished with the NetworkStream, you should close it to release system resources. You can do this by calling the Close method or the Dispose method.
Important Security Note: Do not use NetworkStream to transmit sensitive information over an unsecured network. For secure communication, use SslStream.
Constructors
- NetworkStream(System.Net.Sockets.Socket socket)
- Initializes a new instance of the
NetworkStreamclass for the specified Socket. - NetworkStream(System.Net.Sockets.Socket socket, bool ownsSocket)
- Initializes a new instance of the
NetworkStreamclass for the specified Socket and a value that indicates whether the stream owns the socket. - NetworkStream(System.Net.Sockets.Socket socket, System.IO.FileAccess access)
- Initializes a new instance of the
NetworkStreamclass for the specified Socket and FileAccess value. - NetworkStream(System.Net.Sockets.Socket socket, System.IO.FileAccess access, bool ownsSocket)
- Initializes a new instance of the
NetworkStreamclass for the specified Socket, FileAccess value, and a value that indicates whether the stream owns the socket.
Methods
The NetworkStream class inherits many methods from System.IO.Stream, including:
Read(...): Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.Write(...): Writes a sequence of bytes to the current stream and advances the position within the stream by the number of bytes written.Flush(): Clears all buffers for this stream and causes any buffered data to be written to the underlying device.Close(): Closes the current stream and releases any resources associated with the current stream.
Properties
The NetworkStream class inherits properties from System.IO.Stream, such as:
CanRead: Gets a value indicating whether the stream supports reading.CanWrite: Gets a value indicating whether the stream supports writing.CanSeek: Gets a value indicating whether the stream supports seeking. (Typically false for NetworkStream).
Example
The following example demonstrates how to create a simple client that connects to a server, sends a message, and receives a response using NetworkStream.
using System; using System.Net; using System.Net.Sockets; using System.Text; public class NetworkStreamClient { public static void Main(string[] args) { string serverIP = "127.0.0.1"; int serverPort = 13000; string messageToSend = "Hello, Server!"; try { // Create a TCP client and connect to the server TcpClient client = new TcpClient(serverIP, serverPort); Console.WriteLine("Connected to server."); // Get the NetworkStream to send and receive data NetworkStream stream = client.GetStream(); // Convert message to byte array and send byte[] dataToSend = Encoding.ASCII.GetBytes(messageToSend); stream.Write(dataToSend, 0, dataToSend.Length); Console.WriteLine("Sent: " + messageToSend); // Receive response from server byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received: " + responseData); // Close the stream and client stream.Close(); client.Close(); Console.WriteLine("Connection closed."); } catch (SocketException socketEx) { Console.WriteLine("Socket exception: {0}", socketEx); } catch (Exception ex) { Console.WriteLine("General exception: {0}", ex); } } }
This example assumes a server is running and listening on the specified IP address and port. The server should be designed to echo back any data it receives.