to another MemoryStream
object and then display it in another PictureBox
control, like this:
//---read the data in buffer and write to ms2---
MemoryStream ms2 = new MemoryStream();
ms2.Write(buffer, 0, bytesRead);
//---load it in another PictureBox control---
pictureBox2.Image = new Bitmap(ms2);
NetworkStream Class
The NetworkStream
class provides methods for sending and receiving data over Stream
sockets in blocking mode. Using the NetworkStream
class is more restrictive than using most other Stream
implementations. For example, the CanSeek()
properties of the NetworkStream
class are not supported and always return false. Similarly, the Length()
and Position()
properties throw NotSupportedException
. It is not possible to perform a Seek()
operation, and the SetLength()
method also throws NotSupportedException
.
Despite these limitations, the NetworkStream
class has made network programming very easy and encapsulates much of the complexity of socket programming. Developers who are familiar with streams programming can use the NetworkStream
class with ease.
This section leads you through creating a pair of socket applications to illustrate how the NetworkStream
class works. The server will listen for incoming TCP clients and send back to the client whatever it receives.
The following code is for the server application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server {
class Program {
const int PORT_NO = 5000;
const string SERVER_IP = '127.0.0.1';
static void Main(string[] args) {
//---listen at the specified IP and port no.---
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
Console.WriteLine('Listening...');
listener.Start();
//---incoming client connected---
TcpClient client = listener.AcceptTcpClient();
//---get the incoming data through a network stream---
NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize;
//---read incoming stream---
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
//---convert the data received into a string---
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine('Received : ' + dataReceived);
//---write back the text to the client---
Console.WriteLine('Sending back : ' + dataReceived);
nwStream.Write(buffer, 0, bytesRead);
client.Close();
listener.Stop();
Console.ReadLine();
}
}
}
Basically, you use the TcpListener
class to listen for an incoming TCP connection. Once a connection is made, you use a NetworkStream
object to read data from the client, using the Read()
method as well as write data to the client by using the Write()
method.
For the client, you use the TcpClient
class to connect to the server using TCP and, as with the server, you use the NetworkStream
object to write and read data to and from the client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client {
class Program {
const int PORT_NO = 5000;
const string SERVER_IP = '127.0.0.1';
static void Main(string[] args) {
//---data to send to the server---
string textToSend = DateTime.Now.ToString();
//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//---send the text---
Console.WriteLine('Sending : ' + textToSend);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine('Received : ' +
Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
Console.ReadLine();
client.Close();
}
}