Subnet program java

import java.util.Scanner;
public class Subnet
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the IP address: “);
int[] ipint=new int[4];
String ip=sc.nextLine();
String split_ip[]=ip.split(“\\.”);
String split_bip[]=new String[4];
String bip=””;
for(int i=0;i<4;i++)
{
ipint[i]=Integer.parseInt(split_ip[i]);
}
System.out.println(“IP: “);
for(int i=0;i<4;i++)
{
System.out.print(ipint[i]);
System.out.print(“.”);
}

System.out.print(“Enter the number of bits: “);
int n=sc.nextInt();

int bits=(int)Math.ceil(Math.log(n)/Math.log(2)); //eg. if address=120,log 120/log2=log to the base 2=>6.9068
int[] mask=new int[4];
int j=0,x,k;

//class A
if(ipint[0]<127)
{
j=0;

mask[j]=255;
mask[j+1]=0;
mask[j+2]=0;
mask[j+3]=0;

for(int i=1;i<4;i++)
{
if(n<8)
{
for(j=7,k=0;k<n;j–,k++)
{
mask[i]=mask[i]+(int)Math.pow(2,j);

}
break;
}
else
{
mask[i]=255;
n=n-8;
}
}
System.out.println(“class A”);
}
//class B
else if(ipint[0]<191)
{
j=0;

mask[j]=255;
mask[j+1]=255;
mask[j+2]=0;
mask[j+3]=0;

for(int i=2;i<4;i++)
{
if(n<8)
{
for(j=7,k=0;k<n;j–,k++)
{
mask[i]=mask[i]+(int)Math.pow(2,j);

}
break;
}
else
{
mask[i]=255;
n=n-8;
}
}
System.out.println(“class B”);
}
//class C
else if(ipint[0]<223)
{
j=0;

mask[j]=255;
mask[j+1]=255;
mask[j+2]=255;
mask[j+3]=0;

for(int i=3;i<4;i++)
{
if(n<8)
{
for(j=7,k=0;k<n;j–,k++)
{
mask[i]=mask[i]+(int)Math.pow(2,j);

}
break;
}
else
{
mask[i]=255;
n=n-8;
}
}
System.out.println(“class C”);
}
System.out.println(“The Subnet Mask is = “);
for(int i=0;i<4;i++)
System.out.print(mask[i]+”.”);
int[] netid=new int[4];
System.out.println(“The Network ID = “);
for(int i=0;i<4;i++)
{
netid[i]=ipint[i]&mask[i];
}

for(int i=0;i<4;i++)
System.out.print(netid[i]+”.”);

}
}

Advertisements

Multi user Chat application

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MultiThreadChatClient implements Runnable{
static Socket clientSocket = null;
static PrintWriter os = null;
static BufferedReader is = null;
static BufferedReader inputLine = null;
static boolean closed = false;

public static void main(String[] args) {

// The default port

int port_number=7686;
String host=”localhost”;

if (args.length < 2)
{
System.out.println("Usage: java MultiThreadChatClient \n"+
"Now using host="+host+", port_number="+port_number);
}
else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
// Initialization section:
// Try to open a socket on a given host and port
// Try to open input and output streams
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintWriter(clientSocket.getOutputStream(),true);
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host "+host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "+host);
}

// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port port_number

if (clientSocket != null && os != null && is != null) {
try {

// Create a thread to read from the server

new Thread(new MultiThreadChatClient()).start();

while (!closed) {
os.println(inputLine.readLine());
}

// Clean up:
// close the output stream
// close the input stream
// close the socket

os.close();
is.close();
clientSocket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}

@SuppressWarnings("deprecation")
public void run() {
String responseLine;

// Keep on reading from the socket till we receive the "Bye" from the server,
// once we received that then we want to break.
try{
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}

Server :

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

public class MultiThreadChatServer{

// Declaration section:
// declare a server socket and a client socket for the server
// declare an input and an output stream

static Socket clientSocket = null;
static ServerSocket serverSocket = null;

// This chat server can accept up to 10 clients' connections

static clientThread t[] = new clientThread[10];

public static void main(String args[]) {

// The default port

int port_number=7686;

if (args.length < 1)
{
System.out.println("Usage: java MultiThreadChatServer \n"+
"Now using port number="+port_number);
} else {
port_number=Integer.valueOf(args[0]).intValue();
}

// Initialization section:
// Try to open a server socket on port port_number (default 2222)
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)

try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}

// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams for this socket will be created in
// client's thread since every client is served by the server in
// an individual thread

while(true){
try {
clientSocket = serverSocket.accept();
for(int i=0; i<=9; i++){
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
}

// This client thread opens the input and the output streams for a particular client,
// ask the client's name, informs all the clients currently connected to the
// server about the fact that a new client has joined the chat room,
// and as long as it receive data, echos that data back to all other clients.
// When the client leaves the chat room this thread informs also all the
// clients about that and terminates.

class clientThread extends Thread{

BufferedReader is = null;
PrintWriter os = null;
Socket clientSocket = null;
clientThread t[];

public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}

public void run()
{
String line;
String name;
try{
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new PrintWriter(clientSocket.getOutputStream(),true);
os.println("Enter your name.");
name = is.readLine();
os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
while (true) {
line = is.readLine();
if(line.startsWith("/quit")) break;
for(int i=0; i<=9; i++)
if (t[i]!=null) t[i].os.println(" “+line);
}
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );

os.println("*** Bye "+name+" ***");

// Clean up:
// Set to null the current thread variable such that other client could
// be accepted by the server

for(int i=0; i<=9; i++)
if (t[i]==this) t[i]=null;

// close the output stream
// close the input stream
// close the socket

is.close();
os.close();
clientSocket.close();
}
catch(IOException e){};
}
}

Computer Network Lab Assignment List

1 Lab Assignment on Unit I: (Mandatory Assignment)

Part A: Setup a wired LAN using Layer 2 Switch and then IP switch of minimum four computers. It includes preparation of cable, testing of cable using line tester, configuration machine using IP addresses, testing using PING utility and demonstrate the PING packets captured traces using Wireshark Packet Analyzer Tool.

Part B: Extend the same Assignment for Wireless using Access Point

2

Write a program for error detection and correction for 7/8 bits ASCII codes using Hamming Codes or CRC. Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.( 50% students will perform Hamming Code and others will perform CRC)

3

Write a program to simulate Go back N and Selective Repeat Modes of Sliding Window Protocol in peer to peer mode and demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

4

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

5 Write a program to demonstrate subletting and find the subnet masks.
6 Lab Assignment on Unit V: (Mandatory Assignment) (Use C/C++)

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)

Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

7 Lab Assignment on Unit V: (Mandatory Assignment) (Use C/C++)

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.

8 Lab Assignment on Unit V: (Mandatory Assignment) (Use C/C++)

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

9 Lab Assignment on Unit VI: (Use JAVA/PYTHON)

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

10

Installing and configure DHCP server and write a program to install the software on remote machine.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

GROUP B
9 Lab Assignment on Unit IV and Unit V: (Mandatory Assignment)

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).

10 Configure RIP/OSPF/BGP using packet Tracer
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.

12 Write a program using TCP 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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                  Prof A. S. Patil                                                           Prof N. R. Wankhade      

               ( Subject In charge)                                                                  (HOD)

 

 

                   Prof A. S. Patil

                (Subject In charge)

CNL Lab Assignment based on Socket Programming

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)
Demonstrate the packets captured traces using Wireshark Packet Analyzer Tool for peer to peer mode.

part a :

Server Program:

/****************** SERVER CODE ****************/

#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”);
else
printf(“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;
}

Client Program:

/****************** CLIENT CODE ****************/

#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;
}

ping

ping is a computer network administration software utility used to test the reachability of a host on an Internet Protocol (IP) network. It measures the round-trip time for messages sent from the originating host to a destination computer that are echoed back to the source. The name comes from active sonar terminology that sends a pulse of sound and listens for the echo to detect objects under water, although it is sometimes interpreted as a ackronym to packet Internet groper.

Ping operates by sending Internet Control Message Protocol (ICMP/ICMP6 ) Echo Request packets to the target host and waiting for an ICMP Echo Reply. The program reports errors, packet loss, and a statistical summary of the results, typically including the minimum, maximum, the mean round-trip times, and standard deviation of the mean.

The command-line options of the ping utility and its output vary between the numerous implementations. Options may include the size of the payload, count of tests, limits for the number of network hops (TTL) that probes traverse, and interval between the requests. Many systems provide a companion utility ping6, for testing on Internet Protocol version 6 (IPv6) networks

ping

CNL Assignment No1:

Theory:

Physical Addressing and Switching

Network is a group of devices connected to each other. On Layer 2, devices can communicate within a single network only. Layer 2 devices cannot span multiple networks, for multiple networks Layer 3 support is required.  Each device in a single network needs to be identified uniquely. At Layer 2, unique identification is done via physical addressing scheme. Device hardware (NIC Card) which provides interconnection has unique physical address assigned to it known as MAC Address. MAC address is of 48 bits, written in hexadecimal form separated after 8 bits with either colon (:) or hyphen (-).

Example address: 00:80:48:5C:1A:52.

So each machine will have unique physical address by which machine is identified. Total MAC Address is of 6 bytes.

Each manufacturer is allotted with first 3 bytes of MAC Address which act as series prefix for all the NIC’s by that company, last 3 bytes are unique within one series. By this way no MAC address of one company can clash with another nor within company series.

Communication among devices at layer 2 is done via some interconnecting device, which forms connection between each host machine or network devices (router, wireless, etc).

Switch is that device which provides interconnection. Switch has ports (physical interface) at which wires from various network devices or host machines connect. All devices in a network are connected to a switch which interconnects them; this is Layer 1 (inter-connection).  Now let’s get into deep understanding about how communication is done after interconnection. For this we need to understand working of switch.

Working of Switch:

All other devices are connected to these ports. Whenever any machine sends packet to any other machine, source machine send packet to switch, switch then forwards it to destination machine. Each packet which comes to switch contains source and destination physical address in it, on basis of which switch forwards packet to other machine. Switch always sent packet based on destination MAC address. Its process is as follows: (process also known as Switching)

  • When switch receives a packet from any device, it checks for its destination MAC address.
  • Then switch compares destination MAC address with its MAC Address Table for corresponding MAC address.
    • If MAC Address is found, packet is sent out to port against which MAC Address was matched.
      • If entry is not found, Unknown  unicasts (when the switch  doesn’t have a port mapping for a destination  mac address in the frame)  are treated like broadcasts by Layer Two  devices, and are flooded out  of all ports except the port on which the  frame originated.