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

Lab Programs

Uploaded by

bluewhale0628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab Programs

Uploaded by

bluewhale0628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction

**Title:** ECLIPSE
**Steps to be followed to use eclipse:**

**Step 1:**
- File → New → Java Project → File name → Finish

**Step 2:**
- Window → Show views → Package Explorer → File name → New
→ Class → Class name

**Note:** Always, lename and classname should be the same.

**To run the program:**


- Use the green button
- `public static void main` - name class
- To print a statement, we have to use the system:
`System.out.println("Hello World");`

**Program:**
```java

package computer.net;
class computernet {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```

**Output:** Hello World

**Network con guration commands using LINUX:**


- Linux networking commands are used extensively to inspect, analyze, maintain, and
troubleshoot the network connected to the system.

1. **ifcon g**
**Syntax:** ifcon g
`ifcon g etho <address> netmask <address>`

2. **ip**
**Syntax:** `ip a`, `ip add`

3. **traceroute**
**Syntax:** `traceroute <destination>`
`$ traceroute -n google.com`

4. **tracepath**
**Syntax:** `tracepath <destination>`
`tracepath mindmajix.com`

5. **ping**
**Syntax:** `ping <destination>`
`$ ping google.com`

6. **netstat**
fi
fi
fi
fi
fi
**Syntax:** `netstat`
`netstat -p`, `netstat -s`, `netstat -r`

7. **ss**
**Syntax:** `ss`
`ss -ta`, `ss -ua`, `ss -xa`

8. **dig**
**Syntax:** `dig <domain name>`
`$ dig google.com`

9. **nslookup**
**Syntax:** `nslookup <domain name>`

10. **route**
**Syntax:** `route`
`route -n`, `route -cn`

11. **arp**
**Syntax:** `arp`
`arp -n`, `arp -d HWADDR`

12. **iwcon g**


**Syntax:** `iwcon g`

13. **hostname**
**Syntax:** `hostname`
`sudo hostname <new name>`

14. **curl & wget**


**Syntax:** `curl -o < le link>`
`wget < le link>`

16) mtr who is


Syntax: who is <website Name>

17) mtr
Syntax: mtr <path>

18) if plugstatus
Sudo apt-get install ifplugd
Syntax: ifplugstatus

19) iftop:
tcpdump
Syntax: $tcpdump -i <network -device>
fi
fi
fi
fi
Lab Programs
1.LeakyBucket
package leaky;
import java.io.*;
public class program1 {

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


int b_Size = 0, out_rate, P_Size, discard, cnt, outcnt,clkcnt=0;
Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));
System.out.println("Enter the bucket Size");
b_Size = Integer.parseInt(br.readLine());
System.out.println("Enter the output rate");
out_rate = Integer.parseInt(br.readLine());
System.out.println("Enter the number of packets");
P_Size = Integer.parseInt(br.readLine());
discard= b_Size-P_Size;
if (discard<0)
{
discard = P_Size-b_Size;
P_Size = b_Size;
System.out.println("Packet are more so"+discard+"packets are discarded");
}
System.out.println("Input packet");int packets[] = new int[P_Size];
for(cnt=0; cnt<P_Size; cnt++)
{
packets[cnt] = Integer.parseInt(br.readLine());
}
for(cnt=0; cnt<P_Size;)
{
for (cnt=0; cnt<P_Size;)
{
System.out.println("At time tick"+(clkcnt++));
for(outcnt = 0; outcnt<out_rate;outcnt++)
{
if(cnt<P_Size)
{
System.out.print("packet"+packets[cnt++]+"drained out");
System.out.println();
}
else
break;
}
}
}
}
}
ff
ff
2. Write a program for error detecting code using CRC-CCITT(16bits).

SOURCE CODE:

import java.io.*;
public class CRC {
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine()); divisor=new
int[divisor_bits]; System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/* CRC GENERATION */
System.out.println("data length is "+data.length);
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++){
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++) // append dividend and remainder
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);

/* ERROR DETECTION */
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());

for(int j=0; j<crc.length; j++){


rem[j] = crc[j];
}
rem=divide(crc, divisor, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)

{
System.out.println("Data is incorrect!");
break;
}
if (i==rem.length-1)
System.out.println("Data is correct");
}
}
static int[] divide(int div[],int divisor[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)


cur++;

if((rem.length-cur)<divisor.length)
break;
}
return rem;
}}
OUTPUT
Case 1:
Enter number of bits
16
Enter the data
1
1
1
1
1
1
1
0
0
0
1
0
0

0
1
0
Enter the divisor bits
1
1
0
1
Data length is 16
Dividend after appending 0’s are
1
1
1
1
1
1
1
0
0
0
1
0
0
0
1
0
0
0
0

CRC CODE
1111111000100010101
Enter CRC Code of 19 bits
1
1
1
1
1
1
1
0
0
0
1
0
0
0
1
0
1
0
1
Data is correct
Case-2
Enter number of bits
16
Enter the data
1
1
1
1
1
1
1
0
0
0
1
0
0
0
1
0
Enter the divisor bits
1
1
0
1
Data length is 16
Dividend after appending 0’s are
1
1
1
1
1
1
1
0
0
0
1
0
0
0
1
0
0
0
0

CRC CODE
1111111000100010101
Enter CRC Code of 19 bits
1
0
1
1
1
1
1
0
0
0
1
0
0
0
1
0
1
0
1
Data is incorrect
3.Hamming

#include <stdio.h>
#include <math.h>
int input[32];
int code[32];
int ham_calc(int,int);
void main()
{
int n,i,p_n = 0,c_l,j,k;
printf("Please enter the length of the Data Word: ");
scanf("%d",&n);
printf("Please enter the Data Word:\n");
for(i=0;i<n;i++)
{
scanf("%d",&input[i]);
}
i=0;
while(n>(int)pow(2,i)-(i+1))
{
p_n++;
i++;
}
c_l = p_n + n;
j=k=0;
for(i=0;i<c_l;i++)
{

if(i==((int)pow(2,k)-1))
{
code[i]=0;
k++;
}
else
{
code[i]=input[j];
j++;
}
}
for(i=0;i<p_n;i++)
{
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
code[position-1]=value;
}
printf("\nThe calculated Code Word is: ");
for(i=0;i<c_l;i++)
printf("%d",code[i]);
printf("\n");
printf("Please enter the received Code Word:\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);

int error_pos = 0;
for(i=0;i<p_n;i++)
{
int position = (int)pow(2,i);
int value = ham_calc(position,c_l);
if(value != 0)
error_pos+=position;
}
if(error_pos == 1)
printf("The received Code Word is correct.\n");
else
printf("Error at bit position: %d\n",error_pos);
}
int ham_calc(int position,int c_l)
{
int count=0,i,j;
i=position-1;
while(i<c_l)
{
for(j=i;j<i+position;j++)
{
if(code[j] == 1)
count++;
}
i=i+2*position;
}
if(count%2 == 0)
return 0;
else
return 1;
}

OUTPUT:

Please enter the length of the Data Word: 8


Please enter the Data Word:
1
1
0
1
0
0
1
1

The calculated Code Word is: 011110100011


Please enter the received Code Word:
0
1
1
1
1
1
1
0
0
0
1
1
Error at bit position: 6
--------------------------------
4.SOCKET PROGRAMMING

Chat Server
import java.net.*;
import java.io.*;
public class Server
{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
in = new DataInputStream( new BufferedInputStream(socket.getInputStream()));
String line = "";
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Server server = new Server(5000);
}
}
Chat Client
import java.net.*;
import java.io.*;
public class Client
{
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
public Client(String address, int port)
{
try
{
socket = new Socket(address, port);
System.out.println("Connected");
input = new DataInputStream(System.in);
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
String line = "";
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 5000);
}
}
OUTPUT:
Server:
Server started
Waiting for a client …
Client accepted
Client:
Connected
Server accepts the client
Input from client
Hello
I made my first socket connection
Over
Display at the server
Hello
I made my first socket connection
Over
Closing connection
12/08/2024, 09:42 1.jpg

https://classroom.google.com/c/NjYyNjU2NjEyMjkz/m/Njc4MTE1NDY0NDMw/details 1/1
12/08/2024, 09:43 2.jpg

https://classroom.google.com/c/NjYyNjU2NjEyMjkz/m/Njc4MTE1NDY0NDMw/details 1/1
6.CRC

#include<stdio.h>
#include<string.h>
char t[28],cs[28],g[]="1011";
int a,e,c;
void xor()
{
for(c = 1;c < N; c++)
cs[c] = (( cs[c] == g[c])?'0':'1');
}
void crc()
{
for(e=0;e<N;e++)
cs[e]=t[e];
do{
if(cs[0]=='1')
xor();
for(c=0;c<N-1;c++)
cs[c]=cs[c+1];
cs[c]=t[e++];
}while(e<=a+N-1);
}
int main()
{
printf("\nEnter data : ");
scanf("%s",t);
printf("\n ----- ");
printf("\nGeneratng polynomial : %s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n ----- ");
printf("\nModified data is : %s",t);
printf("\n ----- ");
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n ----- ");
printf("\nFinal codeword is : %s",t);
printf("\n ----- ");
printf("\nTest error detection 0(yes) 1(no)? : ");
scanf("%d",&e);
if(e==0)
{
do{
printf("\nEnter the position where error is to be inserted : ");
scanf("%d",&e);
}while(e==0 || e>a+N-1);
t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n ----- ");
printf("\nErroneous data : %s\n",t);
}
crc();
for(e=0;(e<N-1) && (cs[e]!='1');e++);
if(e<N-1)
printf("\nError detected\n\n");
else
printf("\nNo error detected\n\n");
printf("\n ----- \n");
return 0;
}

OUTPUT:

Enter data : 10100001

----------------------------------------
Generatng polynomial : 1011
----------------------------------------
Modified data is : 10100001000
----------------------------------------
Checksum is : 010
----------------------------------------
Final codeword is : 10100001010
----------------------------------------
Test error detection 0(yes) 1(no)? : no

No error detected

----------------------------------------

--------------------------------
Process exited after 60.61 seconds with return value 0
Press any key to continue . . .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy