merged doc
merged doc
import java.lang.System;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.Random;
import java.util.*;
}
for (int i = 0; i < p; i++)
if (v[i] == -1) {
System.out.println("Request to retransmit
from packet no "
+ (i+1) + " again!!");
n = i;
out.write(n);
out.flush();
}
System.out.println();
v[n] = in.read();
System.out.println("Received frame is: " + v[n]);
System.out.println("quiting");
} catch (Exception e) {
System.out.println(e);
}
}
}
/* OUTPUT
Server Side:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
try
{
int a[] = { 30, 40, 50, 60, 70, 80, 90, 100 };
Serversocket = new ServerSocket(8011);
System.out.println("waiting for connection");
Socket client = Serversocket.accept();
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
System.out.println("The number of packets sent is:" +
a.length);
int y = a.length;
dos.write(y);
dos.flush();
int k = dis.read();
dos.write(a[k]);
dos.flush();
}
catch (IOException e)
{
System.out.println(e);
}
finally
{
try
{
dis.close();
dos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
/* OUTPUT
[sinhgad@localhost ~]$ su
Password:
[root@localhost sinhgad]# javac ser.java
[root@localhost sinhgad]# java ser
waiting for connection
The number of packets sent is:8
[root@localhost sinhgad]#
*/
Program:
import java.util.Scanner;
for(int i=0;i<32;i++){
if(i<mask){
defmask = defmask + "1";
}
else{
defmask = defmask + "0";
}
}
System.out.println(defmask);
String netid = new String();
for(int i=0;i<32;i++){
netid = netid +
(Integer.parseInt(""+binip.charAt(i))&Integer.parseInt(""+defmask.charAt(i)));
}
int p=-1;
System.out.println(netid);
String[] net = new String[4];
String[] def = new String[4];
for(int i=0;i<32;i++){
if(i%8==0){
p++;
net[p] = "";
def[p]="";
net[p] = net[p] + netid.charAt(i);
def[p] = def[p] + defmask.charAt(i);
}
else{
net[p] = net[p] + netid.charAt(i);
def[p] = def[p] + defmask.charAt(i);
}
}
System.out.println("Given IP : "+ip);
System.out.print("subnet mask :");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(def[i],2));
if(i!=3)
System.out.print(".");
}
System.out.println();
System.out.print("NetId : ");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(net[i],2));
if(i!=3)
System.out.print(".");
}
}
}*/
String temp = new String("00000000");
return temp.substring(binaryString.length())+ binaryString;
}
}
Output:
java -cp /tmp/Y7iGysNwAS subnetmask
enter ip address192.168.1.101
enter mask
255
192 168 1 101
CLASS C
IP in binary : 11000000101010000000000101100101
Default Mask :
241111111111111111111111111111111111000000101010000000000101100101
Given IP : 192.168.1.101
subnet mask :255.255.255.255
NetId : 192.168.1.101
#include <iostream>
#include<fstream>
#include <iomanip>
#include<string>
using namespace std;
int main() {
cout << "***** PACKET ANALYZER *****" << endl; // prints !!!Hello World!!!
string value, sr_no,time,source,destination,info,protocol,len;
int count=-1,i=0;
int choice;
do
{
ifstream file("data.csv");
//Reinitialize Counters
count=-1;
i=0;
cout<<"\nEnter which protocol packets you want to see"<<endl;
cout<<"1.IP\n2.UDP\n3.TCP\n4.Ethernet\n0Exit!!!\nChoice:"<<endl;
cin>>choice;
string protocolChoice; //sting to hold user packet choice
switch(choice){
case 1: protocolChoice="ICMPv6";
break;
case 2: protocolChoice="UDP";
break;
case 3: protocolChoice="TCP";
break;
case 4: protocolChoice="ARP";
break;
default: protocolChoice="ARP";
break;
}
while(file.good()) //LOOP UNTIL FILE HAS CONTENT
{
getline(file,sr_no,','); //GET STRING TILL ,
getline(file,time,',');
getline(file,source,',');
getline(file,destination,',');
getline(file,protocol,',');
getline(file,len,',');
getline(file,info,'\n');
protocol=string(protocol,1,protocol.length()-2);
if(protocol=="Protocol"||protocol==protocolChoice)
{
cout <<setw(4)<<left<<i++;
cout <<setw(12)<<left<< string( time, 1, time.length()-2 );
cout << setw(30)<<left<<string( source, 1, source.length()-2 );
cout << setw(30)<<left<<string( destination, 1, destination.length()-2
);
cout <<setw(8)<<left<<protocol;
cout <<setw(8)<<left<< string( len, 1, len.length()-2 );
cout << string( info, 1, info.length()-2 )<<"\n";
count++;
}
}
file.close();
cout<<"\nTotal Packet Count: "<<count;
}while(choice!=0);
return 0;
}
/* output:
* ***** PACKET ANALYZER *****
/* OUTPUT
}
catch (UnknownHostException ex) {
System.out.println("Could not find " + host);
}
}
}
}
/*OUTPUT
1.Enter Host Name
2.Enter IP address
Choice=1