NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
III-I C.S.E.
NETWORK PROGRAMMING
LAB MANUAL
1 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
List of Programs
[Link] Program Name
1. Understanding and using of commands like ifconfig, netstat, ping, arp, telnet, ftp,
finger, traceroute, whoisetc. Usage of elementary socket system calls (socket (),
bind(), listen(), accept(),connect(),send(),recv(),sendto(),recvfrom()).
2 Implementation of Connection oriented concurrent service (TCP).
3 Implementation of Connectionless Iterative time service (UDP).
4 Implementation of Select system call.
5 Implementation of gesockopt (), setsockopt () system calls.
6 Implementation of getpeername () system call.
7 Implementation of remote command execution using socket system calls.
8 Implementation of Distance Vector Routing Algorithm.
9 Implementation of SMTP
10 Implementation of FTP.
11 Implementation of HTTP.
12 Implementation of RSA algorithm.
Note: Implement programs 2 to 7 in C and 8 to 12 in JAVA.
2 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Program: 8
AIM:Implementation of Distance Vector Routing Algorithm.
import [Link].*;
public class DVR
{
static int graph[][]; static int via[][]; static int rt[][]; static int v;
static int e;
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Please enter the number of Vertices: ");
v = [Link]([Link]());
[Link]("Please enter the number of Edges: ");
e = [Link]([Link]());
graph = new int[v][v];
via = new int[v][v];
rt = new int[v][v];
for(int i = 0; i < v; i++)
for(int j = 0; j < v; j++)
{
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 9999;
}
for(int i = 0; i < e; i++)
{
[Link]("Please enter data for Edge " + (i + 1) + ":");
[Link]("Source: ");
int s = [Link]([Link]());
s--;
3 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
[Link]("Destination: ");
int d = [Link]([Link]());
d--;
[Link]("Cost: ");
int c = [Link]([Link]());
graph[s][d] = c;
graph[d][s] = c;
}
dvr_calc_disp("The initial Routing Tables are: ");
[Link]("Please enter the Source Node for the edge whose cost has
changed: ");
int s = [Link]([Link]());
s--;
[Link]("Please enter the Destination Node for the edge whose cost has
changed: ");
int d = [Link]([Link]());
d--;
[Link]("Please enter the new cost: ");
int c = [Link]([Link]());
graph[s][d] = c;
graph[d][s] = c;
dvr_calc_disp("The new Routing Tables are: ");
}
static void dvr_calc_disp(String message)
{
[Link]();
init_tables();
update_tables();
[Link](message);
print_tables();
[Link]();
}
4 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
static void update_table(int source)
{
for(int i = 0; i < v; i++)
{
if(graph[source][i] != 9999)
{
int dist = graph[source][i];
for(int j = 0; j < v; j++)
{
int inter_dist = rt[i][j];
if(via[i][j] == source)
inter_dist = 9999;
if(dist + inter_dist < rt[source][j])
{
rt[source][j] = dist + inter_dist;
via[source][j] = i;
}
}
}
}
}
static void update_tables()
{
int k = 0;
for(int i = 0; i < 4*v; i++)
{
update_table(k);
k++;
if(k == v)
k = 0;
}
}
5 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
static void init_tables()
{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
if(i == j)
{
rt[i][j] = 0;
via[i][j] = i;
}
else
{
rt[i][j] = 9999;
via[i][j] = 100;
}
}
}
}
static void print_tables()
{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
[Link]("Dist: " + rt[i][j] + " ");
}
[Link]();
}
}
6 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Output:
Please enter the number of Vertices: 4
Please enter the number of Edges: 5
Please enter data for Edge 1: Source: 1
Destination: 2
Cost: 1
Please enter data for Edge 2:
Source: 1
Destination: 3
Cost: 3
Please enter data for Edge 3:
Source: 2
Destination: 3
Cost: 1
Please enter data for Edge 4:
Source: 2
Destination: 4
Cost: 1
Please enter data for Edge 5:
Source: 3
Destination: 4
7 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Cost: 4
The initial Routing Tables are:
Dist: 0 Dist: 1 Dist: 2 Dist: 2
Dist: 1 Dist: 0 Dist: 1 Dist: 1
Dist: 2 Dist: 1 Dist: 0 Dist: 2
Dist: 2 Dist: 1 Dist: 2 Dist: 0
Please enter the Source Node for the edge whose cost has changed: 2 Please enter
the Destination Node for the edge whose cost has changed: 4 Please enter the new
cost: 10
The new Routing Tables are:
Dist: 0 Dist: 1 Dist: 2 Dist: 6
Dist: 1 Dist: 0 Dist: 1 Dist: 5
Dist: 2 Dist: 1 Dist: 0 Dist: 4
Dist: 6 Dist: 5 Dist: 4 Dist: 0
8 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Program 9:
Implementation of SMTP
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class SendEmail
public static void main(String [] args){
String to = "youremail@[Link]";//change accordingly
String from = "youremail@[Link]";//change accordingly
String host = "localhost";//or IP address
final String username = "youremail@[Link]";
final String password = "yourpassword";
//Get the session object
Properties prop = [Link]();
[Link]("[Link]", "[Link]");
[Link]("[Link]", "465");
[Link]("[Link]", "true");
9 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
[Link]("[Link]", "465");
[Link]("[Link]", "[Link]");
Session session = [Link](prop,
new [Link]() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
});
//compose the message
try{
MimeMessage message = new MimeMessage(session);
[Link](new InternetAddress(from));
[Link]([Link],new InternetAddress(to));
[Link]("SMTP Protocol Example");
[Link]("Hello, this is example of sending email... - Prasad ");
// Send message
[Link](message);
[Link]("message sent successfully....");
}catch (MessagingException mex) {[Link]();}
10 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Output:
Compiling :
Executing :
Open your Gmail and check the email received…
Here’s the email body…
11 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Program 10:
AIM: Implementation of FTP.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class FTPDownloadFileDemo {
public static void main(String[] args) {
String server = "localhost";
int port = 21;
String user = "user";
String pass = "pass";
FTPClient ftpClient = new FTPClient();
try {
[Link](server, port);
[Link](user, pass);
[Link]();
[Link](FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/test/[Link]";
File downloadFile1 = new File("C:/javap/[Link]");
12 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
OutputStream outputStream1 = new
BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = [Link](remoteFile1,
outputStream1);
[Link]();
if (success) {
[Link]("File #1 has been downloaded
successfully.");
}
// APPROACH #2: using InputStream
retrieveFileStream(String)
String remoteFile2 = "/test/[Link]";
File downloadFile2 = new File("C:/javaps/[Link]");
OutputStream outputStream2 = new BufferedOutputStream(new
FileOutputStream(downloadFile2));
InputStream inputStream = [Link](remoteFile2);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = [Link](bytesArray)) != -1) {
[Link](bytesArray, 0, bytesRead);
}
success = [Link]();
if (success) {
[Link]("File #2 has been downloaded
successfully.");
}
[Link]();
[Link]();
} catch (IOException ex) {
13 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
[Link]("Error: " + [Link]());
[Link]();
} finally {
try {
if ([Link]()) {
[Link]();
[Link]();
}
} catch (IOException ex) {
[Link]();
}
}
}
}
Output :
Process :
Download Filezilla software and proceed with following settings :
Step 1:
14 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Step 2:
Step 3: Clcik on Add User
15 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Step 4:
Step 5: Check Password option and type password ->
Step 6:
Specify the location of file to be downloaded in the program… and to compile
and execute the program you need the package :
.. download from the internet ..
After download compile and run your program as specified in the screenshot
below
16 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
17 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Program 11:
AIM: Implementation of HTTP.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HttpDemo {
public static void main(String args[]) throws IOException {
URL url = new URL("[Link]
HttpURLConnection con = (HttpURLConnection) [Link]();
[Link]("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(
[Link]()));
String inputLine;
while ((inputLine = [Link]()) != null) {
[Link](inputLine);
}
[Link]();
}
18 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Output :
19 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Program: 12
AIM: Implementation of RSA Algorithm.
import [Link].*;
import [Link].*;
class RSA
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int p,q,n,z,d=0,e,i;
[Link]("Enter the number to be encrypted and
decrypted");
int msg=[Link]();
double c;
BigInteger msgback;
[Link]("Enter 1st prime number p");
p=[Link]();
[Link]("Enter 2nd prime number q");
q=[Link]();
n=p*q;
z=(p-1)*(q-1);
[Link]("the value of z = "+z);
for(e=2;e<z;e++)
{
if(gcd(e,z)==1) // e is for public key exponent
{
break;
}
}
[Link]("the value of e = "+e);
20 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
for(i=0;i<=9;i++)
{
int x=1+(i*z);
if(x%e==0) //d is for private key exponent
{
d=x/e;
break;
}
}
[Link]("the value of d = "+d);
c=([Link](msg,e))%n;
[Link]("Encrypted message is : -");
[Link](c);
//converting int value of n to BigInteger
BigInteger N = [Link](n);
//converting float value of c to BigInteger
BigInteger C = [Link](c).toBigInteger();
msgback = ([Link](d)).mod(N);
[Link]("Derypted message is : -");
[Link](msgback);
static int gcd(int e, int z)
{
if(e==0)
return z;
else
return gcd(z%e,e);
}
}
21 PREPARED BY R.S.V.V. PRASAD RAO
NETWORK PROGRAMMING LAB MANUAL III-II C.S.E.
Output :
22 PREPARED BY R.S.V.V. PRASAD RAO