0% found this document useful (0 votes)
19 views

DCCN 6 To 9

The program implements transmission control protocol and user datagram protocol in Java. It creates server and client sockets to allow communication between them. Data is sent and received with input/output streams. Acknowledgements are sent back for received frames.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

DCCN 6 To 9

The program implements transmission control protocol and user datagram protocol in Java. It creates server and client sockets to allow communication between them. Data is sent and received with input/output streams. Acknowledgements are sent back for received frames.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS

SCIENCE

EX. NO: 6
INSTALLATION AND CONFIGURE THE NET ANIM
DATE:

AIM:

To Write a Network Simulator Program to install and configure NET ANIM.


ALGORITHM:

1. Install Mercurial

2. Install QT4 development package:

3. Build Net Anim:

4. To view the animation on NetAnim compile the code with NET ANIM.

5. To run the code:


a. Move the waf , waf.bat , wscript and wutils.py les in to the scratch folder (~/ns- allinone-
3.24/ns-3.24/scratch/).

b. Move the example code to the scratch folder and make the changes required for NetAnim,as
shown above.
c. Now cd to the scratch folder (cd ~/ns-allinone-3.24/ns-3.24/scratch/).

d. Run the code using the command: ./ waf –run

6. To visualize on NetAnim:

• cd to the netanim folder (cd ~/netanim/).

• Run Netanim: ./NetAnim

• Include the .xml le generated in the ns-3.24 folder.

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

PROGRAM:

# Create a simulator object set ns [newSimulator]


# Define different colors
# for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

# Open the NAM trace file set nf [open


out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure proc finish {} {
global ns nf
$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file exec namout.nam &
exit 0 }

# Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail ns duplex-link $n2 $n3 1.7Mb 20ms DropTail

# Set Queue Size of link (n2-n3) to 10


$ns queue-limit $n2 $n3 10
# Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

# Monitor the queue for link (n2-n3). (for NAM)


$ns duplex-link-op $n2 $n3 queuePos 0.5
# Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2

$nsattach-agent $n0 $tcp

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink


$ns connect $tcp $sink
$tcp set fid_ 1

# Setup a FTP over TCP connection set ftp [new Application/FTP]


$ftp attach-agent $tcp
$ftp set type_ FTP

# Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1
$udp set null [new Agent/Null]

$ns attach-agent $n3 $null


$ns connect $udp $null
$udp set fid_ 2
# Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
# Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
# Detach tcp and sink agents

# (not really necessary)


$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" # Call the
finish procedure after
puts "CBR interval = [$cbr set interval_]"

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

# 5 seconds of simulation time

$ns at 5.0 "finish"

# Run the simulation

$ns run

OUTPUT:

PREPARATION 30

LAB PERFOMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

RESULT:

Thus, the program for Network Simulator Program to install and configure NET ANIM has been
executed successfully and its output is verified.

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

EX. NO: 7A IMPLEMENTATION OF TRANSMISSION CONTROL PROTOCOL AND


DATE: USER DATAGRAM PROTOCOL

AIM:
To implement transmission control protocol and user datagram protocol using Java.

ALGORITHM:

1. Server Setup:
o Create a ServerSocket on a specified port.
o Accept incoming client connections using accept() method.
o Create input and output streams for communication.
2. Client Setup:
o Create a Socket with the server's IP address and port.
o Initialize input and output streams for data exchange.
3. Data Exchange:
o Client sends data to server using output stream.
o Server receives data from client using input stream.
o Optionally, server responds to client requests.
4. Loop:
o Repeat data exchange until termination condition is met (e.g., client closes connection).
5. Cleanup:
o Close input/output streams and sockets after communication ends.

PROGRAM:
Sender.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class sender {
public static void main(String args[])
{
int p=9000,i,q=8000;
String h="localhost";
try
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of frames : ");
int number = scanner.nextInt();
if(number==0)
{
System.out.println("No frame is sent");
}
else
{
Socket s2;
s2= new Socket(h,q);
717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

DataOutputStream d1 = new DataOutputStream(s2.getOutputStream());


d1.write(number);
}
String str1;
for (i=0;i<number;i++)
{
System.out.print("Enter message : ");
String name = scanner.next();
System.out.println("Frame " + i+" is sent");
Socket s1;
s1= new Socket(h,p+i);
DataOutputStream d = new DataOutputStream(s1.getOutputStream());
d.writeUTF(name);
DataInputStream dd= new DataInputStream(s1.getInputStream());
Integer sss1 = dd.read();
System.out.println("Ack for :" + sss1 + " is received");
}
}
catch(Exception ex)
{
System.out.println("ERROR :"+ex);
}
}
}

OUTPUT:

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

Reciever.java
import java.io.*;
import java.net.*;
import java.util.*;

public class receiver {


public static void main(String args[])
{
String h="Serverhost";
int q=5000;
int i;
try
{
ServerSocket ss2;
ss2 = new ServerSocket(8000);
Socket s1 =ss2.accept();
DataInputStream dd1= new DataInputStream(s1.getInputStream());
Integer i1 =dd1.read();
for(i=0;i<i1;i++)
{
ServerSocket ss1;
ss1 = new ServerSocket(9000+i);
Socket s =ss1.accept();
DataInputStream dd= new DataInputStream(s.getInputStream());
String sss1 = dd.readUTF();
System.out.println(sss1);
System.out.println("Frame "+ i+" received");
DataOutputStream d1 = new DataOutputStream(s.getOutputStream());
d1.write(i);
System.out.println("ACK sent for "+ i);
}
}
catch(Exception ex)
{
System.out.println("Error"+ex);
}
}
}
OUTPUT:

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

PREPARATION 30

LAB PERFOMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

RESULT:
Thus, the implementation of transmission control protocol and user datagram protocol using Java is done
successfully.

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
EX. NO: 7B
IMPLEMENTATION OF TRANSMISSION CONTROL PROTOCOL AND USER
DATE: DATAGRAM PROTOCOL

AIM:
To implement transmission control protocol and user datagram protocol using Java.

ALGORITHM:

1. Server Setup:
a. Get ready to listen for messages on a specific phone line (port).
b. Keep an ear out for incoming messages.
2. Client Setup:
a. Get ready to send messages.
b. Know who you want to send the message to (server's address) and on which "phone line" (port).
3. Sending and Receiving Messages:
a. Clients send messages to the server.
b. Servers listen for messages and receive them from clients.
4. Continual Communication:
a. Both clients and server can keep sending messages back and forth as needed.
5. Handling Errors:
a. If something goes wrong (like a message doesn't get through), figure out what happened and try
again if necessary.
6. Wrap-Up:
a. Once the conversation is over, hang up the phone (close the connection).

PROGRAM:

Server.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer
{
public static void main(String[] args) throws IOException
{
DatagramSocket ds = new DatagramSocket(1234);
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
while (true)
{
DpReceive = new DatagramPacket(receive, receive.length);
ds.receive(DpReceive);
System.out.println("Client:-" + data(receive));
if (data(receive).toString().equals("bye"))

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
{
System.out.println("Client sent bye..... EXITING");
break;
}
receive = new byte[65535];
}
}
public static StringBuilder data(byte[] a)
{
if (a == null)
return null;
StringBuilder ret = new StringBuilder();
int i = 0;
while (a[i] != 0)
{
ret.append((char) a[i]);
i++;
}
return ret;
}
}

OUTPUT:

Client.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClient


{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
while (true)
{
String inp = sc.nextLine();
buf = inp.getBytes();
717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1234);
ds.send(DpSend);
if (inp.equals("bye"))
break;
}
}
}

OUTPUT:

PREPARATION 30

LAB PERFOMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

RESULT:
Thus, the implementation of transmission control protocol and user datagram protocol using Java is done
successfully.

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
EX. NO: 8

IMPLEMENTATION OF FILE TRANSFER PROTOCOL


DATE:

AIM:
To implement file transfer protocol using java.

ALGORITHM:

1. Establish Connection:
a. The client initiates a TCP connection to the server on port 21 (FTP control port).
b. The server responds with a welcome message indicating readiness.
2. Authentication:
a. The client sends authentication credentials (username and password) to the server.
b. The server verifies the credentials. If valid, the server allows access; otherwise, it denies access.
3. Change Working Directory (optional):
a. The client can request to change the working directory on the server using commands like CWD.
4. Specify Transfer Mode (optional):
a. The client can specify the transfer mode (ASCII or binary) using commands like TYPE.
5. Data Transfer:
a. For file uploads, the client initiates a data connection (usually on port 20) to the server.
b. The client sends the file data over the data connection.
c. For file downloads, the server initiates a data connection to the client, and the server sends the file
data over the data connection.
6. Transfer Completion:
a. After the file transfer is complete, the server and client close the data connection.
7. Close Connection:
a. Once all transfers are complete, either the client or the server sends the QUIT command to terminate
the FTP session.
b. The server responds with a farewell message and closes the connection.

PROGRAM:

Server.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class FTPServer {


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

ServerSocket server=new ServerSocket(5000);

System.out.println("Server started");
System.out.println("Waiting for a client ...");

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
Socket socket = server.accept();

System.out.println("Client has joined sharing ");

DataInputStream readInput = new DataInputStream(socket.getInputStream());


DataOutputStream writeOutput = new DataOutputStream(socket.getOutputStream());

Scanner ip = new Scanner(System.in)

String lines=readInput.readUTF();

FileWriter myWriter = new FileWriter("D:\\GOD.txt");


myWriter.write(lines);
myWriter.close();

server.close();
socket.close();
writeOutput.close();
readInput.close();
ip.close();

}
}

OUTPUT:

Client.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.net.Socket;
import java.util.Scanner;

public class FTPClient {


public static void main(String arg[]) throws Exception {

System.out.println("Attempting to connect...");

Socket socket=new Socket("localhost",5000);


DataInputStream readInput = new DataInputStream(socket.getInputStream());
DataOutputStream writeOutput = new DataOutputStream(socket.getOutputStream());
717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
Scanner ip=new Scanner(System.in);

System.out.println("Start transfering");

String transfer="";

BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("D:\\GOD.txt"));
String line = reader.readLine();
while (line != null) {
transfer=transfer+line+"\n";
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}

writeOutput.writeUTF(transfer);

System.out.println("Transfer done!\nClosing connection");

socket.close();
readInput.close();
writeOutput.close();
ip.close();

}
}

OUTPUT:

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

PREPARATION 30

LAB PERFOMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

RESULT:
Thus, the implementation of file transfer protocol using Java is done successfully.

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE
EX. NO: 9

DATE: IMPLEMENTATION OF DATA ENCRYPTION AND DECRYPTION

AIM:
To implement data encryption and decryption using Java.

ALGORITHM:
Encryption Algorithm:
1. Key Generation: Generate a secret encryption key.
2. Encryption: Use the encryption key to encrypt the plaintext message.
3. Output: Output the encrypted ciphertext.
Decryption Algorithm:
1. Key Retrieval: Obtain the decryption key (the same key used for encryption).
2. Decryption: Use the decryption key to decrypt the ciphertext message.
3. Output: Output the decrypted plaintext.

PROGRAM:
Encrypter.java
// Import Java libraries
import java.util.Scanner;

public class EncryptionCode {


public static void main(String[] args) {
// Create a Scanner object attached to the keyboard
Scanner in = new Scanner (System.in);
// Plain text message to be encrypted from user
System.out.print("Please enter something to be encrypted or the encrypted message: ");
String userMessage = in.nextLine();
System.out.print("Please enter an integer value between 1 and 6 for encryption key: ");
int userKey = in.nextInt();

// User error check for key


while (userKey > 6) {
System.out.print("Your key must be between 1 and 6.\nPlease enter an integer value between 1 and 6: ");
userKey = in.nextInt();
}

// Encryption or decryption processing


String message = userMessage;
int key = userKey;
char [] chars = message.toCharArray();
for(char i : chars) {
i += key;
System.out.print(i);
}
}
}

717822I153 – SENBAGAVALLI N
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA 21ID15-DATA COMMUNICATION AND COMPUTER NETWORKS
SCIENCE

OUTPUT:

PREPARATION 30

LAB PERFOMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

RESULT:
Thus, the implementation of data encryption and decryption using Java is done successfully.

717822I153 – SENBAGAVALLI N

You might also like