Network Programming Questions Comments
Network Programming Questions Comments
Q0: Which namespaces must be imported to use the classes used in the above
code?
using System;
// Base namespace
using System.Net;
// Provides simple network functions
using System.Net.Sockets;
// Enables socket programming
using System.IO;
// Provides I/O classes like Stream, BinaryWriter
using System.Text;
// Provides encoding classes like ASCIIEncoding
using System.Threading;
// Enables multithreading support
Q3: Write code to send an image from client to server using NetworkStream.
try
// Begin try block to catch exceptions
{
openFileDialog1.ShowDialog();
// Open file dialog to select image
string mypic_path = openFileDialog1.FileName;
// Get selected file path
pictureBox1.Image = Image.FromFile(mypic_path);
// Load image into PictureBox
MemoryStream ms = new MemoryStream();
// Create MemoryStream to hold image data
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
// Save image into MemoryStream in its raw format
byte[] arrImage = ms.GetBuffer();
// Get byte array from MemoryStream
ms.Close();
// Close MemoryStream
TcpClient myclient = new TcpClient(txt_host.Text, 5020);
// Connect to server using host text and port 5020
NetworkStream myns = myclient.GetStream();
// Obtain network stream from TCP client
BinaryWriter mysw = new BinaryWriter(myns);
// Create BinaryWriter to write binary data to network stream
mysw.Write(arrImage);
// Send image byte array over network
mysw.Close();
// Close BinaryWriter
myns.Close();
// Close network stream
myclient.Close();
// Close TCP connection
}
Q4: Write server-side code to receive the image sent by the client and display it
in a PictureBox.
TcpListener mytcpl = new TcpListener(5000);
// Initialize TCP listener on port 5000
mysocket = mytcpl.AcceptSocket();
// Accept incoming connection as a socket
myns = new NetworkStream(mysocket);
// Wrap socket in a NetworkStream
pictureBox1.Image = Image.FromStream(myns);
// Load image directly from network stream into PictureBox
mytcpl.Stop();
// Stop listening (close TCP listener)
if(mysocket.Connected == true)
// Check if socket is still connected
{
while(true)
// Loop to continuously receive images
{
Image_Receiver();
// Recursively call Image_Receiver to handle next image
}
Q5: Write the Form1_Closing event to stop the TCP listener and exit the
application.
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e) // Event handler for form closing
{
try
// Begin try block to catch exceptions
{
mytcpl.Stop();
// Stop the TCP listener
Application.Exit();
// Exit the application
}
Q6: Write code to save the received image from PictureBox to a JPEG file.
try
// Begin try block to catch exceptions
{
catch(Exception)
// Catch any exception (silently ignore)
{
}
Q7: Write code to use a Thread to receive the image.
using System.Threading;
// Import threading namespace
Thread myth;
// Declare a Thread variable
myth = new Thread(new System.Threading.ThreadStart(Image_Receiver));
// Initialize thread to run Image_Receiver method
myth.Start();
// Start the thread
try
// Begin try block
{
mytcpl.Stop();
// Stop the TCP listener
myth.Abort();
// Abort the image-receiving thread
}