You are on page 1of 7

Assignment 3

4. Bit Stuffing Simulation


In bit stuffing processing, we add 0 after five consecutive 1's in databits.

#include<stdio.h>
int main()
{
int i=0,count=0;
char databits[80];

printf("Enter Data Bits: ");


scanf("%s",databits);

printf("Data Bits Before Bit Stuffing:%s",databits);


printf("\nData Bits After Bit stuffing :");

for(i=0; i<strlen(databits); i++)


{
if(databits[i]=='1')
count++;
else
count=0;
printf("%c",databits[i]);
if(count==5)
{
printf("0");
count=0;
}
}
return 0;
}

Output of the Program:

Enter Data Bits: 101111111000


Data Bits Before Bit Stuffing:101111111000
Data Bits After Bit stuffing :1011111011000

5. Program to display IP Address


C Program to display local IP Address.

#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\ipconfig");
return 0;
}

pg. 3
6. ARP Simulation
This program will find Physical Address for the given IP address.

/* Sample arp.txt file generated using arp -a command


192.168.3.3 f4-f4-9c-18-bb-4b dynamic
192.168.3.2 ca-f8-87-a3-ee-20 dynamic
192.168.90.255 ff-ff-ff-ff-ff-ff static
221.0.0.251 03-01-ef-00-ff-fc static
29.253.251.20 02-01-ed-ff-00-fa static
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *f1, *fopen();
int ch;
char str1[80], *token;
const char str[80] = "", s[2] = " ";
f1 = fopen("arp.txt","r");
if ( f1 == NULL ) /* check does file exist*/
{
printf("Cannot open file for reading \n" );
exit(1);
}
while(fgets(str1,80,f1)!=NULL){
token = strtok(str1,s);
while (token != NULL){
if (strcmp(token,"192.168.3.2") ==0)
{
token = strtok(NULL, s);
printf("Your Physical address is %s",token);
}
else
token = strtok(NULL, s);
}
}
fclose(f1);
return 0;
}

Output of the program

Your Physical address is ca-f8-87-a3-ee-20

pg. 4
7. AES Encryption Decryption
Note: You need to create PlainTextInput.txt file in D:\ drive of
computer. Execution of program will create EncryptedText.txt and
DecryptedText.txt file in D:\ drive.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;

public class AESEncryptionDecryption {

public static void main(String[] args) {


try{
Cipher cipher = Cipher.getInstance("AES");
KeyGenerator kg = KeyGenerator.getInstance("AES");
Key key = kg.generateKey();

cipher.init(Cipher.ENCRYPT_MODE, key);
CipherInputStream cipt=new CipherInputStream(new
FileInputStream(new File("D:\\PlainTextInput.txt")), cipher);
FileOutputStream fip=new FileOutputStream(new
File("D:\\EncryptedText.txt"));

int i;
while((i=cipt.read())!=-1)
{
fip.write(i);
}

cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream ciptt=new CipherInputStream(new
FileInputStream(new File("D:\\EncryptedText.txt")), cipher);
FileOutputStream fop=new FileOutputStream(new
File("D:\\DecryptedText.txt"));

int j;
while((j=ciptt.read())!=-1)
{
fop.write(j);
}

}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("Encryption and Decryption of plain text
file performed successfully.");
}
}

pg. 5
Output of the program:

Encryption and Decryption of plain text file performed successfully.

Content of PlainTextInput.txt file: HelloStudent

Content of EncryptedText.txt file: ùóÃ ¬x9f¯ —©c aá

Content of DecryptedText.txt file: HelloStudent

8. Networking Protocol Header


Various protocols like TCP, UDP, IP, IPSec, HTTPs etc have their own
header. Following program can be extended to create full protocol
header.

Write a C program to generate protocol header with following set of


parameters into it:
First parameter is Source IP Address
Second parameter is Destination IP Address
Third parameter is total number of characters present in input.txt
file.

#include<stdio.h>
#include<conio.h>

int main()
{

FILE *input,*out;
int c,counter=0;
//Source Add
char source[]="192.168.1.1";
//Destination Add
char dest[]="192.168.1.20";

input= fopen("input.txt","r");
out=fopen("output.txt","w");

if(input==NULL){
printf("Input File Not found");
}
else if(out==NULL){
printf("Output file not Created");
}
else{
//Counting No of charecter in files
do{
c=getc(input);
counter++;
}while(c!=EOF);

counter= counter-1;
//Generatting Headers

fprintf(out,"%s,%s,%d",source,dest,counter);
printf("Header generated Successfully.\nCheck output.txt file.");

pg. 6
fclose(input);
fclose(out);

return 0;
}
}

9. FTP server client simulation using socket program


This JAVA socket program will download webpage (HTML file) from
server to client. Downloaded file will be opened into web browser.

SERVER side program

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

public class FTPServer {


public static void main(String[] args) throws IOException {
//server will start at port number 12345.
ServerSocket servsock = new ServerSocket(12345);

//server will send index1.htm file to client.


File myFile = new File("D:\\index1.html");
System.out.println("Waiting for Client Request...");
while (true) {
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}
}

SERVER side Output:

Waiting for Client Request...

CLIENT side program

import java.awt.Desktop;
import java.io.*;
import java.net.*;

public class FTPClient {


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

//socket will be created local IP: 127.0.0.1 and port 12345.


Socket sock = new Socket("127.0.0.1", 12345);
byte[] mybytearray = new byte[1024];
pg. 7
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("D:\\index2.html");
BufferedOutputStream bos = new BufferedOutputStream(fos);

int bytesRead = is.read(mybytearray, 0, mybytearray.length);


bos.write(mybytearray, 0, bytesRead);

System.out.println("File downloaded...");

//Following code will open index2.htm file in browser.

String url="D:\\index2.htm";
File htmlFile=new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());

bos.close();
sock.close();
}
}

CLIENT side output:

File downloaded...
BUILD SUCCESSFUL (total time: 1 second)

Program to download a web page


PROGRAM TO DOWNLOAD A WEB PAGE

10. Java program to download a given web page using


URL Class.
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

public class DownloadWebPageDemo {

public static void DownloadWebPageDemo(String webpage) {


try {

// Creating URL object


URL url = new URL(webpage);
BufferedReader br = new BufferedReader(new
InputStreamReader(url.openStream()));

// Filename in which downloaded content to be saved


BufferedWriter writer = new BufferedWriter(new
FileWriter("HomePage.html"));

// read all line from input stream upto end


String line;
while ((line = br.readLine()) != null) {
writer.write(line);
}

pg. 8
br.close();
writer.close();
System.out.println("Webpage Downloaded Successfully.");
System.out.println("Downloaded file saved as
HomePage.html");
}
catch (MalformedURLException MalurlEx) {
System.out.println("URL Exception raised");
} catch (IOException ie) {
System.out.println("IOException raised");
}
}

public static void main(String args[])


throws IOException {
String url = "https://cprogrampracticals.blogspot.com";
DownloadWebPageDemo(url);
}
}

Output of program

Web page Downloaded Successfully.


Downloaded file saved as HomePage.html

Note: HomePage.html file will be downloaded into the folder where a


Source file is stored.

pg. 9

You might also like