WIRESHARK

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Amrita School of Engineering

Amrita Vishwa Vidyapeetham


Chennai Campus
Department of ECE
COMPUTER NETWORKS -19ECE311

Name of the Student MYLAVARAPU SRIPRITHAM

Registration Number CH.EN.U4ECE20028

Name of the Experiment JAVA PROGRAMS

Exp Number 1

Software Used WIRESHARK AND ECLIPSE

Date of the Experiment 17-04-2023


done

Date of report 28-04-2023


submission
WIRESHARK SIMULATIONS
DNS

HEADER FORMAT
HTTP

HEADER FORMAT
SNMP HEADER FORMAT
IPV4

IPV4 HEADER FORMAT


TCP

TCP HEADER FORMAT


ARP

SMTP
FTP

File transfer protocol (FTP) is a network protocol that allows users to exchange files

over the internet with ease. Although there are better, more secure alternatives to

FTP, website administrators often use it to manage the files uploaded to their website’s

server. We also use FTP to share publicly available resources, automate file transfer

tasks, download services online and more. That said, FTP isn’t secure and is highly

vulnerable to malicious attacks so you shouldn’t use it to transfer sensitive files.

A user can store files on what is known as an FTP server, and other users can connect

to that server and interact with its contents. To use FTP, you must have a working

internet connection and you’ll need to download an FTP client with which you’ll

interact to upload files to and download from the server. You can use a command-

line FTP client instead if you’re feeling brave. You’ll also need to know the FTP

server’s IP address or host name.

How Does FTP Work?

The FTP server:The server is where all of the files are stored. You can interact with the

server to upload new files, download the files that are stored in it as well as move,

delete and perform other operations on the stored files. In order to communicate with

the FTP server, you’ll need an FTP client.


THE FTP CLIENT:
The FTP client is an application that allows users to communicate and interact with the

FTP server. To connect to a server, the user will need to know the server’s IP address

(or the hostname). Once the user has the server’s address, they can create a connection

to the server using the FTP client. Users are generally required to log in with a

username and password in order to access the serve

FTP requires two communication channels to work: a control channel (also known as

command channel) and a data channel. Since FTP uses two channels, it requires two

ports, one for the data channel (port 20) and one for the control channel (port 21). FTP

uses transmission control protocol (TCP) for transport needs.


FTP SERVER
package FTP;

import java.io.*;
import java.net.*;

public class FTP_server {

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


ServerSocket s=new ServerSocket(6666);
Socket sr=s.accept();
FileInputStream fr=new FileInputStream("C:\\Users\\HP\\OneDrive\\
Documents\\PRITHAM_20028_FTP.txt");
byte []b=new byte [20002];
fr.read(b, 0, b.length);
OutputStream os=sr.getOutputStream();
os.write(b, 0, b.length);
s.close();
fr.close();

FTP CLIENT
package FTP;

import java.net.*;
import java.io.*;

@SuppressWarnings("unused")
public class ftp_client {

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


Socket sr= new Socket("localhost", 6666);
InputStream is=sr.getInputStream();
FileOutputStream fr=new FileOutputStream("C:\\Users\\HP\\OneDrive\\
Documents\\NUZVID_20028_FTP.txt");
byte []b=new byte[20002];
is.read(b,0,b.length);
fr.write(b,0,b.length);

}
TCP/IP PROTOCOL
TCP/IP is a suite of protocols used by devices to communicate over the Internet
and most local networks. It is named after two of it’s original protocols—the
Transmission Control Protocol (TCP) and the Internet Protocol (IP). TCP provides
apps a way to deliver (and receive) an ordered and error-checked stream of
information packets over the network. The User Datagram Protocol (UDP) is used
by apps to deliver a faster stream of information by doing away with error-
checking. When configuring some network hardware or software, you may need to
know the difference.

Both TCP and UDP are protocols used for sending bits of data known as packets
over the Internet. Both protocols build on top of the IP protocol. In other words,
whether you’re sending a packet via TCP or UDP, that packet is sent to an IP
address. These packets are treated similarly, as they’re forwarded from your
computer to intermediary routers and on to the destination.

HOW TCP WORKS

TCP is the most commonly used protocol on the Internet.

When you request a web page in your browser, your computer sends TCP packets
to the web server’s address, asking it to send the web page back to you. The web
server responds by sending a stream of TCP packets, which your web browser
stitches together to form the web page. When you click a link, sign in, post a
comment, or do anything else, your web browser sends TCP packets to the server
and the server sends TCP packets back.

TCP is all about reliability—packets sent with TCP are tracked so no data is lost or
corrupted in transit. This is why file downloads don’t become corrupted even if
there are network hiccups. Of course, if the recipient is completely offline, your
computer will give up and you’ll see an error message saying it can’t communicate
with the remote host.

TCP achieves this in two ways. First, it orders packets by numbering them.
Second, it error-checks by having the recipient send a response back to the sender
saying that it has received the message. If the sender doesn’t get a correct response,
it can resend the packets to ensure the recipient receives them correctly.

HOW UDP WORKS

The UDP protocol works similarly to TCP, but it throws out all the error-checking
stuff. All the back-and-forth communication introduce latency, slowing things
down.

When an app uses UDP, packets are just sent to the recipient. The sender doesn’t
wait to make sure the recipient received the packet—it just continues sending the
next packets. If the recipient misses a few UDP packets here and there, they are
just lost—the sender won’t resend them. Losing all this overhead means the
devices can communicate more quickly.

CLIENT
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
Server
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

Udp Server
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {


public static void main(String a[]) throws Exception
{
DatagramSocket ds = new DatagramSocket(9999); //server port which receives
the client port address
byte[] b1 = new byte[1024];
DatagramPacket dp = new DatagramPacket(b1, b1.length); //dp [data and length
of the data] is initialiazing
ds.receive(dp); //receives the client data

String str = new String(dp.getData(),0,dp.getLength()); //the received data


is stored as a string
System.out.println("str"+str);
int num = Integer.parseInt(str.trim()); //the str is converted into a
integer and stored
System.out.println("num"+num);
int result = num*num;
byte[] b2 = String.valueOf(result).getBytes(); //result is converted into a
string
InetAddress ia = InetAddress.getLocalHost(); //server address is stored
DatagramPacket dp1 = new DatagramPacket(b2, b2.length, ia, dp.getPort());
//// storing the data, length of data, sender port and receiver port address in dp
ds.send(dp1); //sending the server data
}
}

Client
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {


public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();

int i=2; //inserting the number to be operated


byte[] b = String.valueOf(i).getBytes(); //getting the byte of that number

InetAddress ia = InetAddress.getLocalHost(); //getting address of the port


DatagramPacket dp = new DatagramPacket(b,b.length,ia,9999); // storing the
data, length of data, sender port and receiver port address in dp
ds.send(dp); //sending it to server

byte[] b1 = new byte[1024]; //decoding the sent data


DatagramPacket dp1 = new DatagramPacket(b1, b1.length); //dp1 is
initializing
ds.receive(dp1); //receving the data from server

String str = new String(dp1.getData(),0,dp1.getLength());


System.out.println("resut is "+str);
}
}
Result

STOP AND WAIT PROTOCOL

It is the simplest flow control method. In this, the sender will transmit one frame at
a time to the receiver. The sender will stop and wait for the acknowledgement from
the receiver.

This time (i.e. the time joining message transmitting and acknowledgement
receiving) is the sender’s waiting time, and the sender is idle during this time.

When the sender gets the acknowledgement (ACK), it will send the next data
packet to the receiver and wait for the disclosure again, and this process will
continue as long as the sender has the data to send.

While sending the data from the sender to the receiver, the data flow needs to be
controlled. If the sender is transmitting the data at a rate higher than the receiver
can receive and process it, the data will get lost.
The Flow-control methods will help in ensuring that the data doesn't get lost. The
flow control method will check that the senders send the data only at a rate that the
receiver can receive and process.

The working of Stop and Wait Protocol is shown in the figure below −

The main advantage of stop & wait protocols is their accuracy. The next frame is
transmitted only when the first frame is acknowledged. So there is no chance of the
frame being lost.

The drawback of this approach is that it is inefficient. It makes the transmission


process slow. An individual frame travels from source to destination in this
method, and a single acknowledgement travels from destination to source. As a
result, each frame sent and received uses the entire time needed to traverse the link.
Moreover, if two devices are a distance apart, a lot of time is wasted waiting for
ACKs leading to an increase in total transmission time.
Stop and wait Sender
import java.io.*;
import java.net.*;
public class Sender{
Socket sender;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,str, msg;
int n,i=0,sequence=0;
Sender(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for Connection....");
sender = new Socket("localhost",2005);
sequence=0;
out=new ObjectOutputStream(sender.getOutputStream());
out.flush();
in=new ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
System.out.println("reciver > "+str);
System.out.println("Enter the data to send....");
packet=br.readLine();
n=packet.length();
do{
try{

if(i<n){
msg=String.valueOf(sequence);
msg=msg.concat(packet.substring(i,i+1));
}else if(i==n){
msg="end";out.writeObject(msg);break;
}out.writeObject(msg);
sequence=(sequence==0)?1:0;
out.flush();
System.out.println("data sent>"+msg);
ack=(String)in.readObject();
System.out.println("waiting for ack.....\n\n");
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println("receiver > "+" packet recieved\n\n");
}else{
System.out.println("Time out resending data....\n\n");
sequence=(sequence==0)?1:0;
}}catch(Exception e){}
}while(i<n+1);
System.out.println("All data sent. exiting.");
}catch(Exception e){}
finally{
try{
in.close();
out.close();
sender.close();
}
catch(Exception e){}
}}
public static void main(String args[]){
Sender s=new Sender();
s.run();
}}

Receiver
import java.io.*;
import java.net.*;
public class Receiver{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Receiver(){}
public void run(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
reciever = new ServerSocket(2005,10);
System.out.println("waiting for connection...");
connection=reciever.accept();
sequence=0;
System.out.println("Connection established :");
out=new ObjectOutputStream(connection.getOutputStream());
out.flush();
in=new ObjectInputStream(connection.getInputStream());
out.writeObject("connected .");

do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(packet.substring(0,1))==sequence){
data+=packet.substring(1);
sequence=(sequence==0)?1:0;

System.out.println("\n\nreceiver >"+packet);
}
else
{
System.out.println("\n\nreceiver >"+packet +" duplicate data");
}if(i<3){
out.writeObject(String.valueOf(sequence));i++;
}else{
out.writeObject(String.valueOf((sequence+1)%2));
i=0;
}}
catch(Exception e){}
}while(!packet.equals("end"));
System.out.println("Data recived="+data);
out.writeObject("connection ended .");
}catch(Exception e){}
finally{
try{in.close();
out.close();
reciever.close();
}
catch(Exception e){}
}}

public static void main(String args[]){


Receiver s=new Receiver();
while(true){
s.run();
}
}
}

Result
Go-Back-N
Go-Back-N protocol, also called Go-Back-N Automatic Repeat request, is a data
link layer protocol that uses a sliding window method for reliable and sequential
delivery of data frames. It is a case of sliding window protocol having to send
window size of N and receiving window size of 1

Working Principle
Go – Back – N ARQ provides for sending multiple frames before receiving the
acknowledgment for the first frame. The frames are sequentially numbered and a
finite number of frames. The maximum number of frames that can be sent depends
upon the size of the sending window. If the acknowledgment of a frame is not
received within an agreed upon time period, all frames starting from that frame are
retransmitted.
The size of the sending window determines the sequence number of the outbound
frames. If the sequence number of the frames is an n-bit field, then the range of
sequence numbers that can be assigned is 0 to 2n−1. Consequently, the size of the
sending window is 2n−1. Thus in order to accommodate a sending window size of
2n−1, a n-bit sequence number is chosen.

The sequence numbers are numbered as modulo-n. For example, if the sending
window size is 4, then the sequence numbers will be 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, and
so on. The number of bits in the sequence number is 2 to generate the binary
sequence 00, 01, 10, 11.

The size of the receiving window is 1.

Go back n Client
import java.util.*;
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value of m :");
int m=Integer.parseInt(br.readLine());
int x=(int)((Math.pow(2,m))-1);
System.out.print("Enter no. of frames to be sent:");
int count=Integer.parseInt(br.readLine());
int data[]=new int[count];
int h=0;
for(int i=0;i<count;i++)
{
System.out.print("Enter data for frame no " +h+ " => ");
data[i]=Integer.parseInt(br.readLine());
h=(h+1)%x;
}
Socket client=new Socket("localhost",6262);
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
System.out.println("Connected with server.");
boolean flag=false;
GoBackNListener listener=new GoBackNListener(ois,x);
listener=new GoBackNListener(ois,x);
listener.t.start();
int strt=0;
h=0;
oos.writeObject(x);
do
{
int c=h;
for(int i=h;i<count;i++)
{
System.out.print("|"+c+"|");
c=(c+1)%x;
}
System.out.println();
System.out.println();
h=strt;
for(int i=strt;i<x;i++)
{
System.out.println("Sending frame:"+h);
h=(h+1)%x;
System.out.println();
oos.writeObject(i);
oos.writeObject(data[i]);
Thread.sleep(100);
}
listener.t.join(3500);
if(listener.reply!=x-1)
{
System.out.println("No reply from server in 3.5 seconds. Resending data from frame
no " + (listener.reply+1));
System.out.println();
strt=listener.reply+1;
flag=false;
}
else
{
System.out.println("All elements sent successfully. Exiting");
flag=true;
}
}while(!flag);
oos.writeObject(-1);
}
}

class GoBackNListener implements Runnable


{
Thread t;
ObjectInputStream ois;
int reply,x;
GoBackNListener(ObjectInputStream o,int i)
{
t=new Thread(this);
ois=o;
reply=-2;
x=i;
}
@Override
public void run() {
try
{
int temp=0;
while(reply!=-1)
{
reply=(Integer)ois.readObject();
if(reply!=-1 && reply!=temp+1)
reply=temp;
if(reply!=-1)
{
temp=reply;
System.out.println("Acknowledgement of frame no " + (reply%x) + " recieved.");
System.out.println();
}
}
reply=temp;
}
catch(Exception e)
{
System.out.println("Exception => " + e);
}
}
}

Server
import java.net.*;
import java.io.*;
import java.util.*;
public class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket server=new ServerSocket(6262);
System.out.println("Server established.");
Socket client=server.accept();
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
System.out.println("Client is now connected.");
int x=(Integer)ois.readObject();
int k=(Integer)ois.readObject();
int j=0;
int i=(Integer)ois.readObject();
boolean flag=true;
Random r=new Random(6);
int mod=r.nextInt(6);
while(mod==1||mod==0)
mod=r.nextInt(6);
while(true)
{
int c=k;
for(int h=0;h<=x;h++)
{
System.out.print("|"+c+"|");
c=(c+1)%x;
}
System.out.println();
System.out.println();
if(k==j)
{
System.out.println("Frame "+k+" recieved"+"\n"+"Data:"+j);
j++;
System.out.println();
}

else
System.out.println("Frames recieved not in correct order"+"\n"+" Expected farme:" +
j +"\n"+ " Recieved frame no :"+ k);
System.out.println();
if(j%mod==0 && flag)
{
System.out.println("Error found. Acknowledgement not sent. ");
flag=!flag;
j=j-1;
}
else if(k==j-1)
{
oos.writeObject(k);
System.out.println("Acknowledgement sent");
}
System.out.println();
if(j%mod==0)
flag=!flag;
k=(Integer)ois.readObject();
if(k==-1)
break;
i=(Integer)ois.readObject();
}
System.out.println("Client finished sending data. Exiting");
oos.writeObject(-1);
}
}

Result
INFERENCE

FTP (File Transfer Protocol) can be implemented in Java using the Apache
Commons Net library, which provides a set of classes for communicating with FTP
servers. The FTPClient class can be used to connect to an FTP server, authenticate
with a username and password, upload and download files, and perform other FTP
operations.TCP (Transmission Control Protocol) can be implemented in Java using
the java.net.Socket and java.net.ServerSocket classes, which provide a set of low-
level networking APIs for creating and managing TCP connections. The Socket class
can be used to establish a TCP connection to a remote host, while the ServerSocket
class can be used to listen for incoming TCP connections on a local port.UDP (User
Datagram Protocol) can be implemented in Java using the java.net.DatagramSocket
class, which provides a set of APIs for sending and receiving UDP packets. The
DatagramSocket class can be used to create a UDP socket, bind it to a local port, and
send and receive packets using the DatagramPacket class.Go-Back-N protocol and
Stop-and-Wait protocol can be implemented in Java using the java.net.Socket and
java.net.ServerSocket classes, along with a custom protocol implementation that
implements the sliding window algorithm. The sender can use a sliding window to
keep track of the packets it has sent and the receiver can use a sliding window to
keep track of the packets it has received. The sender can resend packets if they are
not acknowledged within a specified time period, and the receiver can acknowledge
packets as they are received, and send cumulative acknowledgments for packets
received in order. I have successfully done java programming and achieved proper
outputs.

You might also like