Ass 1 Network Prog
Ass 1 Network Prog
ASSIGNMENT
COVER
REGION: Mash west SEMESTER: 1 YEAR: 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.
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
1) Define the following
i) UDP – User Datagram Protocol is a communication protocol used across the internet for
especially time-sensitive transmission such as video playback or DNS lookups.
ii) TCP – Transmission Control Protocol is a networking protocol that allows two
computers to communicate.
3) Write code for client application that is supposed to hack into a remote server listening for
messages on port 2021 and download a file called Secret.txt. Assume that the file is
streamed by the server upon connection.
import Java.net.Socket;
import Java.io.*;
import Java.net.*;
class LojohnesSocket{
public static void main(String[] args){
try{
Socket s = new Socket(“server”,2021);
DataInputStream ts = new DataInputStream(s.getInputStream());
boolean I = true;
while(i){
string str = ts.readline(“Secret.txt”);
system.out.println(str);
}
}
catch(IOException e){
system.out.println(“Error occurred” +e);
}
}
QUESTION 2
A) What do you understand by Java Sockets
A Java socket is an end point of a two way communication link between two programs on the
network. A socket is bound to a port number and Java socket is a communication of IP address and
a portwasnumber.
This study source downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
B) What package would you want to import into a java application to make use of Java sockets
in an application.
Java.net.*;
Java.io.*;
C) Write code for a multithreaded server that would echo system time to a telnet client on port
23.
import java.io.BufferedReader;
public class MultithreadedServer{
public static void main(String[] args){
ServerSocket ss = null;
try{
ss = new ServerSocket(23);
ss.setReuseAddress(true);
while(true){
Socket client = ss.accept();
System.out.println(“New client connected” + client.getInetAddress().getHostAddress());
clientHandler clientsock = new clientHandler(client);
new Thread(clientsock).start();
}
}catch (IOException e){
e.printStackTrace();
}finally{
if(ss != null){
try{
ss.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
}
private static class clientHandler implements Runnable{
private final socket clientSocket;
public clientHandler(Socket socket){
this.clientSocket
This study source was downloaded by=100000800936633
socket; from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
}
public void run(){
printWriter out = null;
BufferedReader in = null;
try{
Out = new printWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
string line;
while((line = in.readline()) != null){
system.out.println(“sent from the client: %s \n”, line);
out.println(line);
}
}catch (IOException e){
e.printStackTrace();
} finally {
try{
if(out != null){
out.close();
}
if(in != null){
in.close();
clientSocket.close();
} catch (IOException e){
e.printStackTrace();
}
}
QUESTION 3
1) Give two ways in which threads can be implemented in Java.
Extends Thread class: is to create a thread by a new class that extends Thread class and
create an instance of that class. The extending class must override run() method which is the
entry point of new thread.
public class MyThread extends Thread{
public void run(){
system.out.println(“Thread started…..”)
}
public static void main(String[] args){
MyThread t = new MyThread();
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
t.start();
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
}
}
Implementing the Runnable Interface: this is the easiest way to create thread. After
implementing runnable interface, the class needs to implement the run() method which is
public void run()
class MyThread implements Runnable{
public void run(){
system.out.println(“Thread started ……”);
}
Public static void main(String[] args){
Mythread t = new MyThread();
Thread m = new Thread(t);
m.start();
}
}
4) Create a serializable class called BankAccount with the following fields accountName,
accountNumber, accountType, and balance. You should provide the getters and setters for
the varriables you define and a method called saveToFile() that persists the calling object to
a file called bankaccounts.
import java.io.Serializable;
public class BankAccount implements Serializable{
private string accountName;
private int accountNumber;
private string accountType;
private double balance;
public string ToString(){
return “BankAccount{account name =” +accountName + “, account number =” +accountNumber +
“,account type =” + accountType + “, balance =” + balance +”}”;
}
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
public string getName(){
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
return accountName;
}
public void setName(String accountName){
this.accountName = accountName;
}
public int getNumber(){
return accountNumber;
}
public void setNumber(int accountNumber){
this.accountNumber = accountNumber;
}
public string getType(){
return accountType;
}
public void setType(string accountType){
this.accountType = accountType;
}
public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
}
QUESTION 4
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,
evelop 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.
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
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.
QUESTION 5
Write a programme that uses the URL class and the OpenStream() method to read and write
interactively the contents of a website. Insert line comments on each line to explain why you put
the code.
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:43:17 GMT -05:00
https://www.coursehero.com/file/133753016/ass-1-network-progdoc/
Powered by TCPDF (www.tcpdf.org)