Methods
Methods
| Name | Description |
|---|---|
|
bool CanRead
|
Gets a value indicating whether the current stream supports reading. |
|
bool CanSeek
|
Gets a value indicating whether the current stream supports seeking. |
|
bool CanTimeout
|
Gets a value indicating whether the current stream supports setting timeouts. |
|
bool CanWrite
|
Gets a value indicating whether the current stream supports writing. |
|
void Close()
|
Closes the current stream and releases any resources (such as socket connections) associated with the current stream. |
|
void Dispose()
|
Releases all resources used by the NetworkStream. |
|
IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
|
Begins a deferred copy of a sequence of bytes from the current stream to the specified buffer with the specified offset and count. |
|
IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
|
Begins a deferred copy of a sequence of bytes from the current stream to the specified buffer with the specified offset and count. |
|
int EndRead(IAsyncResult asyncResult)
|
Waits for the pending asynchronous read to complete. |
|
void EndWrite(IAsyncResult asyncResult)
|
End a pending asynchronous write. |
|
void Flush()
|
Clears all buffers for the current stream and causes any buffered data to be written to the underlying device. |
|
long Seek(long offset, SeekOrigin origin)
|
Sets the position within the current stream to the specified value. |
|
void SetLength(long value)
|
Sets the length of the current stream. |
|
int Read(byte[] buffer, int offset, int count)
|
Reads a sequence of bytes from the current stream into the specified byte array, starting at the specified offset, and ending at offset + count - 1. |
|
int ReadByte()
|
Reads one byte from the current stream and advances the position of the stream by one byte, or returns -1 if the end of the stream is reached. |
|
void Write(byte[] buffer, int offset, int count)
|
Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. |
|
void WriteByte(byte value)
|
Writes a byte to the current stream and advances the position of the stream by one byte. |
|
void WriteTimeout(int value)
|
Gets or sets a value, in milliseconds, that determines how long the server waits for a send operation to complete. |
|
int ReadTimeout
|
Gets or sets a value, in milliseconds, that determines how long the server waits for a read operation to complete. |
Remarks
The NetworkStream class provides the underlying stream used by the TcpClient and UdpClient classes. You can also create a NetworkStream directly from a Socket.
Use the NetworkStream to send and receive data over a network. You can read from and write to the stream simultaneously, as NetworkStream supports duplex communication.
The CanRead, CanWrite, and CanSeek properties indicate whether the stream supports reading, writing, and seeking, respectively. The NetworkStream does not support seeking.
The ReadTimeout and WriteTimeout properties determine the time-out period for read and write operations. If a time-out occurs, a SocketException is thrown.
Example
using System;
using System.Net.Sockets;
using System.Text;
public class NetworkStreamExample
{
public static void Main(string[] args)
{
// This is a simplified example and requires a server to connect to.
// In a real application, you would establish a connection first.
try
{
// Assume 'clientSocket' is an already connected Socket
// For demonstration purposes, let's create a dummy socket (this won't actually connect)
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// In a real scenario, you'd have a connected socket here.
// For example:
// TcpClient client = new TcpClient("localhost", 11000);
// Socket clientSocket = client.Client;
using (NetworkStream stream = new NetworkStream(clientSocket, true))
{
// Send data
string messageToSend = "Hello, Server!";
byte[] messageBytes = Encoding.ASCII.GetBytes(messageToSend);
stream.Write(messageBytes, 0, messageBytes.Length);
Console.WriteLine($"Sent: {messageToSend}");
// Receive data
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string receivedMessage = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine($"Received: {receivedMessage}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}