0% found this document useful (0 votes)
2 views3 pages

Network Programming Code Questions

The document contains a series of network programming questions and answers focused on TCP and UDP communication in C#. It includes code snippets for sending and receiving messages, images, and handling TCP listeners and threads. Additionally, it provides examples of saving images and using multicast for UDP communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Network Programming Code Questions

The document contains a series of network programming questions and answers focused on TCP and UDP communication in C#. It includes code snippets for sending and receiving messages, images, and handling TCP listeners and threads. Additionally, it provides examples of saving images and using multicast for UDP communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Network Programming Questions and Answers

Q0: Which namespaces must be imported to use the classes used in the above code?
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;

Q1: Write code to send a message using TCP in C#.


String str = Console.ReadLine();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);

TcpClient tcpclnt = new TcpClient("192.168.0.2", 8001);


NetworkStream mynetsream = tcpclnt.GetStream();
StreamWriter myswrite = new StreamWriter(mynetsream);
myswrite.WriteLine("Your Message");
mynetsream.Close();
tcpclnt.Close();

Q2: Write code to receive a message on the server using TCP.


TcpListener myList = new TcpListener(5020);
myList.Start();
Socket s = myList.AcceptSocket();
NetworkStream myns = new NetworkStream(s);
StreamReader mysr = new StreamReader(myns);
Console.Write(mysr.ReadLine());
s.Close();

Q3: Write code to send an image from client to server using NetworkStream.
try
{
openFileDialog1.ShowDialog();
string mypic_path = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(mypic_path);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] arrImage = ms.GetBuffer();
ms.Close();
TcpClient myclient = new TcpClient(txt_host.Text, 5020);
NetworkStream myns = myclient.GetStream();
BinaryWriter mysw = new BinaryWriter(myns);
mysw.Write(arrImage);
mysw.Close();
myns.Close();
myclient.Close();
}
catch(Exception ex){ MessageBox.Show(ex.Message); }

Q4: Write server-side code to receive the image sent by the client and display it in a
PictureBox.
TcpListener mytcpl = new TcpListener(5000);
mysocket = mytcpl.AcceptSocket();
myns = new NetworkStream(mysocket);
pictureBox1.Image = Image.FromStream(myns);
mytcpl.Stop();
if(mysocket.Connected == true)
{
while(true)
{
Image_Receiver();
}
}

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)
{
try
{
mytcpl.Stop();
Application.Exit();
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}

Q6: Write code to save the received image from PictureBox to a JPEG file.
try
{
saveFileDialog1.Filter = "JPEG Image (*.jpg)|*.jpg";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string mypic_path = saveFileDialog1.FileName;
pictureBox1.Image.Save(mypic_path);
}
}
catch(Exception)
{
}

Q7: Write code to use a Thread to receive the image.


using System.Threading;

Thread myth;
myth = new Thread(new System.Threading.ThreadStart(Image_Receiver));
myth.Start();

Q8: Write updated Form1_Closing event code to stop the thread.


private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
mytcpl.Stop();
myth.Abort();
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}

Q9: Write code to send a message using UDP Multicast.


Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 5020);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SendTo(data, iep);
server.Close();

Q10: Write code to receive a UDP Multicast message.


UdpClient sock = new UdpClient(5020);
sock.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
byte[] data = sock.Receive(ref iep);
string stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString());
sock.Close();

You might also like