Categories
TE Computer Engineering

13. Configure RIP/OSPF/BGP using packet Tracer.

Fig: OSPF Protocol Configuration



OSPF Router 0 Configuration

Router>enable
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface FastEthernet0/0
Router(config-if)#ip address 192.168.1.1 255.255.255.0
Router(config-if)#no shutdown

Router(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up

Router(config-if)#exit
Router(config)#interface Serial2/0
Router(config-if)#ip address 10.10.0.2 255.0.0.0
Router(config-if)#clock rate 64000
Router(config-if)#
Router(config-if)#exit
Router(config)#interface Serial3/0
Router(config-if)#ip address 12.12.0.2 255.0.0.0
Router(config-if)#clock rate 64000
Router(config-if)#no shutdown

%LINK-5-CHANGED: Interface Serial3/0, changed state to down
Router(config-if)#
%LINK-5-CHANGED: Interface Serial3/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial3/0, changed state to up

Router(config-if)#exit
Router(config)#interface Serial2/0
Router(config-if)#no shutdown

Router(config-if)#
%LINK-5-CHANGED: Interface Serial2/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial2/0, changed state to up
exit
Router(config)#router ospf 1
Router(config-router)#network 192.168.1.0 0.0.0.255 area 0
Router(config-router)#network 10.0.0.0 0.255.255.255 area 0
Router(config-router)#network 12.0.0.0 0.255.255.255 area 0
Router(config-router)#exi
Router(config)#exit
Router#
%SYS-5-CONFIG_I: Configured from console by console

00:08:39: %OSPF-5-ADJCHG: Process 1, Nbr 11.11.0.2 on Serial2/0 from LOADING to FULL, Loading Done

00:09:57: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.2.1 on Serial3/0 from LOADING to FULL, Loading Done

OSPF Router 1 configuration
outer>enable
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface Serial2/0
Router(config-if)#ip address 10.10.0.3 255.0.0.0
Router(config-if)#no shutdown

%LINK-5-CHANGED: Interface Serial2/0, changed state to down
Router(config-if)#
Router(config-if)#exit
Router(config)#interface Serial3/0
Router(config-if)#ip address 11.11.0.2 255.0.0.0
Router(config-if)#clock rate 64000
Router(config-if)#no shutdown

%LINK-5-CHANGED: Interface Serial3/0, changed state to down
Router(config-if)#
Router(config-if)#exit
Router(config)#interface Serial2/0
Router(config-if)#
Router(config-if)#exit
Router(config)#interface Serial3/0
Router(config-if)#
%LINK-5-CHANGED: Interface Serial3/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial3/0, changed state to up

Router(config-if)#exit
Router(config)#interface Serial2/0
Router(config-if)#
%LINK-5-CHANGED: Interface Serial2/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial2/0, changed state to up
exit
Router(config)#router ospf 1
Router(config-router)#network 10.0.0.0 0.255.255.255 area 0
Router(config-router)#network 10.0.0.0 0.255.255.255 area 0
00:08:37: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.1.1 on Serial2/0 from LOADING to FULL, Loading Done

Router(config-router)#network 11.0.0.0 0.255.255.255 area 0
Router(config-router)#exit
Router(config)#
00:09:38: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.2.1 on Serial3/0 from LOADING to FULL, Loading Done

OSPF Router 2 configuration

Router>enable
Router#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface FastEthernet0/0
Router(config-if)#ip address 192.168.2.1 255.255.255.0
Router(config-if)#no shutdown

Router(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up

Router(config-if)#exit
Router(config)#interface Serial2/0
Router(config-if)#ip address 11.11.0.3 255.0.0.0
Router(config-if)#no shutdown

Router(config-if)#
%LINK-5-CHANGED: Interface Serial2/0, changed state to up

Router(config-if)#exit
Router(config)#interface Serial3/0
Router(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial2/0, changed state to up
ip address 12.12.0.3 255.0.0.0
Router(config-if)#no shutdown

Router(config-if)#
%LINK-5-CHANGED: Interface Serial3/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial3/0, changed state to up
exit
Router(config)#router ospf 1
Router(config-router)#network 192.168.2.0 0.0.0.255 area 0
Router(config-router)#network 11.0.0.0 0.255.255.255 area 0
Router(config-router)#network 11.0.0.0 0.255.255.255 area 0
00:09:36: %OSPF-5-ADJCHG: Process 1, Nbr 11.11.0.2 on Serial2/0 from LOADING to FULL, Loading Done

Router(config-router)#network 12.0.0.0 0.255.255.255 area 0
Router(config-router)#exit
Router(config)#
00:09:53: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.1.1 on Serial3/0 from LOADING to FULL, Loading Done

Categories
TE Computer Engineering

12. Use network simulator NS2 to implement: a. Monitoring traffic for the given topology b. Analysis of CSMA and Ethernet protocols c. Network Routing: Shortest path routing, AODV. d. Analysis of congestion control (TCP and UDP).

Categories
TE Computer Engineering

11. Write a program using UDP sockets for wired network to implement a. Peer to Peer Chat b. Multiuser Chat Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

Multi user Chat
Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Client implements Runnable
{
static MulticastSocket clientSocket ;
static byte[] recvData = new byte[1024];
static String getsen=”hmmm”;
static boolean send_flag=false;
public static void main(String[] args)
{
// TODO Auto-generated method stub
BufferedReader br ;
try
{
clientSocket = new MulticastSocket(4447);
InetAddress group = InetAddress.getByName(“230.0.0.1”);
InetAddress IPAddr = InetAddress.getByName(“localhost”);
clientSocket.joinGroup(group);
byte[] sendData = new byte[1024];
String sentence=null;
new Thread(new Client()).start();
//SEND DATA
while(true)
{
sentence=null;
br= new BufferedReader(new InputStreamReader(System.in));
sentence=br.readLine();
sendData = new byte[1024];
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddr,9877);
clientSocket.send(sendPacket);
send_flag = true;
}

}
catch (SocketException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
//READ FROM SERVER
public void run()
{
// TODO Auto-generated method stub
DatagramPacket recvPacket;
while(!getsen.equals(“Bye”))
{
try
{
recvPacket = new DatagramPacket(recvData,recvData.length);
clientSocket.receive(recvPacket);
getsen = new String(recvPacket.getData());
if(send_flag!=true)
System.out.println(“BROADCASTED MESSAGE:”+getsen);
getsen= ” “;
send_flag = false;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

Server.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class Server
{
static byte[] recvData = new byte[1024];
static byte[] sendData = new byte[1024];
static DatagramSocket ds;
static InetAddress group;
public static void main(String[] args) { // TODO Auto-generated method stub
try {
ds = new DatagramSocket(9877);
group = InetAddress.getByName(“230.0.0.1”);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); DatagramPacket recvPack;
while(true) {
recvData = new byte[1024];
try {
recvPack = new DatagramPacket(recvData,recvData.length);
ds.receive(recvPack);
String sentence = new String(recvPack.getData());
System.out.println(sentence); method(sentence);
}
catch (IOException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
} public static void method(String sentence) { // TODO Auto-generated method stub sendData = sentence.getBytes();
DatagramPacket sendPack = new DatagramPacket(sendData,sendData.length,group,4447);
try {
ds.send(sendPack);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Peer to Peer Chat
Client.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class PeerClient {
public final static int PORT=8778;
public final static String SERVER_IP=”127.0.0.1″;
public static void main(String[] args) {
BufferedReader br=null;
DatagramSocket socket=null;
String s;
try {
socket=new DatagramSocket();
br=new BufferedReader(new InputStreamReader(System.in));
InetAddress server=InetAddress.getByName(SERVER_IP);
System.out.println(“Client Socket Created.\nENter msg:”);

while(true) {
System.out.print(“>”);
s=br.readLine();
byte[] sendMsg=s.getBytes();
DatagramPacket sendingPacket=new DatagramPacket(sendMsg, sendMsg.length,server,PORT);
socket.send(sendingPacket);
System.out.println(“Waiting for reply.”);
byte[] replyMsg=new byte[1000];
DatagramPacket receivingPacket=new DatagramPacket(replyMsg, replyMsg.length); socket.receive(receivingPacket);
byte[] data=receivingPacket.getData();
String s1=new String(data, 0,data.length); System.out.println(receivingPacket.getAddress().getHostAddress()+”:”+receivingPacket.getPort()+”=> “+s1);
if(s1.equalsIgnoreCase(“bye”)) { System.out.println(“Connection Closed”);
break;
}
} socket.close();
} catch (Exception e) { // TODO: handle exception } }}

Server.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class PeerServer {
public static void main(String[] args) {
DatagramSocket socket=null;
try { socket=new DatagramSocket(8778);
//server socket byte[] buffer=new byte[1000]; //buffer for msg DatagramPacket incomingPacket=new DatagramPacket(buffer, buffer.length); //packet to receive data System.out.println(“Server Socket Created.\nWaiting for data..\n”);

while(true) {
socket.receive(incomingPacket);
byte[] msg=incomingPacket.getData();
String s=new String(msg,0,msg.length); System.out.println(incomingPacket.getAddress().getHostAddress()+”:”+incomingPacket.getPort()+” > “+s);
if(s.equalsIgnoreCase(“bye”))
{ String reply=new String(“Bye”);
msg=reply.getBytes();
DatagramPacket sendPacket=new DatagramPacket(msg, msg.length,incomingPacket.getAddress(),incomingPacket.getPort()); socket.send(sendPacket); break;
}
else {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print(“>”);
s=br.readLine();
msg=s.getBytes();
DatagramPacket sendPacket=new DatagramPacket(msg, msg.length,incomingPacket.getAddress(),incomingPacket.getPort()); socket.send(sendPacket);
}
}
socket.close();
} catch(Exception e) { } }
}

Categories
TE Computer Engineering

10. Write a program using TCP sockets for wired network to implement a. Peer to Peer Chat

Download write-up here

//ClientCode.java
import java.io.*;
import java.net.*;

public class ClientCode
{
Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ClientCode()
    {
    try{

        Socket socket=new Socket(“127.0.0.1”,8080);
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {

        System.out.println(“Enter the msg”);
        str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        System.out.println(“Msg from Server”);
        str=sock_in.readLine();
        System.out.println(str);
        if(str.equals(“bye”))
        break;
        }
        //socket.close();
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ClientCode();
}
}

/* OUTPUT
unixlab@unixlab-Veriton-M200-H81:~$ javac ClientCode.java
unixlab@unixlab-Veriton-M200-H81:~$ java ClientCode
Enter the msg
hiiii
Msg from Server
hii,welcome to svit
Enter the msg
what r u doing?
Msg from Server
nothing
Enter the msg
ok bye
Msg from Server
bye
unixlab@unixlab-Veriton-M200-H81:~$
*/

//ServerCode.java
import java.io.*;
import java.net.*;

public class ServerCode
{
ServerSocket ss;
Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ServerCode()
    {
    try{
        ss=new ServerSocket(8080);
        System.out.println(“Server is listening port 8080”);
        socket=ss.accept();
        System.out.println(“Connection established…”);
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {
        System.out.println(“Msg from client”);
        str=sock_in.readLine();

        System.out.println(str);

        System.out.println(“Enter the msg”);
        str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        if(str.equals(“bye”))
        break;
        }
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ServerCode();
}
}

/* Output:
unixlab@unixlab-Veriton-M200-H81:~$ javac ServerCode.java
unixlab@unixlab-Veriton-M200-H81:~$ java ServerCode
Server is listening port 8080
Connection established…
Msg from client
hiiii
Enter the msg
hii,welcome to svit
Msg from client
what r u doing?
Enter the msg
nothing
Msg from client
ok bye
Enter the msg
bye
unixlab@unixlab-Veriton-M200-H81:~$
*/

Categories
TE Computer Engineering

9. Write a program for DNS lookup. Given an IP address input, it should return URL and vice-versa.

Download The write up here

/import java.net.*;
import java.util.*;

public class IPDemo
{
public static void main(String[] args){
String host;
Scanner ch = new Scanner(System.in);
System.out.print(“1.Enter Host Name \n2.Enter IP address \nChoice=”);
int choice = ch.nextInt();
if(choice==1)
{
Scanner input = new Scanner(System.in);
System.out.print(“\n Enter host name: “);
host = input.nextLine();
try {
InetAddress address = InetAddress.getByName(host);
System.out.println(“IP address: ” + address.getHostAddress());
System.out.println(“Host name : ” + address.getHostName()); 
System.out.println(“Host name and IP address: ” + address.toString());
}
catch (UnknownHostException ex) {
     System.out.println(“Could not find ” + host);
}
}
else
{
Scanner input = new Scanner(System.in);
System.out.print(“\n Enter IP address: “);
host = input.nextLine();
try {
InetAddress address = InetAddress.getByName(host);
System.out.println(“Host name : ” + address.getHostName());
System.out.println(“IP address: ” + address.getHostAddress());
System.out.println(“Host name and IP address: ” + address.toString());

}
catch (UnknownHostException ex) {
     System.out.println(“Could not find ” + host);
}
}

}
}
/*OUTPUT
iotlab@iotlab-Veriton-M200-B360:~$
javac IPDemo.java 
iotlab@iotlab-Veriton-M200-B360:~$
java IPDemo 
1.Enter Host Name 
2.Enter IP address 
Choice=1
 Enter host name: www.google.comIP
address: 172.217.160.196
Host name : www.google.com
Host name and IP address: www.google.com/172.217.160.196
iotlab@iotlab-Veriton-M200-B360:~$
java IPDemo 
1.Enter Host Name 
2.Enter IP address Choice=2
 Enter IP address: 8.8.8.8
Host name : dns.googleIP
address: 8.8.8.8
Host name and IP address: dns.google/8.8.8.8
iotlab@iotlab-Veriton-M200-B360:~$ */

Categories
TE Computer Engineering

8. Write a program to analyze following packet formats captured through Wireshark for wired network. 1. Ethernet 2. IP 3.TCP 4. UDP

Download The write up here

#include <iostream>
#include<fstream>
#include <iomanip>
#include<string>
using namespace std;

int main()
{
cout << “***** PACKET ANALYZER *****” << endl;
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 *****

Enter which protocol packets you want to see
1.IP
2.UDP
3.TCP
4.Ethernet
0Exit!!!
Choice:
1
0   Time        Source                        Destination                   ProtocolLength  Info
1   0.000000000 fe80::f68e:38ff:fe87:a57e     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from f4:8e:38:87:a5:7e
2   0.151808000 fe80::175:6553:3c34:d4f0      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from c8:1f:66:06:4a:84
3   0.245234000 fe80::208:a1ff:fe43:c3c2      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 00:08:a1:43:c3:c2
4   0.301527000 fe80::4046:d001:d60a:e934     ff02::1:ff00:1                ICMPv6  86      Neighbor Solicitation for fe80::1 from 00:25:64:92:4d:81
5   0.310878000 fe80::80a7:7d55:7ecf:5582     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 34:17:eb:9e:8e:45
6   0.382715000 fe80::104b:adee:75e6:c425     ff02::1:ff2f:e430             ICMPv6  86      Neighbor Solicitation for fe80::a490:6a6c:d52f:e430 from 00:19:d1:45:e9:4b
7   0.486747000 fe80::8e2:220e:db99:187f      ff02::2                       ICMPv6  70      Router Solicitation from c8:e0:eb:9e:44:9e
8   0.619047000 fe80::adb7:4c35:7a64:621e     ff02::1:ff18:d425             ICMPv6  86      Neighbor Solicitation for fe80::899f:4a1b:518:d425 from b8:ac:6f:68:65:68
9   0.621767000 fe80::25e2:1c6e:545d:d5ca     ff02::1:ff00:1                ICMPv6  86      Neighbor Solicitation for fe80::1 from f0:4d:a2:fd:b3:b3
10  0.879948000 fe80::6600:6aff:fe37:40d9     ff02::1:ff02:22f              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:22f from 64:00:6a:37:40:d9
11  0.943252000 fe80::4a4d:7eff:fec6:fe57     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 48:4d:7e:c6:fe:57
12  0.973236000 fe80::ad92:4946:c11e:bff0     ff02::1:ff00:1                ICMPv6  86      Neighbor Solicitation for fe80::1 from f4:8e:38:9d:86:5c
13  1.001717000 fe80::f68e:38ff:fe87:a57e     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from f4:8e:38:87:a5:7e
14  1.158015000 fe80::175:6553:3c34:d4f0      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from c8:1f:66:06:4a:84
15  1.164756000 fe80::90c7:9c8e:4162:743a     ff02::16                      ICMPv6  110     Multicast Listener Report Message v2
16  1.247232000 fe80::208:a1ff:fe43:c3c2      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 00:08:a1:43:c3:c2
17  1.299874000 fe80::4046:d001:d60a:e934     ff02::1:ff00:1                ICMPv6  86      Neighbor Solicitation for fe80::1 from 00:25:64:92:4d:81
18  1.334884000 fe80::80a7:7d55:7ecf:5582     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 34:17:eb:9e:8e:45
19  1.381157000 fe80::104b:adee:75e6:c425     ff02::1:ff2f:e430             ICMPv6  86      Neighbor Solicitation for fe80::a490:6a6c:d52f:e430 from 00:19:d1:45:e9:4b
20  1.410771000 fe80::adb7:4c35:7a64:621e     ff02::1:ff11:4e6f             ICMPv6  86      Neighbor Solicitation for fe80::5058:2741:6f11:4e6f from b8:ac:6f:68:65:68
21  1.422139000 fe80::ec3b:be3b:a1cf:b8dc     ff02::1:ff64:621e             ICMPv6  86      Neighbor Solicitation for fe80::adb7:4c35:7a64:621e from 28:d2:44:f6:d0:71
22  1.464011000 fe80::c2c9:76ff:fe50:72f9     ff02::2                       ICMPv6  70      Router Solicitation from c0:c9:76:50:72:f9
23  1.472534000 fe80::adb7:4c35:7a64:621e     ff02::1:ff1c:b39b             ICMPv6  86      Neighbor Solicitation for fe80::d107:c499:311c:b39b from b8:ac:6f:68:65:68
24  1.502391000 fe80::4a4d:7eff:feca:8004     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 48:4d:7e:ca:80:04
25  1.614264000 fe80::4a4d:7eff:fec6:ff33     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 48:4d:7e:c6:ff:33
26  1.639345000 fe80::221:9bff:fe6e:4b01      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from 00:21:9b:6e:4b:01
27  1.880789000 fe80::6600:6aff:fe37:40d9     ff02::1:ff02:22f              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:22f from 64:00:6a:37:40:d9
28  1.998620000 fe80::adb7:4c35:7a64:621e     ff02::1:ffa7:7fb2             ICMPv6  86      Neighbor Solicitation for fe80::a1fb:332b:83a7:7fb2 from b8:ac:6f:68:65:68
29  2.003773000 fe80::f68e:38ff:fe87:a57e     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from f4:8e:38:87:a5:7e
30  2.028027000 fe80::e298:61ff:fe35:9a26     ff02::1:ff64:621e             ICMPv6  86      Neighbor Solicitation for fe80::adb7:4c35:7a64:621e from e0:98:61:35:9a:26
31  2.040149000 fe80::f68e:38ff:fe87:a56a     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from f4:8e:38:87:a5:6a
32  2.107577000 fe80::b283:feff:fe4d:f1c9     ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from b0:83:fe:4d:f1:c9
33  2.162415000 fe80::90c7:9c8e:4162:743a     ff02::16                      ICMPv6  110     Multicast Listener Report Message v2
34  2.181982000 fe80::175:6553:3c34:d4f0      ff02::1:ff02:21a              ICMPv6  86      Neighbor Solicitation for fe80::726d:ecff:fe02:21a from c8:1f:66:06:4a:84

Total Packet Count: 34
Enter which protocol packets you want to see
1.IP
2.UDP
3.TCP
4.Ethernet
0Exit!!!
Choice:
2
0   Time        Source                        Destination                   ProtocolLength  Info

Total Packet Count: 0
Enter which protocol packets you want to see
1.IP
2.UDP
3.TCP
4.Ethernet
0Exit!!!
Choice:
3
0   Time        Source                        Destination                   ProtocolLength  Info
1   0.243260000 216.58.197.68                 10.10.14.151                  TCP     66      https > 51709 [FIN, ACK] Seq=1 Ack=1 Win=175 Len=0 TSval=2559300079 TSecr=23747257
2   0.438095000 108.168.177.14                10.10.13.238                  TCP     103     [TCP segment of a reassembled PDU]
3   0.746828000 192.168.16.254                10.10.10.28                   TCP     60      57777 > etftp [RST] Seq=1 Win=5840 Len=0
4   0.855756000 64.233.188.188                10.10.15.48                   TCP     97      hpvroom > 39687 [PSH, ACK] Seq=1 Ack=1 Win=175 Len=31 TSval=2933171628 TSecr=49981356
5   1.839024000 118.214.135.85                10.10.12.0                    TCP     60      https > 50976 [FIN, ACK] Seq=32 Ack=1 Win=980 Len=0
6   1.839028000 118.214.135.85                10.10.12.0                    TCP     60      https > 50977 [FIN, ACK] Seq=32 Ack=1 Win=980 Len=0
7   1.886438000 192.168.3.254                 192.168.3.211                 TCP     62      ndl-aas > fnet-remote-ui [SYN, ACK] Seq=0 Ack=1 Win=29200 Len=0 MSS=1460 SACK_PERM=1
8   1.888346000 192.168.3.254                 192.168.3.211                 TCP     60      ndl-aas > fnet-remote-ui [ACK] Seq=1 Ack=211 Win=30016 Len=0

Total Packet Count: 8
*/

Categories
TE Computer Engineering

7. Write a program using UDP Sockets to enable file transfer (Script, Text, Audio and Video one file each) between two machines. Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

Download The write up here

Client.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER “127.0.0.1”
#define BUFLEN 503
#define PORT 8885

void die(char *s)// used to quit
{
perror(s);
exit(1);
}
unsigned long fsize(char* file)
{
    FILE * f = fopen(file, “r”);
    fseek(f, 0, SEEK_END);//used to take the function pointer to the starting of the file or end of the file or to     any position specified by the user.
    unsigned long len = (unsigned long)ftell(f);// used to find out the position of file pointer in the file with  respect to starting of the file.
    fclose(f);
    return len;
}

int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];

if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die(“socket”);
}

memset((char *) &si_other, 0, sizeof(si_other));

si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);

if (inet_aton(SERVER , &si_other.sin_addr) == 0) //converts the specified string, in the Internet standard  dot notation, to a network address, and stores the address in  the structure provided.
{
fprintf(stderr, “inet_aton() failed\n”);
exit(1);
}

char fname[20];
printf(“Enter Filename with extension: “);
scanf(“%s”,&fname);//add.txt
sendto(s, fname, 20 , 0 , (struct sockaddr *) &si_other, slen);
memset(message,0,503);
unsigned long siz = fsize(fname);//fsize(add.txt)=20
printf(“%ld”,(siz % 503));//20%503=20
char str[10];
sprintf(str, “%d”, siz);
sendto(s, str, 20 , 0 , (struct sockaddr *) &si_other, slen);
FILE *f;
f=fopen(fname,”rb”);
memset(message,0,503);
fread(message, 503,1,f);
int itr =1;
while(itr*503<siz)
{
//fread(message, 503,1,f);
if (sendto(s, message, 503 , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die(“sendto()”);
}
memset(message,0,503);
fread(message, 503,1,f);
itr++;
}
fread(message, (siz % 503),1,f);
sendto(s, message, (siz % 503) , 0 , (struct sockaddr *) &si_other, slen);
memset(message,0,503);
fclose(f);
close(s);
return 0;
}

Server.c

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<arpa/inet.h>#include<sys/socket.h>
#define BUFLEN 503#define PORT 8885
void die(char *s){ perror(s); exit(1);}
int main(void){ struct sockaddr_in si_me, si_other; int s, i,j, slen = sizeof(si_other) , recv_len; char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { die(“socket”); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) { die(“bind”); } char fname[20]; FILE *fp; recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen); char fna[100]; memset(buf,0,503); recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen); strcpy(fna,buf); int len= strlen(fna); printf(“%d”,len);//20 for(j=len-1;j>=0;j–) { if(fna[j]==’.’) { fna[j-1]=’1′; } } unsigned long mm = atoi(buf); fp=fopen(fna,”wb”); int itr=1; memset(buf,0,503); while(itr*503<mm) { if ((recv_len = recvfrom(s, buf, 503, 0, (struct sockaddr *) &si_other, &slen)) == -1) { die(“recvfrom()”); } fwrite(buf,503, 1, fp); memset(buf,0,503); itr++; } printf(“%d”,(mm%503)); recv_len = recvfrom(s, buf, (mm%503), 0, (struct sockaddr *) &si_other, &slen); fwrite(buf,(mm%503), 1, fp); memset(buf,0,503); fclose(fp); close(s); return 0;}

Categories
TE Computer Engineering

6. Write a program using TCP socket for wired network for following a. Say Hello to Each other ( For all students) b. File transfer ( For all students) c. Calculator (Arithmetic) (50% students) d. Calculator (Trigonometry) (50% students).

Download The write up here

1. Simple Hello

Client.c
#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>
int main(){int clientSocket;char buffer[1024];struct sockaddr_in serverAddr;socklen_t addr_size;
/*—- Create the socket. The three arguments are: —-*//* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*—- Configure settings of the server address struct —-*//* Address family = Internet */serverAddr.sin_family = AF_INET;/* Set port number, using htons function to use proper byte order */serverAddr.sin_port = htons(7891);/* Set IP address to localhost */serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);/* Set all bits of the padding field to 0 */memset(serverAddr.sin_zero, ‘\0’, sizeof serverAddr.sin_zero);
/*—- Connect the socket to the server using the address struct —-*/addr_size = sizeof serverAddr;connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*—- Read the message from the server into the buffer —-*/recv(clientSocket, buffer, 1024, 0);
/*—- Print the received message —-*/printf(“Data received: %s”,buffer);
return 0;}

Server.c

#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>
int main(){int welcomeSocket, newSocket;char buffer[1024];struct sockaddr_in serverAddr;struct sockaddr_storage serverStorage;socklen_t addr_size;
/*—- Create the socket. The three arguments are: —-*//* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
/*—- Configure settings of the server address struct —-*//* Address family = Internet */serverAddr.sin_family = AF_INET;/* Set port number, using htons function to use proper byte order */serverAddr.sin_port = htons(7891);/* Set IP address to localhost */serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);/* Set all bits of the padding field to 0 */memset(serverAddr.sin_zero, ‘\0’, sizeof serverAddr.sin_zero);
/*—- Bind the address  struct to the socket —-*/bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*—- Listen on the socket, with 5 max connection requests queued —-*/if(listen(welcomeSocket,5)==0)printf(“Listening\n”);elseprintf(“Error\n”);
/*—- Accept call creates a new socket for the incoming connection —-*/addr_size = sizeof serverStorage;newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*—- Send message to the socket of the incoming connection —-*/strcpy(buffer,”Hello World\n”);send(newSocket,buffer,13,0);
return 0;}
/*OUTPUT CLIENTiotlab@iotlab-Veriton-M200-B360:~$ cd TCP\ Socket/iotlab@iotlab-Veriton-M200-B360:~/TCP Socket$ cd Simple\ Hello/iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Simple Hello$ gcc client_simple_hello.c -o clientclient_simple_hello.c: In function ‘main’:client_simple_hello.c:23:30: warning: implicit declaration of function ‘inet_addr’; did you mean ‘s6_addr’? [-Wimplicit-function-declaration] serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);                              ^~~~~~~~~                              s6_addriotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Simple Hello$ ./clientData received: Hello World

OUTPUT SERVERiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Simple Hello$ gcc server_simple_hello.c -o serverserver_simple_hello.c: In function ‘main’:server_simple_hello.c:24:30: warning: implicit declaration of function ‘inet_addr’; did you mean ‘s6_addr’? [-Wimplicit-function-declaration] serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);                              ^~~~~~~~~                              s6_addriotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Simple Hello$ ./serverListening*/

2. File transfer

Client.c

#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <arpa/inet.h>
int main(void){    int sockfd = 0;    int bytesReceived = 0;    char recvBuff[256];    memset(recvBuff, ‘0’, sizeof(recvBuff));    struct sockaddr_in serv_addr;
    /* Create a socket first */    if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)    {        printf(“\n Error : Could not create socket \n”);        return 1;    }
    /* Initialize sockaddr_in data structure */    serv_addr.sin_family = AF_INET;    serv_addr.sin_port = htons(5000); // port    serv_addr.sin_addr.s_addr = inet_addr(“172.16.6.168”);
    /* Attempt a connection */    if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)    {        printf(“\n Error : Connect Failed \n”);        return 1;    }
    /* Create file where data will be stored */    FILE *fp;    fp = fopen(“sample_file.txt”, “ab”);     if(NULL == fp)    {        printf(“Error opening file”);        return 1;    }
    /* Receive data in chunks of 256 bytes */    while((bytesReceived = read(sockfd, recvBuff, 256)) > 0)    {        printf(“Bytes received %d\n”,bytesReceived);            // recvBuff[n] = 0;        fwrite(recvBuff, 1,bytesReceived,fp);        // printf(“%s \n”, recvBuff);    }
    if(bytesReceived < 0)    {        printf(“\n Read Error \n”);    }

    return 0;}

Server.c

#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>
int main(void){    int listenfd = 0;    int connfd = 0;    struct sockaddr_in serv_addr;    char sendBuff[1024];    int numrv;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    printf(“Socket retrieve success\n”);
    memset(&serv_addr, ‘0’, sizeof(serv_addr));    memset(sendBuff, ‘0’, sizeof(sendBuff));
    serv_addr.sin_family = AF_INET;    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);    serv_addr.sin_port = htons(5000);
    bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
    if(listen(listenfd, 10) == -1)    {        printf(“Failed to listen\n”);        return -1;    }

    while(1)    {        connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
        /* Open the file that we wish to transfer */        FILE *fp = fopen(“sample_file.txt”,”rb”);        if(fp==NULL)        {            printf(“File opern error”);            return 1;           }   
        /* Read data from file and send it */        while(1)        {            /* First read file in chunks of 256 bytes */            unsigned char buff[256]={0};            int nread = fread(buff,1,256,fp);            printf(“Bytes read %d \n”, nread);        
            /* If read was success, send data. */            if(nread > 0)            {                printf(“Sending \n”);                write(connfd, buff, nread);            }
            /*             * There is something tricky going on with read ..              * Either there was error, or we reached end of file.             */            if (nread < 256)            {                if (feof(fp))                    printf(“End of file\n”);                if (ferror(fp))                    printf(“Error reading\n”);                break;            }

        }
        close(connfd);        sleep(1);    }

    return 0;}
/*OUTPUT SERVERiotlab@iotlab-Veriton-M200-B360:~$ cd TCP\ Socket/iotlab@iotlab-Veriton-M200-B360:~/TCP Socket$ cd File\ Transfer/iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/File Transfer$ gcc Server_file.c -o serveriotlab@iotlab-Veriton-M200-B360:~/TCP Socket/File Transfer$ ./server Socket retrieve successBytes read 0 End of file
OUTPUT CLIENTiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/File Transfer$ gcc Client_file.c -o clientiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/File Transfer$ ./clientiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/File Transfer$ */

3. Calculator (Arithmetic) (50% students) 

Client.c

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include<strings.h>
#include <arpa/inet.h>
//#define buffsize  150
void main()
{
int b,sockfd,sin_size,con,n,len;
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
printf(“socket created sucessfully\n”);
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
servaddr.sin_port=6006;
sin_size = sizeof(struct sockaddr_in);
if((con=connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0); //initiate a connection on a socket
printf(“connect sucessful\n”);
printf(“Enter operation:\n +:Addition \n -: Subtraction \n /: Division \n*:Multiplication \n”);
scanf(“%c”,&operator);
printf(“Enter operands:\n”);
scanf(“%d %d”, &op1, &op2);
write(sockfd,&operator,10);
write(sockfd,&op1,sizeof(op1));
write(sockfd,&op2,sizeof(op2));
read(sockfd,&result,sizeof(result));
printf(“Operation result from server=%d\n”,result);
close(sockfd);
}

Server.c

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include <unistd.h>
#include<string.h>
#include <arpa/inet.h>

void main()
{
int b,sockfd,connfd,sin_size,l,n,len;
char operator;
int op1,op2,result;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0)
printf(“socket created sucessfully\n”);  //socket creation
struct sockaddr_in servaddr;           
struct sockaddr_in clientaddr;

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
servaddr.sin_port=6006;

if((bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0)
printf(“bind sucessful\n”);   //bind() assigns the
     //  address  specified  by  addr  to  the  socket  referred  to by the file
      // descriptor sockfd.  addrlen  specifies  the  size,  in  bytes,  of  the
     //  address structure pointed to by addr.  Traditionally, this operation is
      // called “assigning a name to a socket”.

if((listen(sockfd,5))==0) //listen for connections on a socket
printf(“listen sucessful\n”);

sin_size = sizeof(struct sockaddr_in);
if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0);
printf(“accept sucessful\n”);

read(connfd, &operator,10);
read(connfd,&op1,sizeof(op1));
read(connfd,&op2,sizeof(op2));

switch(operator)
{
case ‘+’:
result=op1 + op2;
  printf(“Result is: %d + %d = %d\n”,op1, op2, result);
  break;
case ‘-‘:
result=op1 – op2;
        printf(“Result is: %d – %d = %d\n”,op1, op2, result);
        break;
case ‘*’:
result=op1 * op2;
        printf(“Result is: %d * %d = %d\n”,op1, op2, result);
        break;
case ‘/’:
result=op1 / op2;
        printf(“Result is: %d / %d = %d\n”,op1, op2, result);
        break;
default:
        printf(“ERROR: Unsupported Operation”);
     }
write(connfd,&result,sizeof(result)); 
close(sockfd);

}

/*OUTPUT SERVER
iotlab@iotlab-Veriton-M200-B360:~$ cd TCP\ Socket/
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket$ cd Arithmetic/
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$ gcc server_arithmetic.c -o ser
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$ ./ser
socket created sucessfully
bind sucessful
listen sucessful
accept sucessful
Result is: 10 + 15 = 25

iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$ 

OUTPUT CLIENT
iotlab@iotlab-Veriton-M200-B360:~$ cd TCP\ Socket/
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket$ cd Arithmetic/
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$ gcc client_arithmetic.c -o cl
iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$ ./cl
socket created sucessfully
connect sucessful
Enter operation:
 +:Addition 
 -: Subtraction 
 /: Division 
*:Multiplication 
+
Enter operands:
10
15
Operation result from server=25

iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Arithmetic$  
*/

4. Calculator (Trigonometry) (50% students)

Client.c

#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/in.h> #include <unistd.h>#include<string.h> #include<strings.h>#include <arpa/inet.h>#include<math.h>//#define buffsize  150void main(){ int b,sockfd,sin_size,con,n,len; double angle,result; char op;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0) printf(“socket created sucessfully\n”); struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”); servaddr.sin_port=6666;
sin_size = sizeof(servaddr); if((con=connect(sockfd,(struct sockaddr *) &servaddr, sin_size))==0); //initiate a connection on a socket printf(“connect sucessful\n”); printf(“Enter operation:\n 1:sin \n 2:cos\n 3:tan \n “); scanf(“%c”,&op);
printf(“Enter angle in degree:”); scanf(“%lf”,&angle);
write(sockfd,&op,1); write(sockfd,&angle,sizeof(angle)); read(sockfd,&result,sizeof(result)); 
printf(“\n Operation result from server=%lf\n”,result);   close(sockfd);}

Server.c

#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/in.h> #include <unistd.h>#include<string.h> #include <arpa/inet.h>#include<math.h>#define PI 3.14159265void main(){ int b,sockfd,connfd,sin_size,l,n,len; char op; double angle1; double result,val;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))>0) printf(“socket created sucessfully\n”);  //socket creation struct sockaddr_in servaddr;               struct sockaddr_in clientaddr;
servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”); servaddr.sin_port=6666;
if((bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)))==0) printf(“bind sucessful\n”);   //bind() assigns the     //  address  specified  by  addr  to  the  socket  referred  to by the file      // descriptor sockfd.  addrlen  specifies  the  size,  in  bytes,  of  the     //  address structure pointed to by addr.  Traditionally, this operation is      // called “assigning a name to a socket”.
if((listen(sockfd,5))==0) //listen for connections on a socket printf(“listen sucessful\n”); sin_size = sizeof(clientaddr); if((connfd=accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size))>0); printf(“accept sucessful\n”); val = PI / 180; read(connfd, &op,1);
read(connfd, &angle1, sizeof(angle1)); switch(op)  {         case ‘1’:  result=sin(angle1*val);          printf(“sin(%lf)=%lf “,angle1,result);          break;         case ‘2’: result=cos(angle1*val);          printf(“cos(%lf) =%lf “,angle1,result);                 break;         case ‘3’: result=tan(angle1*val);          printf(“tan(%lf) = %lf”,angle1,result);                 break;         default:                  printf(“ERROR: Unsupported Operation”);     }   write(connfd,&result,sizeof(result)); close(connfd);    close(sockfd);}
/*OUTPUT CLIENTiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $ gcc server_trig.c -o ser -lmiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $ ./sersocket created sucessfullybind sucessfullisten sucessfulaccept sucessfulsin(90.000000)=1.000000 iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $  
OUTPUT SERVERiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $ gcc client_trig.c -o cl -lmiotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $ ./clsocket created sucessfullyconnect sucessfulEnter operation: 1:sin  2:cos 3:tan  1Enter angle in degree:90
 Operation result from server=1.000000iotlab@iotlab-Veriton-M200-B360:~/TCP Socket/Trignometri $ */

Categories
TE Computer Engineering

5. Write a program to simulate the behaviour of link state routing protocol to find suitable path for transmission.

Download The write up here

Dijkstra.java

import java.util.*;

public class Dijkstra
{
  public  int distance[] = new int[10];
  public  int cost[][] = new int[10][10];

  public void calc(int n,int s)
  {
   int flag[] = new int[n+1];
   int i,minpos=1,k,c,minimum;
      for(i=1;i<=n;i++)
   {
             flag[i]=0;
       this.distance[i]=this.cost[s][i];
    }
      c=2;
    while(c<=n)
   {
    minimum=99;
    for(k=1;k<=n;k++)
    {
           if(this.distance[k]<minimum && flag[k]!=1)
        {
         minimum=this.distance[i];
         minpos=k;
        }
       }
             flag[minpos]=1;
       c++;
       for(k=1;k<=n;k++)
  {
          if(this.distance[minpos]+this.cost[minpos][k] <  this.distance[k] && flag[k]!=1 )
     this.distance[k]=this.distance[minpos]+this.cost[minpos][k];
  } 
   }

  }
  public static void main(String args[])
  {
   int nodes,source,i,j;
   Scanner in = new Scanner(System.in);
   System.out.println(“Enter the Number of Nodes \n”);
   nodes = in.nextInt();
   Dijkstra d = new Dijkstra();
   System.out.println(“Enter the Cost Matrix Weights: \n”);
         for(i=1;i<=nodes;i++)
           for(j=1;j<=nodes;j++)
     {
            d.cost[i][j]=in.nextInt();
             if(d.cost[i][j]==0)
               d.cost[i][j]=999;
           }
   System.out.println(“Enter the Source Vertex :\n”);
   source=in.nextInt();

      d.calc(nodes,source);
   System.out.println(“The Shortest Path from Source \t”+source+”\t to all other vertices are : \n”);
         for(i=1;i<=nodes;i++)
           if(i!=source)
  System.out.println(“source :”+source+”\t destination :”+i+”\t MinCost is :”+d.distance[i]+”\t”);


}
}

/*OUTPUTiotlab@iotlab-Veriton-M200-B360:~$ javac Dijkstra.java iotlab@iotlab-Veriton-M200-B360:~$ java Dijkstra Enter the Number of Nodes 
5Enter the Cost Matrix Weights: 
0 1 0 2 01 0 1 0 40 1 0 0 32 0 0 0 30 4 3 3 0Enter the Source Vertex :
1The Shortest Path from Source  1  to all other vertices are : 
source :1  destination :2  MinCost is :1 source :1  destination :3  MinCost is :2 source :1  destination :4  MinCost is :2 source :1  destination :5  MinCost is :5 */

Categories
TE Computer Engineering

4. Write a program to demonstrate subnetting and find the subnet masks.

Download The write up here

import java.io.*;
import java.net.InetAddress;
public class Subnet1 {

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

        System.out.println(“ENTER IP:”);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String ip = br.readLine();
        String checkclass = ip.substring(0, 3);

        int cc = Integer.parseInt(checkclass);
        String mask = null;
        if(cc>0)
        {
            if(cc<=127)
            {
                mask = “255.0.0.0”;
        System.out.println(“Class A IP Address”);
        System.out.println(“SUBNET MASK:\n”+mask);
            }
            if(cc>=128 && cc<=191)
            {
                mask = “255.255.0.0”;
        System.out.println(“Class B IP Address”);
        System.out.println(“SUBNET MASK:\n”+mask);
            }
            if(cc>=192 && cc<=223)
            {
                mask = “255.255.255.0”;
        System.out.println(“Class C IP Address”);
        System.out.println(“SUBNET MASK:\n”+mask);
            }
        if(cc>=224 && cc<=239)
            {
        mask = “255.0.0.0”;
                System.out.println(“Class D IP Address Used for multicasting”);
            }
            if(cc>=240 && cc<=254)
            {
        mask = “255.0.0.0”;
                System.out.println(“Class E IP Address Experimental Use”);
            }
        }

        String networkAddr=””;
    String lastAddr=””;
        String[] ipAddrParts=ip.split(“\\.”);
        String[] maskParts=mask.split(“\\.”);

        for(int i=0;i<4;i++){
        int x=Integer.parseInt(ipAddrParts[i]);
        int y=Integer.parseInt(maskParts[i]);
        int z=x&y;
        networkAddr+=z+”.”;
    int w=z|(y^255);
    lastAddr+=w+”.”;
        }

    System.out.println(“First IP of block: “+networkAddr);
    System.out.println(“Last IP of block: “+lastAddr);
   }

}

/*OUTPUT
iotlab@iotlab-Veriton-M200-B360:~$ javac Subnet1.java
iotlab@iotlab-Veriton-M200-B360:~$ java Subnet1
ENTER IP:
226.35.65.23
Class D IP Address Used for multicasting
First IP of block: 226.0.0.0.
Last IP of block: 226.255.255.255.
iotlab@iotlab-Veriton-M200-B360:~$ java Subnet1
ENTER IP:
192.168.100.5
Class C IP Address
SUBNET MASK:
255.255.255.0
First IP of block: 192.168.100.0.
Last IP of block: 192.168.100.255.
iotlab@iotlab-Veriton-M200-B360:~$
*/