You are on page 1of 5

ZQMS-ARC-REC-002

ASSIGNMENT
COVER
REGION: Mash west SEMESTER: 1 YEAR: 2021

PROGRAMME: Bachelor of Software Engineering BSEH INTAK E: 6

FULL NAME OF STUDEN T: Lovemore Johannes PIN: P1884195L

EMAIL ADDRESS: lojohnes2012@gmail.com

CONTACT TELEPHONE/CELL: 0777993975 ID. NO.: 58-275814A23

COURSE NAME: NETWORK PROGRAMMING COURSE CODE: BITH261

ASSIGNMENT NO. e.g. 1 or 2: 2 STUDENT’S SIGNATURE

DUE DATE: 10 FEBRUARY 2021 SUBMISSION DATE: 30 JANUARY


2021

ASSIGNMENT TITLE:

Instructions
Marks will be awarded for good presentation and thoroughness in your approach.
NO marks will be awarded for the entire assignment if any part of it is found to be copied directly
from printed materials or from another student.
Complete this cover and attach it to your assignment. Insert your scanned signature.

Student declaration
I declare that:
 I understand what is meant by plagiarism
 The implications of plagiarism have been explained to me by the institution
 This assignment is all my own work and I have acknowledged any use of the published or
unpublished works of other people.

MARK ER’S COMMEN TS:

OVERALL MARK: MARK ER’S NAME:


This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
MARK ER’S SIGNATURE: DATE:
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
QUESTION 1
A) Explain the concept of a layered task with respect to the TCP model.
We always use the concept of layers in our daily life. As an example, let us consider two friends
who communicate through postal email. The process of sending a letter to a friend would be
complex if there were no services available from the post office.
SENDER RECEIVER
The letter is written, put in an Higher layers The letter is picked up,
envelope and dropped in a removed from the envelop and
mailbox. read
↑↑
↓↓
The letter is carried from the Middle layers The letter is carried from the
mailbox to a post office. post office to the mailbox
↑↑
↓↓
The letter is delivered to a Lower layers The letter is delivered from the
carrier by the post office carrier to the post office
→→→→→→→→→→→→→
Parcel is carried from source to destination

B) What is an UnknownHostException.
This is a subclass of IOException, so it is a checked exception. It emerges when you are trying to
connect to a remote host using its host name, but the IP address of that Host cannot be resolved.

C) What is a stream?
A stream is a sequence of objects that supports various methods which can be pipelined to produce
the desired results.

D) Explain in your own words, your understanding of persistence objects and serialized
objects.
When we implement persistence objects, we could simply require all classes of persistence objects
to provide a member function that inserts each member variable into a given file stream ( this
process is done by serialized objects).
class persistent
{
public void serialized(fstream);
}
Persistent objects are class used by an entity class. They do not have indices but instead are stored
or retrieved when an entity class makes direct use of them.
Serialized objects they convert state of objects into a byte stream in a way that the byte stream can
be reverted back into a copy of the objects.

E) Why threads are important in network programming.


 A thread is a flow of execution through the process code
 Threads provide a way to improve application performance through parallelism
 It also improves the performance of operating system
This study sourcewas Each thread
downloaded represents afrom
by 100000800936633 separate flow of
CourseHero.com control 04:40:27 GMT -05:00
on 04-12-2023
 All threads within a process share same global memory
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
F) What do you understand by synchronisation
 Synchronization in java is the capability to control the access of multiple threads to any
shared resource.
 Synchronisation is used mainly to prevent thread interference and to prevent consistency
problem.
 We have two types of synchronisation which are process synchronization and thread
synchronization.
 We also have two types of thread synchronization which are mutual exclusion and inter-
thread communication.
 Synchronisation is built around an entity known as the lock or monitor. Every object has a
lock associated with it. By convention a thread that needs consistent access to an object’s
field has to acquire the object’s lock before accessing them, and then release the lock when
it’s done with them.

G) Build a SimpleSleepThread class by implementing Runnable. Create an object Thread


and start the thread. In run method, let the thread sleeps for 10000ms. Display strings
before and after the thread sleeps. Is there any noticeable time difference between the
two strings displayed? Why and why not.
class SimpleSleepThread implements Runnable{
public void run(){
system.out.println(“Thread is now sleeping”);
for(I = 1; i<=5; i++){
try{
Thread.sleep(10000);
}
Catch (interrupted Exception e) system.out.println(e);
}
system.out.println(“Thread is running….”, +i);
}
}
public static void main( string[] args){
ThreadSleep ts = new ThreadSleep();
Thread s = new Thread(ts);
s.start();
system.out.println(“Thread ends…..”);
}
}

QUESTION 2
Write a GUI programme called MyBrowserExplorer
Your interface should contain at least one text control to enter the URL to visit and a GO
button.
You should also provide a large text area control that displays the contents of the url that is
visited.
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
Your code should make use of the URL class and the openstream() method to read
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
interactively the contents of a website. Insert the comments on each line to explain why you
put the code.

//MyBrowserExplorer.java
//importing java parkages
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//declaring browser class
public class MyBrowserExplorer extends Frame Implements ActionListener
{
//declaring variables
private TextField txtInput;
private TextArea contents;
private Button btnGo;
private Button exit;
//the main
public static void main(String[] args)
{
MyBrowserExplorer m = new MyBrowserExplorer();
m.makeGUI();
m.setSize(550, 400);
m.setVisible(true);
}
public void makeGUI()
{
//creating a layout
setLayout(new FlowLayout());
//creating and adding a text input that we can enter url address to the frame
txtInput = new TextField(50);
add(txtInput);
//creating and adding a button Go to the frame
btnGo = new Button(“Go”);
add(btnGo);
//creating and adding button Exit
exit = new Button(“Exit”);
add(exit);
exit.addActionListener(this);
//creatinjg a text area to display contents when the button Go is pressed
contents = new TextArea(“ “, 20, 60, TextArea.SCROLLBARS-VERTICAL-ONLY);
add(contents);
btnGo.addActionListener(this);
}
//action performed by the button
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==exit);
System.exit(0);
//getting the input from user into the text field
String line;
String location = txtInput.getText();
try{
// reading and writing the contents of URL entered in the text field
URL url = new URL(location);
bufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
while((line =input.readline())!=null)
{
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
contents.append(line);
contents.append(“/n”);
}
input.close();
}
catch (MalformedURLException e)
{
contents.setText(“Invalid URL format”);
}
catch (IOException io)
{
contents.setText(io.toString());
}
}
}

This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00

https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
Powered by TCPDF (www.tcpdf.org)

You might also like