network programming lab

107
VISWAJYOTHI COLLEGE OF ENGINEERING & TECHNOLOGY VAZHAKULAM- MUVATTUPUZHA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LABORATORY MANUEL Network Programming Lab (Seventh Semester- B. Tech-CSE)

Upload: aswathy-mohan

Post on 22-Nov-2014

598 views

Category:

Documents


4 download

TRANSCRIPT

VISWAJYOTHI COLLEGE OF ENGINEERING &

TECHNOLOGY VAZHAKULAM- MUVATTUPUZHA

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LABORATORY MANUEL

Network Programming Lab(Seventh Semester- B. Tech-CSE)

Prepared By

Dept. of Computer Science & Engineering, VJCET

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

CONTENTS

Sl. No Chapter/Section Page No.I NETWORK PROGRAMMING LAB SYLLABUS 3II PROGRAMS1 Write a program to design a UDP Client - Server which implements Echo

Protocol 4

2 Write a program to create a client-server using TCP in which the client is accepting a message from the keyboard and send it to the server. 7

3Write a program to create a client-server using TCP in which both client and server are accepting message from the keyboard and send them to other.

10

4 Write a program using TCP/IP to implement three way handshake protocol 15

5 Write a program to broadcast data from server to multiple clients. 19

6Write a program to implement chat between two clients through a server 24

7Write a program to implement a Client-Server application by using RMI. The application is to reverse a string given by the client as a request to the server.

30

8 Write a program to implement a file server to transfer a file from server side to client side. 33

9Write a program to implement public chat from one client to all clients 43

10Write a program to implement an alarm clock using

A) Applet B) Application 49

11 Implement a frame application were we have one manager as the head of a

division of projects and we have two agents as project managers, also we have two sub agents for agents as project leaders. 53

III VIVA VOCE 60

IV REFERENCE 79

2

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

NETWORK PROGRAMMING LAB SYLLABUS

R708 0+0+3

Experiments using interprocess communication and Network communication, synchronisation &

IPC using semaphore, pipe & messages.

Programs for FTP and socket based chat.

Implementation of File Transfer - Communication through serial port - Communication through

TCP/IP port

Efficient error checking algorithms (Eg: CRC)

Remote Procedure Call, Remote Method Invocation.

Programs with HTML, DHTML, Applets, Java Script, Java, XML, Java Beans, JSP and EJB.

Any experiments according to the syllabus of RT604 Computer Networks, RT605 Network

Computing and RT705 Web Technologies may be substituted

3

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

PROGRAMS

Program No.1

QUESTION:

Write a program to design a UDP Client - Server which implements Echo Protocol

ALGORITHM:

Step 1: Start

Step 2: Client program

Step 2.1: A datagram socket is created

Step 2.2: A datagram packet which contains the data stored in a buffer of specified size is

send to the server by using the send function.

Step 2.3: Another datagram packet is created to receive the data send by the server over

the datagram socket using the receive function.

Step 2.4: Data received is printed on the client side.

Step 3: Server program

Step 3.1: First a datagram socket is created.

Step 3.2: A datagram packet is created to receive the data send by the client over the

datagram socket using the receive function

Step 3.3: A datagram packet which contains the data stored in a buffer of specified size is

send to the client by using the send function.

Step 4: Stop.

PROGRAM:

//CLIENT SIDE

import java.net.*;

import java.io.*;

public class DatagramReceive

{ static final int PORT = 6969;

4

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

public static void main(String args[])

throws Exception

{ String s2;

InetAddress ip = null;

try{ ip = InetAddress.getByName("localhost");

}

catch(UnknownHostException e)

{ System.out.println(e);

System.exit(1);

}

byte[] b2 = new byte[2048];

DatagramPacket p2 = new DatagramPacket(b2,b2.length);

DatagramSocket ss2 = new DatagramSocket(PORT);

ss2.receive(p2);

s2 = new String(b2,0,p2.getLength());

System.out.println("Receiving...\n"+ s2);

byte[]b1 = new byte[s2.length()];

b1 = s2.getBytes();

DatagramPacket p1 = new DatagramPacket(b1,s2.length(),ip,PORT);

DatagramSocket ss1 = null;

try{ ss1 = new DatagramSocket();

}

catch(SocketException e)

{ System.out.println(e);

System.exit(1);

}

try{ ss1.send(p1);

}

catch(IOException e)

{ System.out.println(e);

System.exit(1);

5

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

ss2.close();

}

}

//SERVER SIDE

import java.net.*;

import java.io.*;

import java.util.*;

public class DatagramSend

{ static int PORT = 6969;

public static void main(String args[])

throws Exception

{ InetAddress ip = null;

String s1;

int done=1;

try{ ip = InetAddress.getByName("localhost");

}

catch(UnknownHostException e)

{ System.out.println(e);

System.exit(1);

}

String s3 = " ";

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Bye to exit");

do{ s1 = br.readLine();

s3 = s3 + s1 + "\n";

if (s1.equals("Bye"))

done = 0;

}while(done!=0);

6

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

byte[]b1 = new byte[s3.length()];

b1 = s3.getBytes();

DatagramPacket p1 = new DatagramPacket(b1,s3.length(),ip,PORT);

DatagramSocket ss1 = null;

try{ ss1 = new DatagramSocket();

}

catch(SocketException e)

{ System.out.println(e);

System.exit(1);

}

try{ ss1.send(p1);

}

catch(IOException e)

{ System.out.println(e);

System.exit(1);

}

}

}

Program No.2

QUESTION:

Write a program to create a client-server using TCP in which the client is accepting a message

from the keyboard and send it to the server.

ALGORITHM:

1. Start

2. Client program

a) Create a client socket to communicate with the server.

b) Create an input stream to read from the socket’s input stream.

c) Create an output stream to write to the socket’s output stream.

7

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

d) Read data from the keyboard and send it to the server.

3. Server program

a) Create a server socket to accept data from the client.

b) Create an input stream to receive data from the client.

c) Print the received data at the server side.

4.Stop.

PROGRAM:

//CLIENT SIDE

import java.net.*;

import java.io.*;

import java.util.*;

class ClientTCP

{ public static void main(String args[])

throws Exception

{ Socket s1;

try

{ s1 = new Socket("localhost",6969);

BufferedReader br1 = new BufferedReader(new

InputStreamReader(s1.getInputStream()));

PrintWriter pw = new PrintWriter(s1.getOutputStream());

BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));

while(true)

{ String str2 = br1.readLine() ;

System.out.println("Receiving..."+str2);

if(str2.trim().equals("Bye"))

System.exit(1);

String str1 = br2.readLine();

pw.println(str1);

8

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

pw.flush();

System.out.println("Sending...."+str1);

if(str1.trim().equals("Bye"))

System.exit(1);

}

}

catch(Exception e)

{ System.out.println(e);

}

}

//SERVER SIDE

import java.net.*;

import java.io.*;

import java.util.*;

class ServerTCP

{

public static void main(String args[])

throws Exception

{ ServerSocket ss1;

Socket s1;

try

{ ss1 = new ServerSocket(6969);

s1 = ss1.accept();

int done=1;

BufferedReader br1 = new BufferedReader(new

InputStreamReader(s1.getInputStream()));

PrintWriter pw = new PrintWriter(s1.getOutputStream());

BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));

while(true)

{ if(done!=1)

9

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

{ String str2 = br1.readLine() ;

System.out.println("Receiving..."+str2);

if(str2.trim().equals("Bye"))

System.exit(1);

}

String str1 = br2.readLine();

pw.println(str1);

pw.flush();

System.out.println("Sending...."+str1);

if(str1.trim().equals("Bye"))

System.exit(1);

done = 2;

}

}

catch(Exception e)

{ System.out.println(e);

}

}

}

Program No.3

QUESTION:

Write a program to create a client-server using TCP in which both client and server are accepting

message from the keyboard and send them to other.

ALGORITHM

1. Start

2. Client program

a) Create a client socket to communicate with the server.

10

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

b) Create an input stream to read from the socket’s input stream.

c) Create an output stream to write to the socket’s output stream.

d) Read data from the keyboard and send it to the server.

3. Server program

a) Create a server socket to accept data from the client.

b) Create an input stream to read data from the client.

c) Create an output stream to write to the socket’s output stream.

d) Read data from keyboard and send it to client.

4. Stop.

PROGRAM:

//CLIENT SIDE

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class ClientTCP2 extends Frame implements ActionListener

{ TextArea txt1,txt2;

Button btn;

BufferedReader br;

PrintWriter pw;

String msg;

public ClientTCP2()

{ setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

add(txt1);

add(btn);

add(txt2);

11

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

btn.addActionListener(this);

}

public void setConnection()

{ Socket s;

try

{ txt1.setText(" ");

s = new Socket("localhost",6969);

br = new BufferedReader(new InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

while(true)

{ msg = br.readLine();

txt2.append("Server : "+msg+"\n");

if(msg.trim().equals("Quit"))

{ System.exit(1);

s.close();

}

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public void actionPerformed(ActionEvent ae)

{ if(ae.getSource()==btn)

{ msg = txt1.getText();

pw.println(msg);

pw.flush();

if(msg.trim().equals("Quit"))

System.exit(1);

txt1.setText(" ");

12

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

}

public static void main(String args[]) throws Exception

{ ClientTCP2 obj = new ClientTCP2();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

//SERVER SIDE

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class ServerTCP2 extends Frame implements ActionListener

{ TextArea txt1,txt2;

Button btn;

BufferedReader br;

PrintWriter pw;

String msg;

public ServerTCP2()

{ setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

add(txt1);

add(btn);

add(txt2);

btn.addActionListener(this);

}

13

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

public void setConnection()

{ ServerSocket ss;

Socket s;

try

{ ss = new ServerSocket(6969);

txt1.setText(" ");

while(true)

{ s = ss.accept();

txt2.setText("Connected...");

br = new BufferedReader(new InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

while(true)

{ msg = br.readLine();

txt2.append("Client : "+msg+"\n");

if(msg.trim().equals("Quit"))

{ System.exit(1);

s.close();

ss.close();

}

}

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public void actionPerformed(ActionEvent ae)

{ if(ae.getSource()==btn)

{ msg = txt1.getText();

pw.println(msg);

14

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

pw.flush();

if(msg.trim().equals("Quit"))

System.exit(1);

txt1.setText(" ");

}

}

public static void main(String args[]) throws Exception

{ ServerTCP2 obj = new ServerTCP2();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

Program No.4

QUESTION:

Write a program using TCP/IP to implement three way handshake protocol

ALGORITHM

Step 1: Start

Step 2: Client program

Step 2.1: Create a client socket to communicate with the server.

Step 2.2: Create an input stream to read from the socket’s input stream.

Step 2.3: Create an output stream to write to the socket’s output stream.

Step 2.4: Read data from the keyboard and send it to the server.

Step 3: Server program

Step 3.1: Create a server socket to accept data from the client.

Step 3.2: Create an input stream to read data from the client.

Step 3.3: Create an output stream to write to the socket’s output stream.

Step 3.4: Read data from keyboard and send it to client.

15

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Step 4: Stop.

PROGRAM:

//CLIENT SIDE:

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class HandshakeC extends Frame implements ActionListener

{ TextArea txt1,txt2;

Button btn;

BufferedReader br;

PrintWriter pw;

String msg;

int first ,serverready;

public HandshakeC()

{ setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

first = 1;

serverready = 0;

add(txt1);

add(btn);

add(txt2);

btn.addActionListener(this);

}

//SERVER SIDE:

import java.net.*;

import java.io.*;

16

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class HandshakeS extends Frame implements ActionListener

{ TextArea txt1,txt2;

Button btn;

BufferedReader br;

PrintWriter pw;

String msg;

int clientready;

public HandshakeS()

{ setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

clientready = 0;

add(txt1);

add(btn);

add(txt2);

btn.addActionListener(this);

}

public void setConnection()

{ ServerSocket ss;

Socket s;

try

{ ss = new ServerSocket(6969);

while(true)

{ s = ss.accept();

br = new BufferedReader(new InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

msg = br.readLine();

17

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

if(msg.trim().equals("CR"))

{

txt2.append("Connected...\n");

txt2.append("Client :"+msg+"\n");

pw.println("CA");

pw.flush();

txt2.append("Server: CA\n");

msg = br.readLine();

if(!msg.trim().equals("ACK"))

{ System.exit(1);

s.close();

ss.close();

}

txt2.append("Client : "+msg+"\n");

clientready = 1;

}

else

{ System.exit(1);

s.close();

ss.close();

}

while(true)

{

msg = br.readLine();

txt2.append("Client : "+msg+"\n");

if(msg.trim().equals("Quit"))

{

System.exit(1);

s.close();

ss.close();

18

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

}

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public void actionPerformed(ActionEvent ae)

{ if(ae.getSource()==btn)

{ msg = txt1.getText();

pw.println(msg);

pw.flush();

if(msg.trim().equals("Quit"))

System.exit(1);

txt1.setText(" ");

}

}

public static void main(String args[]) throws Exception

{ HandshakeS obj = new HandshakeS();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

Program No.5

QUESTION:

Write a program to broadcast data from server to multiple clients.

19

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

ALGORITHM:

1. Start

2. Client Program

a) Create a client socket.

b) Read the message sent from the server through the socket.

c) Output the data on the client side.

3. Server program

a) Create a Server socket and a simple socket array.

b) Create two threads, one to accept clients and another one to send data to

clients.

c) If the first thread is active, new clients can be connected to the server and the

number of clients connected can be found out.

d) If the second thread is active then data is read from the keyboard and send to

all clients through corresponding sockets.

4. Stop.

PROGRAM:

//CLIENT SIDE

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class BroadcastC extends Frame

{ TextArea txt1;

BufferedReader br;

String msg;

public BroadcastC()

{ setLayout(new GridLayout(1,1));

txt1 = new TextArea();

20

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

add(txt1);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ setVisible(false);

}

});

}

public void setConnection()

{ Socket s;

try

{ s = new Socket("localhost",6969);

br = new BufferedReader(new InputStreamReader(s.getInputStream()));

while(true)

{br = new BufferedReader(new InputStreamReader(s.getInputStream()));

msg = br.readLine();

txt1.append("Server : "+msg+"\n");

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public static void main(String args[]) throws Exception

{ BroadcastC obj = new BroadcastC();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

21

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

//SERVER SIDE

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class BroadcastS extends Frame implements ActionListener

{ TextArea txt1,txt2;

Button btn;

PrintWriter pw;

String msg;

int i,j;

Socket s[] = new Socket[10];

public BroadcastS()

{ setLayout(new GridLayout(3,1));

txt1 = new TextArea();

btn = new Button("Send");

txt2 = new TextArea();

add(txt1);

add(btn);

add(txt2);

btn.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{ setVisible(false);

}

});

}

public void setConnection()

{ ServerSocket ss;

22

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

try

{ ss = new ServerSocket(6969);

txt2.setText("Server Waiting \n");

while(true){

for(i=1;true;i++)

{ s[i] = ss.accept();

txt2.append("Client "+i+" connected\n");

}

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public void actionPerformed(ActionEvent ae)

{ try{ for(j=1;j<i;j++)

{ pw = new PrintWriter(s[j].getOutputStream(),true);

if(ae.getSource()==btn)

{ msg = txt1.getText();

pw.println(msg);

pw.flush();

}

}

}

catch(Exception e)

{ System.out.println(e);

System.exit(1);

}

}

public static void main(String args[])

23

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

throws Exception

{ BroadcastS obj = new BroadcastS();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

Program No.6

QUESTION:

Write a program to implement chat between two clients through a server

ALGORITHM:

1. Start

2. Client program

a) Create a simple socket.

b) Create two threads.

c) If first thread is active, data is read from the keyboard and written to the socket.

d) If the second thread is active, data is read from the output buffer of the socket and print it.

3. Server program

a) Create two simple sockets and a server socket.

24

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

b) Create two threads.

c) If the first thread is active, then read data from first socket and write it to the second socket.

d) If the second thread is active , then read data from second socket and write it to the first socket.

4. Stop.

PROGRAM:

//CLIENT SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ChatC extends Frame implements ActionListener

{

TextArea txt1,txt2;

Button btn;

PrintWriter pw;

BufferedReader br;

String msg;

Socket s;

public ChatC()

{

setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

25

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

btn = new Button("Send");

add(txt1);

add(btn);

add(txt2);

txt2.setText(" ");

txt1.setText(" ");

btn.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

setVisible(false);

}

});

}

public void setConnection()

{

try

{

s = new Socket("localhost",6969);

br = new BufferedReader(new

InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

while(true)

{

msg = br.readLine();

txt2.append("Receive : "+msg+"\n");

}

}

catch(Exception e){

26

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==btn)

{

msg = txt1.getText();

txt2.append("Send : "+msg+"\n");

pw.println(msg);

pw.flush();

}

}

public static void main(String args[])

throws Exception

{

ChatC obj = new ChatC();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

//SERVER SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ChatS implements Runnable

27

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

{

ServerSocket ss;

Socket s1,s2;

String msg1,msg2;

PrintWriter pw1,pw2;

Thread t1,t2;

BufferedReader br1,br2;

public ChatS()

{

try

{

ss = new ServerSocket(6969);

s1 = ss.accept();

System.out.println("S1\n");

s2 = ss.accept();

System.out.println("s2\n");

br1 = new BufferedReader(new

InputStreamReader(s1.getInputStream()));

br2 = new BufferedReader(new

InputStreamReader(s2.getInputStream()));

pw1 = new PrintWriter(s1.getOutputStream(),true);

pw2 = new PrintWriter(s2.getOutputStream(),true);

}

catch(Exception e)

{

System.out.println(e);

}

}

public void run()

{

try

28

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

{

System.out.println("C Thread : "+Thread.currentThread());

if(Thread.currentThread() == t1)

{

while(true)

{

msg1 = br1.readLine();

System.out.println("Client1 : "+msg1);

pw2.println(msg1);

pw2.flush();

}

}

else

{

while(true)

{

msg2 = br2.readLine();

System.out.println("Client2 : "+msg2);

pw1.println(msg2);

pw1.flush();

}

}

}

catch(Exception e)

{

System.out.println(e);

}

}

public static void main(String args[])

29

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

throws Exception

{

ChatS obj = new ChatS();

obj.t1 = new Thread(obj);

obj.t2 = new Thread(obj);

obj.t1.start();

obj.t2.start();

}

Program No.7

QUESTION:

Write a program to implement a Client-Server application by using RMI. The

application is to reverse a string given by the client as a request to the server.

ALGORITHM:

1. Start

2. This application uses four source files-Interface, implementation, server and

client programs.

2. Generate stubs and skeletons using a tool called the RMI compiler which is

invoked from the command line using ‘rmic’.

3. Start the RMI registry from the command line as shown here:

start rmiregistry

30

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

4. Start the server and client. Client can be started by passing address of the local

machine and the string to be reversed.

5. Stop.

DESCRIPTION:

RMI allows a java object that executes on one machine to invoke a method of a Java object that

executes on another machine. It allows to build distributed applications. All remote interfaces

must extend Remote interface which is a part of java.rmi. All remote methods can throw a

Remote Exception. All remote objects must extend UnicastRemoteObject, which provides

functionality that is needed to make objects available from remote machines.

PROGRAM:

//CLIENT SIDE

import java.rmi.*;

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ClientString

{

public static void main(String args[]) throws Exception

{

int len;

String url = "rmi://127.0.0.1/ServerString";

31

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

stringInf obj = (stringInf)Naming.lookup(url);

len = obj.stringLength(args[0]);

System.out.println("String length = "+len);

}

}

//SERVER SIDE

import java.rmi.*;

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ServerString

{

public static void main(String args[])

{

try

{

StringLengthImpl obj = new StringLengthImpl() ;

Naming.rebind("ServerString",obj);

}

catch(Exception e)

{

System.out.println(e);

}

}

}

32

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

//IMPLEMTATION

import java.rmi.*;

import java.rmi.server.*;

class StringLengthImpl extends UnicastRemoteObject implements stringInf

{

public StringLengthImpl() throws RemoteException{

}

public int stringLength(String str) throws RemoteException

{

return str.length();

}

}

//INTERFACE

import java.rmi.*;

interface stringInf extends Remote

{

public int stringLength(String s1) throws RemoteException;

}

Program No.8

QUESTION:

Write a program to implement a file server to transfer a file from server side to client

side.

ALGORITHM:

33

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

1. Start

2.Client program

a) Implement a client that can send two requests.

i) To get a file from the server.

ii) To put or send a file to the server.

b) The client sends the username and password.

c) After getting approval from the server ,the client either get file from the

server or send file to the server.

3. Server program

a) Implement a server socket that listens to a particular port number.

b) The server validates the username and password sent by the client by

checking it with the details stored in a file in the server.

c) Server reads the filename and sends the data stored in the file for the

‘get’ request.

d) It reads the data from the input stream and writes it to a file in the

server for the ‘put’ instruction.

e) Exit upon client’s request.

4. Stop.

PROGRAM:

//CLIENT SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

34

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

import java.awt.event.*;

class fileserverC extends Frame implements ActionListener,Runnable

{

TextArea txt1,txt2;

Thread t1,t2;

Button btn1,btn2;

PrintWriter pw1,pw2;

BufferedReader br1,br2;

String msg;

Socket s1,s2;

FileDialog open,save;

File f1,f2;

RandomAccessFile rf1,rf2;

String url,str;

public fileserverC()

{

setLayout(new GridLayout(4,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn1 = new Button("Message");

btn2 = new Button("File");

add(txt1);

add(btn1);

add(btn2);

add(txt2);

txt2.setText(" ");

txt1.setText(" ");

btn1.addActionListener(this);

btn2.addActionListener(this);

open = new FileDialog(this,"Select file to open",0);

save = new FileDialog(this,"Select destination to save",1);

35

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

setVisible(false);

}

});

try

{

s1 = new Socket("localhost",6969);

s2 = new Socket("localhost",6968);

br1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));

pw1 = new PrintWriter(s1.getOutputStream());

br2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));

pw2 = new PrintWriter(s2.getOutputStream());

}

catch(Exception e){

}

}

public void run()

{

try

{

if(Thread.currentThread() == t1)

{

while(true)

{

msg = br1.readLine();

txt2.append("Receive : "+msg+"\n");

}

36

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

else

{

while(true)

{

str = br2.readLine();

while(true)

{

if(str.startsWith("FILE"))

{

save.show();

url = save.getDirectory() +

save.getFile();

f1 = new File(url);

rf1 = new RandomAccessFile(f1,"rw");

txt2.append("From File : \n");

while(!

((str=br2.readLine()).endsWith("EOF")))

{

rf1.writeBytes(str);

txt2.append(str+"\n");

}

}

rf1.close();

}

}

}

}

catch(Exception e)

{

}

37

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==btn1)

{

msg = txt1.getText();

txt2.append("Send : "+msg+"\n");

pw1.println(msg);

pw1.flush();

}

else if(ae.getSource()==btn2)

{

open.show();

pw2.println("FILE");

pw2.flush();

url = open.getDirectory() + open.getFile();

f2 = new File(url);

try

{

rf2= new RandomAccessFile(f2,"r");

while(rf2.getFilePointer()<rf2.length())

{

str = rf2.readLine();

pw2.println(str);

pw2.flush();

}

pw2.println("EOF");

pw2.flush();

38

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

rf2.close();

}

catch(Exception e)

{

}

}

}

public static void main(String args[])

throws Exception

{

fileserverC obj = new fileserverC();

obj.setSize(300,300);

obj.setVisible(true);

obj.t1 = new Thread(obj);

obj.t2 = new Thread(obj);

obj.t1.start();

obj.t2.start();

}

}

//SERVER SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class fileserverS implements Runnable

{

ServerSocket ss1,ss2;

Socket s1,s2,fs1,fs2;

39

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

String msg1,msg2;

PrintWriter pw1,pw2,pw3,pw4;

Thread t[] = new Thread[4];

BufferedReader br1,br2,br3,br4;

public fileserverS()

{

try

{

ss1 = new ServerSocket(6969);

ss2 = new ServerSocket(6968);

s1 = ss1.accept();

System.out.println("S1\n");

s2 = ss1.accept();

fs1 = ss2.accept();

fs2 = ss2.accept();

br1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));

br2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));

br3 = new BufferedReader(new InputStreamReader(fs1.getInputStream()));

br4 = new BufferedReader(new InputStreamReader(fs2.getInputStream()));

pw1 = new PrintWriter(s1.getOutputStream(),true);

pw2 = new PrintWriter(s2.getOutputStream(),true);

pw3 = new PrintWriter(fs1.getOutputStream(),true);

pw4 = new PrintWriter(fs2.getOutputStream(),true);

}

catch(Exception e)

{

System.out.println(e);

}

}

40

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

public void run()

{

try

{

System.out.println("C Thread : "+Thread.currentThread());

if(Thread.currentThread() == t[0])

{

while(true)

{

msg1 = br1.readLine();

System.out.println("Client1 : "+msg1);

pw2.println(msg1);

pw2.flush();

}

}

else if(Thread.currentThread() == t[1])

{

while(true)

{

msg2 = br2.readLine();

System.out.println("Client2 msg : "+msg2);

pw1.println(msg2);

pw1.flush();

}

41

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

else if(Thread.currentThread() == t[2])

{

while(true)

{

msg2 = br3.readLine();

System.out.println("Client2 msg : "+msg2);

pw4.println(msg2);

pw4.flush();

}

}

else if(Thread.currentThread() == t[3])

{

while(true)

{

msg2 = br4.readLine();

System.out.println("Client2 msg : "+msg2);

pw3.println(msg2);

pw3.flush();

}

}

}

catch(Exception e)

{

System.out.println(e);

}

}

42

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

public static void main(String args[]) throws Exception

{

fileserverS obj = new fileserverS();

for(int i=0;i<4;i++)

{

obj.t[i] = new Thread(obj);

obj.t[i].start();

}

}

}

Program No.9

QUESTION:

Write a program to implement public chat from one client to all clients.

ALGORITHM:

1. Start

2. At the server side create a simple socket array and a server socket.

3. Create two threads one for accepting the connection request from the client.

4. The other thread is used to read the message from one client and send it to the

other clients.

5. As the client side, create a simple socket.

6. Create two threads, one for reaching the keyboard and the other to receive the

data from output buffer.

7. Stop.

PROGRAM:

43

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

//CLIENT SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class PublicChatC extends Frame implements ActionListener

{

TextArea txt1,txt2;

Button btn;

PrintWriter pw;

BufferedReader br;

String msg;

Socket s;

public PublicChatC()

{

setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

add(txt1);

add(btn);

add(txt2);

txt2.setText(" ");

txt1.setText(" ");

btn.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

setVisible(false);

44

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

});

}

public void setConnection()

{

try

{

s = new Socket("localhost",6969);

br = new BufferedReader(new

InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

while(true)

{

msg = br.readLine();

txt2.append("Receive : "+msg+"\n");

}

}

catch(Exception e){

}

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==btn)

{

msg = txt1.getText();

txt2.append("Send : "+msg+"\n");

pw.println(msg);

45

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

pw.flush();

}

}

public static void main(String args[])

throws Exception

{

PublicChatC obj = new PublicChatC();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

//SERVER SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class PublicChatS implements Runnable

{

ServerSocket ss;

Socket s[] = new Socket[10];

String msg1,msg2;

PrintWriter pw;

Thread t[] = new Thread[10];

BufferedReader br;

int i,k;

public PublicChatS()

{

46

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

try

{

ss = new ServerSocket(6969);

for(i=0;i<3;i++)

s[i] = ss.accept();

}

catch(Exception e)

{

System.out.println(e);

}

}

public void run()

{

try

{

while(true)

{

for(int j=0;j<i;j++)

{

if(Thread.currentThread() == t[j])

{

System.out.println("C Thread : "+Thread.currentThread());

br = new BufferedReader(new InputStreamReader(s[j].getInputStream()));

msg1 = br.readLine();

System.out.println("Rec : "+msg1);

k = j;

}

}

47

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

for(int p=0;p<i;p++)

{

if(p!=k)

{

pw = new PrintWriter(s[p].getOutputStream(),true);

pw.println(msg1);

pw.flush();

}

}

}

}

catch(Exception e)

{

System.out.println(e);

}

}

public static void main(String args[]) throws Exception

{

PublicChatS obj = new PublicChatS();

for(int i=0;i<3;i++)

obj.t[i] = new Thread(obj);

for(int i=0;i<3;i++)

obj.t[i].start();

}

}

Program No.10

QUESTION:

48

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Write a program to implement an alarm clock using

a)Applet b)Application

ALGORITHM:

1. Start

2. Design an interface with necessary text boxes, buttons and Labels.

3. Get the system time using appropriate methods.

4. Display it in a text box.

5. Enter the alarm time in another text box.

6. When the system time matches with the alarm time display a message in

the Label indicating that the alarm is ON.

7. Stop

PROGRAM:

/*<applet code="alarmclock.class" height=400 width=300>

</applet>*/

import java.awt.event.*;

import java.util.Date;

import java.awt.*;

49

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

import java.applet.*;

import java.lang.*;

import java.applet.AudioClip;

public class alarmclock extends Applet implements ActionListener,Runnable

{

int a=0;

Button b1,b2;

TextField t1,t2,t3;

Label l1,l2,l3;

Date d;

String s,s1,a1;

Thread t;

AudioClip c;

public void init()

{

l1=new Label("current Time");

l2=new Label("Alarm Time");

l3=new Label("Alarm status");

b1=new Button("Set alarm");

b2=new Button("Reset");

t1=new TextField(15);

t2=new TextField(15);

t3=new TextField(15);;

b2.addActionListener(this);

b1.addActionListener(this);

setLayout(new FlowLayout());

add(l1);

add(t1);

add(l2);

add(t2);

50

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

add(l3);

add(t3);

add(b1);

add(b2);

t=new Thread(this);

t.start();

setVisible(true);

}

public void run()

{

while(true)

{

d=new Date();

s=new String(d.toString());

s1=s.substring(11,20);

t1.setText(s1);

a1=t2.getText().trim();

if(a==1)

{

if(a1.equals(s1.trim()))

{

t3.setText("ALARM ON");

c.play();

}

}

else

{

t3.setText("ALARM OFF");

}

try

{

51

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Thread.sleep(1000);

t1.setText("");

}

catch(InterruptedException e)

{

System.out.println("Cannot display");

}

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b2)

{

t2.setText(" ");

t3.setText(" ");

a=0;

}

if(e.getSource()==b1)

{

a=1;

t3.setText("alarm set");

}

}

}

// HTML CODE

<html>

<applet code="alarmclock.class" width=200 height=400>

</applet>

</html>

52

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Program No.11

QUESTION:

Implement a frame application were we have one manager as the head of a division of

projects and we have two agents as project managers, also we have two sub agents for agents as

project leaders.

ALGORITHM:

1. Start

2. Client Program

a) Create a client socket.

b) Read the message sent from the network manager through the socket.

c) Output the data on the project manager.

d) Create a client socket for project manager.

e) Read the message sent from the project manager through the socket.

f)Output the data on the agents.

3. Server program

a) Create a Server socket and a simple socket array.

53

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

b) Create two threads, one to accept clients and another one to send data to

clients.

c) If the first thread is active, new clients can be connected to the server and the

number of clients connected can be found out.

d) If the second thread is active then data is read from the keyboard and send to

all clients through corresponding sockets.

4. Stop.

PROGRAM:

//CLIENT SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ProjectC extends Frame implements ActionListener

{

TextArea txt1,txt2;

Button btn;

PrintWriter pw;

BufferedReader br;

String msg;

Socket s;

public ProjectC()

{

54

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

setLayout(new GridLayout(3,1));

txt1 = new TextArea();

txt2 = new TextArea();

btn = new Button("Send");

add(txt1);

add(btn);

add(txt2);

txt2.setText(" ");

txt1.setText(" ");

btn.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

setVisible(false);

}

});

}

public void setConnection()

{

try

{

s = new Socket("localhost",6969);

br = new BufferedReader(new InputStreamReader(s.getInputStream()));

pw = new PrintWriter(s.getOutputStream());

while(true)

{

msg = br.readLine();

txt2.append("Receive : "+msg+"\n");

// txt1.append(msg+"\n");

}

55

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

}

catch(Exception e){

}

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==btn)

{

msg = txt1.getText();

txt2.append("Send : "+msg+"\n");

pw.println(msg);

pw.flush();

}

}

public static void main(String args[]) throws Exception

{

ProjectC obj = new ProjectC();

obj.setSize(300,300);

obj.setVisible(true);

obj.setConnection();

}

}

//SERVER SIDE

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

class ProjectS implements Runnable

56

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

{

ServerSocket ss;

Socket s[] = new Socket[10];

String msg1,msg2;

PrintWriter pw1,pw2;

Thread t[] = new Thread[10];

BufferedReader br1;

int i,k;

public ProjectS()

{

try

{

ss = new ServerSocket(6969);

for(i=0;i<7;i++)

{

s[i] = ss.accept();

if(i==0)

System.out.println("Divisional manager connected...\n");

else if(i==1||i==2)

System.out.println("Project manager" + " connected...\n");

else if(i==3||i==4||i==5||i==6)

System.out.println("Project Leader" + " connected...\n");

}

}

catch(Exception e)

{

System.out.println(e);

}

}

public void run()

57

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

{

try

{

while(true)

{

if(Thread.currentThread() == t[0])

{

System.out.println("C Thread : "+Thread.currentThread());

br1 = new BufferedReader(new InputStreamReader(s[0].getInputStream()));

msg1 = br1.readLine();

pw1 = new PrintWriter(s[1].getOutputStream(),true);

pw2 = new PrintWriter(s[2].getOutputStream(),true);

pw1.println(msg1);

pw1.flush();

pw2.println(msg1);

pw2.flush();

System.out.println("Rec from DM: "+msg1);

}

if(Thread.currentThread() == t[1])

{

System.out.println("C Thread : "+Thread.currentThread());

br1 = new BufferedReader(new InputStreamReader(s[1].getInputStream()));

msg1 = br1.readLine();

pw1 = new PrintWriter(s[3].getOutputStream(),true);

pw2 = new PrintWriter(s[4].getOutputStream(),true);

pw1.println(msg1);

pw1.flush();

pw2.println(msg1);

pw2.flush();

58

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

System.out.println("Rec from PM: "+msg1);

}

if(Thread.currentThread() == t[2])

{

System.out.println("C Thread : "+Thread.currentThread());

br1 = new BufferedReader(new InputStreamReader(s[2].getInputStream()));

msg1 = br1.readLine();

pw1 = new PrintWriter(s[5].getOutputStream(),true);

pw2 = new PrintWriter(s[6].getOutputStream(),true);

pw1.println(msg1);

pw1.flush();

pw2.println(msg1);

pw2.flush();

System.out.println("Rec from PM: "+msg1);

}

}

}

catch(Exception e)

{

System.out.println(e);

}

}

public static void main(String args[])

throws Exception

{

ProjectS obj = new ProjectS();

for(int i=0;i<7;i++)

obj.t[i] = new Thread(obj);

59

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

for(int i=0;i<7;i++)

obj.t[i].start();

}

}

VIVA VOCE

1. Explain the process of TCP/IP connection establishment.

2. When is this process taking place in a Java program?

3. Which are the classes available in Java for socket creation?

4. What is a socket?

5. What is IP address and port number?

6. What is payload in a UDP packet?

7. Where do we specify IP address and port number in case of UDP?

8. What is echo protocol?

9. How many bits are there in IPAddress?

10. Is hostname, IP address, localhost all the same?

11. What are the differences between UDP and TCP?

12. Explain the process of TCP/IP connection closing.

13. When is this process taking place in a Java program?

14. What do you mean by input/output stream of a socket? How do you get them?

15. What are the packages required in Java network programming?

16. Which java packages are used for Networking?

17. What are the possible exceptions raised while connecting to a server?

18. How do you create a socket?

19. What is the method setSoTimeout used for?

20. What is a socket?

21. What is IP address and port number?

22. What is payload in a UDP packet?

23. Where do we specify IP address and port number in case of UDP?

24. What is echo protocol?

60

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

25. Is hostname, IP address, localhost all the same?

26. Which are the classes available in Java for socket creation?

27. What are the differences between UDP and TCP?

28. Explain the process of TCP/IP connection closing.

29. When is this process taking place in a Java program?

30. What do you mean by input/output stream of a socket? How do you get them?

31. Which java packages are used for Networking?

32. What are the possible exceptions raised while connecting to a server?

33. What is the method setSoTimeout used for in UDP?

34. What are the seven layers of ISO/OSI model and their functions?

35. What are the protocols used in each layer?

36. What are bridges, routers, and gateways?

37. Which are the various switching techniques?

38. Difference between transmission and communication.

39. What is multicasting and broadcasting?

40. What is MAC address?

41. Which are the different classes of network addresses?

42. Which are the major types and topologies of network?

43. What are applets? Life cycle of an Applet.

44. How is exception handling done in java?

45. Life cycle of a Thread.

46. What is an interface and explain the structure and working of a simple RMI Program

47. _______ , _________ and _________ are the main keywords used for Exception

handling in Java.

48. IP address is a ___ bit number.

49. To create a thread class, extend from ______ or implement _______.

50. Port number used for SMTP is ____

51. JavaScript is used for __________ scripting.

52. __________ Exception is raised when server is not running or available.

53. XML uses ______________ to describe data.

54. XML preserves white spaces. (True/False)

61

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

55. XML elements cannot be empty (True/False)

56. XSL stands for ____________________________________

57. Explain the process of TCP/IP connection closing and write the Java statement which

does this closing.

58. Explain the life cycle of an applet.

59. _________ and _________ are the packages required for network programming in Java.

60. IP address is a ___ bit number.

61. The method setSoTimeout. Is used for _____________.

62. ________ addresses are used for IP Multicasting.

63. JavaScript is used for __________ scripting.

64. ___________________ Exception is raised when command line arguments are missing.

65. To make a bean available only In a particular JSP page the scope should be set to

___________________

66. __________, ____________ and __________ are declared in DTD.

67. _________ is used to navigate through a. XML document.

68. ___________________tag is used to include a package in JSP page.

69. Explain the process of TCP/IP connection establishment and write the Java statement

which does this process.

70. Explain the steps involved in writing an RMI client/server.

71. Define LAN - local area network

72. Define "signal" as it applies to data communications.

73. A signal is a voltage carried across a cable.

74. Broadband signal transmission is used more frequently than base band signal

transmission in LAN environments. (T/F, circle correct answer)

a. TRUE FALSE

b. ^^^^^

75. Explain the difference between baseband and broadband transmissions.

Base band uses discrete values by measuring a voltage on a wire. Only one signal may be

transmitted at a time. Only one channel of communication can be carried by that signal.

Broadband transmissions can carry a range of values equal to the bandwidth available. One

62

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

common encoding scheme is FDM (frequency division multiplexing). Broadband transmissions

can carry multiple channels of communication with one signal.

76. Explain the difference between analog and digital signals.

Analog signals carry values at the extremes of their frequencies (top and bottom of the sine

wave) and at every value in between. A digital signal has discrete values associated with it,

lacking the in-between values. For this reason, analog signals can carry more information in the

same interval.

77. UTP is a kind of what?

cable (wire)

78. What does UTP stand for?

unshielded twisted pair

79. Another name for thinnet is what?

10Base-2

80. What is a NIC?

network interface card, network information center

81. Name three forms of network connection media.

copper cables: utp, stp, coaxial

fiber optic cables

wireless: infrared, laser, microwave, radio

82. What is the definition of a network protocol?

a set of rules which define a "language" with which computers can communicate

across a network

83. What are layers of abstraction?

A layer is a protocol which uses the services of the layer below and provides

services to the layer above. In this way, a layer is isolated from all layers

to which it is not directly connected and thus will not suffer if those other

layers are altered.

Typically, low-level layers interact with hardware components and upper-level

a. layers interact with humans. Although additional layering makes changes much

b. easier to implement, performance does suffer as a result.

84. What is ISO?

63

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

International Organization for Standardization

85. What does OSI stand for?

Open Systems Interconnection

86. How many layers are there in the ISO reference model?

7

87. Why are there that many layers in the reference model?

(there were 7 committees working on the model)

88. Name the layers of the ISO reference model.

physical, data link, network, transport, session, presentation, application

89. Which layer describes how to represent the start and end of a transmission?

physical

90. Describe the difference between connectionless and connection-oriented data

connections.

Connection-oriented connections include some sort of setup at the beginning of data

exchange and usually include some sort of tear down at the end. In this way, the data

transmission can be checked for errors and some form of error recovery can be attempted. A

connectionless data transmission has no such overhead. As a result, connectionless

transmissions don't try to guarantee delivery.

91. What layer of service splits data into frames?

Data link

92. What is the lowest layer of the ISO reference model which sees "end-to-end"

connectivity?

Transport

93. Describe the difference between circuit switched and packet switched networks.

A circuit switched network establishes physical connections between all intermediate

nodes and sends all packets through those connections. A packet switched network does not

establish connections first, but instead sends packets (datagram’s) through any available route.

In this way, datagram’s can be sent along multiple routes to the same destination.

94. What is the lowest layer of the ISO reference model in which a node is identified with a

unified address scheme?

Network

64

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

95. What is the lowest layer of the ISO reference model in which a particular process on a

node is addressed?

Transport

96. What is the lowest layer of the ISO reference model in which a protocol deals with data

buffering?

Transport

97. Describe the difference between multiplexing and parallelization.

Multiplexing involves sending multiple transmissions through the same channel, while

parallelization involves splitting one transmission across several channels.

98. What ISO reference model layer describes data representation?

i. Session

99. Name three standards for data representation.

i. EBCDIC, ASCII, ASN.1

100. What protocol did Novell Netware popularize and use (almost exclusively)?

i. ipx/spx

101. What is the protocol stack used by Apple Macintosh's called?

i. Appletalk

102. What is the most common protocol stack employed on UNIX systems?

i. tcp/ip

103. What does NOS stand for?

i. Network operating system

104. What does IEEE specification 802.5 describe?

i. Token Ring standard

105. What does CSMA/CD stand for?

i. Carrier sense multiple access with collision detection

106. HDLC, LAPB, SLIP, X.25, SDLC, PPP, and frame relay are examples of what ISO

reference model layer protocols?

Data link

107. Novell Netware is best described as a what?

OS CSMA/CD MIB NOS

^^^^^

65

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

108. An FTP session transfers 100MB of data in 67 minutes for an average

Transfer rate of ~25KB/sec. This is a measure of what?

Throughput bandwidth interference capacitance

^^^^^^^^^^

109. A DNS lookup is an example of what kind of relationship?

Peer-to-peer master-slave client-server connection-oriented

^^^^^^^^^^^^^

110. Which of the following is not a kind of LAN technology?

ethernet PBX token-ring XNS

^^^^^

111. Which is usually bigger?

MAN LAN WAN

^^^^^

112. What is difference between ASP and JSP?

113. Difference between Socket and Port no?

114. What is difference between DHTML and XML?

115. What is J2EE?

116. What is EJB?

117. What is JSP?

118. Applications of XML. Its usage in Databases?

119. Which is portable XML or HTML?

120. What is the range for the protocols?

121. What are the RFC’s for the protocols?

122. What is IETF?

123. Learn the syntaxes for Networking.

124. Explain the process of TCP/IP connection establishment.

125. When is this process taking place in a Java program?

126. Which are the classes available in Java for socket creation?

127. What is a socket?

128. What is IP address and port number?

129. What is payload in a UDP packet?

66

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

130. Where do we specify IP address and port number in case of UDP?

131. What is echo protocol?

132. How many bits are there in IPAddress?

133. Is hostname, IP address, localhost all the same?

134. What are the differences between UDP and TCP?

135. Explain the process of TCP/IP connection closing.

136. When is this process taking place in a Java program?

137. What do you mean by input/output stream of a socket? How do you get them?

138. What are the packages required in Java network programming?

139. Which java packages are used for Networking?

140. What are the possible exceptions raised while connecting to a server?

141. How do you create a socket?

142. What are the two types of transmission technology available?

143. (i) Broadcast and (ii) point-to-point

144. What is subnet?

A generic term for section of a large networks usually separated by a bridge or router.

145. Difference between the communication and transmission.

Transmission is a physical movement of information and concern issues like bit polarity,

synchronisation, clock etc. Communication means the meaning full exchange of information

between two communication media.

146. What are the possible ways of data exchange?

(i) Simplex (ii) Half-duplex (iii) Full-duplex.

147. What is SAP?

Series of interface points that allow other computers to communicate with the other layers of

network protocol stack.

148. What do you meant by "triple X" in Networks?

The function of PAD (Packet Assembler Disassembler) is described in a document

known as X.3. The standard protocol has been defined between the terminal and the PAD, called

X.28; another standard protocol exists between hte PAD and the network, called X.29. Together,

these three recommendations are often called "triple X"

149. What is frame relay, in which layer it comes?

67

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Frame relay is a packet switching technology. It will operate in the data link layer.

150. What is terminal emulation, in which layer it comes?

Telnet is also called as terminal emulation. It belongs to application layer.

151. What is Beaconing?

The process that allows a network to self-repair networks problems. The stations on the

network notify the other stations on the ring when they are not receiving the transmissions.

Beaconing is used in Token ring and FDDI networks.

152. What is redirector?

Redirector is software that intercepts file or prints I/O requests and translates them into

network requests. This comes under presentation layer.

153. What are NETBIOS and NETBEUI?

NETBIOS is a programming interface that allows I/O requests to be sent to and received from a

remote computer and it hides the networking hardware from applications. NETBEUI is NetBIOS

extended user interface. A transport protocol designed by microsoft and IBM for the use on

small subnets.

154. What is RAID?

A method for providing fault tolerance by using multiple hard disk drives.

155. What is passive topology?

When the computers on the network simply listen and receive the signal, they are referred to as

passive because they don’t amplify the signal in any way. Example for passive topology - linear

bus.

156. What is Brouter?

Hybrid devices that combine the features of both bridges and routers.

157. What is cladding?

A layer of a glass surrounding the center fiber of glass inside a fiber-optic cable.

158. What is point-to-point protocol

A communications protocol used to connect computers to remote networking services

including Internet service providers.

159. How Gateway is different from Routers?

A gateway operates at the upper levels of the OSI model and translates information

68

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

between two completely different network architectures or data formats

160. What is attenuation?

The degeneration of a signal over distance on a network cable is called attenuation.

161. What is MAC address?

The address for a device as it is identified at the Media Access Control (MAC) layer in the

network architecture. MAC address is usually stored in ROM on the network adapter card and is

unique.

162. Difference between bit rate and baud rate.

Bit rate is the number of bits transmitted during one second whereas baud rate refers to the

number of signal units per second that are required to represent those bits. baud rate = bit rate /

N where N is no-of-bits represented by each signal shift.

163. What is Bandwidth?

Every line has an upper limit and a lower limit on the frequency of signals it can carry.

This limited range is called the bandwidth.

164. What are the types of Transmission media?

Signals are usually transmitted over some transmission media that are

broadly classified in to two categories.

a) Guided Media:

These are those that provide a conduit from one device to another that include twisted-pair,

coaxial cable and fiber-optic cable. A signal traveling along any of these media is directed and is

contained by the physical limits of the medium. Twisted-pair and coaxial cable use metallic that

accept and transport signals in the form of electrical current. Optical fiber is a glass or plastic

cable that accepts and transports signals in the form of light.

b) Unguided Media:

This is the wireless media that transport electromagnetic waves without using a physical

conductor. Signals are broadcast either through air. This is done through radio communication,

satellite communication and cellular telephony.

165. What is Project 802?

It is a project started by IEEE to set standards to enable intercommunication between equipment

from a variety of manufacturers. It is a way for specifying functions of the physical layer, the

69

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

data link layer and to some extent the network layer to allow for interconnectivity of major LAN

protocols.

It consists of the following:

802.1 is an internetworking standard for compatibility of different LANs and

MANs across protocols.

802.2 Logical link control (LLC) is the upper sublayer of the data link layer

which is non-architecture-specific, that is remains the same for all IEEE-defined LANs.

Media access control (MAC) is the lower sublayer of the data link layer that contains some

distinct modules each carrying proprietary information specific to the LAN product being used.

The modules are Ethernet LAN (802.3), Token ring LAN (802.4), Token bus LAN (802.5).

802.6 is distributed queue dual bus (DQDB) designed to be used in MANs.

166. What is Protocol Data Unit?

The data unit in the LLC level is called the protocol data unit (PDU). The PDU contains

of four fields a destination service access point (DSAP), a source service access point (SSAP), a

control field and an information field. DSAP, SSAP are addresses used by the LLC to identify

the protocol stacks on the receiving and sending machines that are generating and using the data.

The control field specifies whether the PDU frame is a information frame (I - frame) or a

supervisory frame (S - frame) or a unnumbered frame (U - frame).

167. What are the different type of networking / internetworking devices?

Repeater:

Also called a regenerator, it is an electronic device that operates only at physical layer. It

receives the signal in the network before it becomes weak, regenerates the original bit pattern

and puts the refreshed copy back in to the link.

Bridges:

These operate both in the physical and data link layers of LANs of same type. They divide a

larger network in to smaller segments. They contain logic that allow them to keep the traffic for

each segment separate and thus are repeaters that relay a frame only the side of the segment

containing the intended recipent and control congestion.

Routers:

70

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

They relay packets among multiple interconnected networks (i.e. LANs of different type). They

operate in the physical, data link and network layers. They contain software that enable them to

determine which of the several possible paths is the best for a particular transmission.

Gateways:

They relay packets among networks that have different protocols (e.g. between a LAN and a

WAN). They accept a packet formatted for one protocol and convert it to a packet formatted for

another protocol before forwarding it. They operate in all seven layers of the OSI model.

168. What is ICMP?

ICMP is Internet Control Message Protocol, a network layer protocol of the

TCP/IP suite used by hosts and gateways to send notification of datagram problems back to the

sender. It uses the echo test / reply to test whether a destination is reachable and responding. It

also handles both control and error messages.

169. What are the data units at different layers of the TCP / IP protocol suite?

The data unit created at the application layer is called a message, at the transport layer the

data unit created is called either a segment or an user datagram, at the network layer the data unit

created is called the datagram, at the data link layer the datagram is encapsulated in to a frame

and finally transmitted as signals along the transmission media.

170. What is difference between ARP and RARP?

The address resolution protocol (ARP) is used to associate the 32 bit IP address with the

48 bit physical address, used by a host or a router to find the physical address of another host on

its network by sending a ARP query packet that includes the IP address of the receiver. The

reverse address resolution protocol (RARP) allows a host to discover its Internet address when it

knows only its physical address.

171. What is the minimum and maximum length of the header in the TCP segment and IP

datagram?

The header should have a minimum length of 20 bytes and can have a

maximum length of 60 bytes.

172. What is the range of addresses in the classes of internet addresses?

Class A 0.0.0.0 - 127.255.255.255

Class B 128.0.0.0 - 191.255.255.255

Class C 192.0.0.0 - 223.255.255.255

71

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Class D 224.0.0.0 - 239.255.255.255

Class E 240.0.0.0 - 247.255.255.255

173. What is the difference between TFTP and FTP application layer protocols?

The Trivial File Transfer Protocol (TFTP) allows a local host to obtain files from a remote host

but does not provide reliability or security. It uses the fundamental packet delivery services

offered by UDP. The File Transfer Protocol (FTP) is the standard mechanism provided by TCP /

IP for copying a file from one host to another. It uses the services offer by TCP and so is reliable

and secure. It establishes two connections (virtual circuits) between the hosts, one for data

transfer and another for control information.

174. What are major types of networks and explain?

Server-based network Peer-to-peer network

Peer-to-peer network, computers can act as both servers sharing resources and as clients

using the resources.

Server-based networks provide centralized control of network resources and rely on

server computers to provide security and network administration

175. What are the important topologies for networks?

BUS topology:

In this each computer is directly connected to primary network cable in a single line.

Advantages:

Inexpensive, easy to install, simple to understand, easy to extend.

STAR topology:

In this all computers are connected using a central hub.

Advantages:

Can be inexpensive, easy to install and reconfigure and easy to trouble shoot physical

problems.

RING topology:

In this all computers are connected in loop.

Advantages:

All computers have equal access to network media, installation can be simple, and signal does

not degrade as much as in other topologies because each computer regenerates it.

176. What is mesh network?

72

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

A network in which there are multiple network links between computers to provide multiple

paths for data to travel.

177. What is difference between baseband and broadband transmission?

In a baseband transmission, the entire bandwidth of the cable is consumed by a single signal. In

broadband transmission, signals are sent on multiple frequencies, allowing multiple signals to be

sent simultaneously.

178. Explain 5-4-3 rule?

In a Ethernet network, between any two points on the network ,there can be no more than five

network segments or four repeaters, and of those five segments only three of segments can be

populated.

179. What MAU?

In token Ring , hub is called Multistation Access Unit(MAU).

180. What is the difference between routable and non- routable protocols?

Routable protocols can work with a router and can be used to build large networks. Non-

Routable protocols are designed to work on small, local networks and cannot be used with a

router

181. Why should you care about the OSI Reference Model?

It provides a framework for discussing network operations and design.

182. What is logical link control?

One of two sublayers of the data link layer of OSI reference model, as defined by the IEEE 802

standard. This sublayer is responsible for maintaining the link between computers when they are

sending data across the physical network connection.

183. What is virtual channel?

Virtual channel is normally a connection from one source to one destination, although multicast

connections are also permitted. The other name for virtual channel is virtual circuit.

184. What is virtual path?

Along any transmission path from a given source to a given destination, a group of virtual

circuits can be grouped together into what is called path.

185. What is packet filter?

73

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

Packet filter is a standard router equipped with some extra functionality. The extra functionality

allows every incoming or outgoing packet to be inspected. Packets meeting some criterion are

forwarded normally. Those that fail the test are dropped.

186. What is traffic shaping?

One of the main causes of congestion is that traffic is often busy. If hosts could be made to

transmit at a uniform rate, congestion would be less common. Another open loop method to help

manage congestion is forcing the packet to be transmitted at a more predictable rate. This is

called traffic shaping.

187. What is multicast routing?

Sending a message to a group is called multicasting, and its routing algorithm is called multicast

routing.

188. What is region?

When hierarchical routing is used, the routers are divided into what we will call regions, with

each router knowing all the details about how to route packets to destinations within its own

region, but knowing nothing about the internal structure of other regions.

189. What is silly window syndrome?

It is a problem that can ruin TCP performance. This problem occurs when data are passed to the

sending TCP entity in large blocks, but an interactive application on the receiving side reads 1

byte at a time.

190. Expand IDEA.

IDEA stands for International Data Encryption Algorithm.

191. What is wide-mouth frog?

Wide-mouth frog is the simplest known key distribution center (KDC) authentication protocol.

192. What is Mail Gateway?

It is a system that performs a protocol translation between different electronic mail delivery

protocols.

193. What is IGP (Interior Gateway Protocol)?

It is any routing protocol used within an autonomous system.

194. What is EGP (Exterior Gateway Protocol)?

It is the protocol the routers in neighboring autonomous systems use to identify the set of

74

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

networks that can be reached within or via each autonomous system.

195. What is autonomous system?

It is a collection of routers under the control of a single administrative authority and that uses a

common Interior Gateway Protocol.

196. What is BGP (Border Gateway Protocol)?

It is a protocol used to advertise the set of networks that can be reached with in an autonomous

system. BGP enables this information to be shared with the autonomous system. This is newer

than EGP (Exterior Gateway Protocol).

197. What is Gateway-to-Gateway protocol?

It is a protocol formerly used to exchange routing information between Internet core routers.

198. What is NVT (Network Virtual Terminal)?

It is a set of rules defining a very simple virtual terminal interaction. The NVT is used in the start

of a Telnet session.

199. What is a Multi-homed Host?

It is a host that has a multiple network interfaces and that requires multiple IP addresses is called

as a Multi-homed Host.

200. What is Kerberos?

It is an authentication service developed at the Massachusetts Institute of Technology. Kerberos

uses encryption to prevent intruders from discovering passwords and gaining unauthorized

access to files.

201. What is OSPF?

It is an Internet routing protocol that scales well, can route traffic along multiple paths, and uses

knowledge of an Internet's topology to make accurate routing decisions.

202. What is Proxy ARP?

It is using a router to answer ARP requests. This will be done when the originating host believes

that a destination is local, when in fact is lies beyond router.

203. What is SLIP (Serial Line Interface Protocol)?

It is a very simple protocol used for transmission of IP datagrams across a serial line.

204. What is RIP (Routing Information Protocol)?

It is a simple protocol used to exchange information between the routers.

205. What is source route?

75

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

It is a sequence of IP addresses identifying the route a datagram must follow. A source route may

optionally be included in an IP datagram header.

206. What is a socket?

A socket is an endpoint for communication between two machines. ie. for

sending and receiving data between computers. A socket is one end of a process that the

application is using to communicate, and is defined by two addresses: the IP address of the host

computer, and the port address of the application or process running on the host.

207. What is an IP address?

Every computer connected to the Internet is assigned a unique number known as an Internet

Protocol (IP) address. To make it easier for us humans to remember, IP addresses are normally

expressed in decimal format as a "dotted decimal number"

208. What is the full form of ISO/OSI?

ISO/OSI- International Standards Organization /Open Systems Interconnection

209. What is the need for creation of ISO/OSI model?

The theory and idea behind having standards accepted, ratified, and agreed upon

by nations around the world, is to ensure that the system from Country A will be easily

integrated with the system from Country B with little effort. It also helps to make specification

for industries to create goods and services that conform to the standard and by providing

competition to the same product, decrease prices for products that must match the minimum

standards. Comparisons are made easier in this way for products made by competing groups that

must meet or exceed the minimum accepted specified standards

210. Which are the 7 layers in OSI Model?

Physical Layer, Data Link Layer, Network Layer, Transport Layer, Session Layer

Presentation Layer, Application Layer

211. Which are the protocols used in each of these layers?

Physical Layer-RS232, Data Link Layer-SLIP, PPP, X.25 etc. Network Layer-IP,

IGMP (Internet Group Management Protocol),ARP etc. Transport Layer-TCP, UDP etc. Session

Layer-NetBIOS, CMIP (Common Management Information Protocol), Presentation Layer-

SMTP (Simple Mail Transfer Protocol), TELNET, SNMP (Simple Network

Management Protocol), NFS (Network File System) etc.

Application Layer-HTTP, FTP, RPC etc

76

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

212. What are the functions of each of these layers?

Layer 1: Physical Layer Defines physical means of sending data over network

devices Interfaces between network medium and devices Defines optical, electrical and

mechanical characteristics

Layer 2: Data Link Layer Defines procedures for operating the communication

links Frames packets Detects and corrects packets transmit errors

Layer 3: Network Layer Determines how data are transferred between network

devices Routes packets according to unique network device addresses Provides flow and

congestion control to prevent network resource depletion

Layer 4: Transport Layer Manages end-to-end message delivery in

network, Provides reliable and sequential packet delivery through error recovery and flow

control mechanisms. Provides connectionless oriented packet delivery

Layer 5: Session Layer Manages user sessions and dialogues, Controls

establishment and termination of logic links between users. Reports upper layer errors

Layer 6: Presentation Layer Masks the differences of data formats between dissimilar

systems, Specifies architecture-independent data transfer format, Encodes and decodes data;

Encrypts and decrypts data; Compresses and decompresses data

213. Layer 7: Application Layer Defines interface to user processes for communication and

data transfer in network. Provides standardized services such as virtual terminal file and job

transfer and operations

214. Which among the OSI layer deals with segmentation, routing, hubs?

Segmentation-Data Link Layer, Routing-Network Layer, Hubs-Physical Layer.

215. What is Sliding Window protocol?

Sliding Window Protocol is a connection-less protocol. It allows data to be sent in one

direction between a pair of protocol entities, subject to a maximum number of unacknowledged

messages.

216. What is the difference between a switch and a hub?

A hub is typically the least expensive, least intelligent and least complicated .Its job is

very, very simple: anything that comes in one port is sent out to the others. That's it. Every

computer connected to the hub "sees" everything that every other computer on the hub sees. The

hub itself is blissfully ignorant of the data being transmitted. A switch does essentially what a

77

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

hub does, but more efficiently. By paying attention to the traffic that comes across it, it can

"learn" where particular addresses are. For example, if it sees traffic from machine A coming in

on port 2, it now knows that machine A is connected to that port, and that traffic to machine A

needs to only be sent to that port and not any of the others. The net result of using a switch over a

hub is that most of the network traffic only goes where it needs to, rather than to every port. On

busy networks, this can make the network significantly faster.

217. What is the difference between TCP and UDP?

TCP (Transmission Control Protocol) is connection orientated protocol, which basically

says that any packets sent using the TCP protocol have the knowledge of reaching their

destination, and if not reached the packets will be resent until told not to. UDP (User Datagram

Protocol) is an alternative to the Protocol TCP, and uses the Internet Protocol (IP) to actually get

a data unit (datagram) from one network node to another. UDP does not provide the service of

dividing a message into packets (unlike TCP) and reassembling it at the other end. Specifically,

UDP doesn't provide sequencing of the packets that the data arrives in. UDP will send packets

and never know if they actually reach their destination.

218. What is a bridge, a router and a gateway?

A bridge reads the outermost section of data on the data packet, to tell where the message

is going. It reduces the traffic on other network segments, since it does not send all packets.

Bridges can be programmed to reject packets from particular networks. Bridging occurs at the

data link layer of the OSI model, which means the bridge cannot read IP addresses, but only the

outermost hardware address of the packet. A router is the smartest and most complicated device.

A simple way to think of a router is as a computer that can be programmed to understand,

possibly manipulate, and route the data its being asked to handle. Gateway can translate

information between different network data formats or network architectures. It can translate

TCP/IP to AppleTalk so computers supporting TCP/IP can communicate with Apple brand

computers. Most gateways operate at the application layer, but can operate at the network or

session layer of the OSI model.

219. What is w3c?

The World Wide Web Consortium (W3C) develops interoperable technologies

(specifications, guidelines, software, and tools) to lead the Web to its full potential. W3C is a

forum for information, commerce, communication, and collective understanding

78

Network Programming Lab Manuel Dept of Computer Science & Engg, VJCET

220. Define ETHERNET?

Ethernet is a network standard of communication using either coaxial or twisted pair

cable. The most widely used for of LAN communication, Ethernet typically runs at 10

megabytes per second, though newer systems use 100 Mbps, or ever gigabit of transfer. Ethernet

is the IEEE standard 802.3.

221. Which are the various switching techniques?

Circuit Switching, Packet Switching, Message Switching.

REFERENCE

1. UNIX network programming- Stevens

2. Java2 The complete Reference-Herbert Schildt

3. Mastering Java

4. Computer Networks-Andrew .S.Tanenbaum

79