Manual 2 ND
Manual 2 ND
#include <stdio.h>
#include <string.h>
void bitStuffing(int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------
BIT DESTUFFING
#include <stdio.h>
#include <string.h>
void bitDestuffing(int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
int count = 1;
while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1
&& k < N
&& count < 5;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
printf("%d", brr[i]);
}
int main()
{
int N = 7;
int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
bitDestuffing(N, arr);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------
BYTE STUFFING
#include <stdio.h>
#include <string.h>
void main()
{
char frame[50][50], str[50][50];
char flag[10];
strcpy(flag, "flag");
char esc[10];
strcpy(esc, "esc");
int i, j, k = 0, n;
strcpy(frame[k++], "flag");
printf("Enter length of String : \n");
scanf("%d", &n);
printf("Enter the String: ");
for (i = 0; i <= n; i++)
{
gets(str[i]);
}
printf("\nYou entered :\n");
for (i = 0; i <= n; i++)
{
puts(str[i]);
}
printf("\n");
for (i = 1; i <= n; i++)
{
if (strcmp(str[i], flag) != 0 && strcmp(str[i], esc) != 0)
{
strcpy(frame[k++], str[i]);
}
else
{
strcpy(frame[k++], "esc");
strcpy(frame[k++], str[i]);
}
}
strcpy(frame[k++], "flag");
printf("------------------------------\n\n");
printf("Byte stuffing at sender side:\n\n");
printf("------------------------------\n\n");
for (i = 0; i < k; i++)
{
printf("%s\t", frame[i]);
}
}
-------------------------------------------------------------------------
----------------------------------------------------------
HAMMING CODE
#include<stdio.h>
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
printf("Enter 4 bits of data one by one\n");
scanf("%d",&data[0]);
scanf("%d",&data[1]);
scanf("%d",&data[2]);
scanf("%d",&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);
printf("\nData sent : ");
for(i=0;i<7;i++)
printf("%d",data[i]);
printf("\nData received : ");
for(i=0;i<7;i++)
printf("%d",dataatrec[i]);
printf("\nCorrect message is\n");
if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d",dataatrec[i]);
}
}
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
-------------------------------------------------------------------------
---------------------------------------------
MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}}
MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}}
-------------------------------------------------------------------------
---------------------------------------------------------------------
Server side
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class udpBaseServer_2
{
public static void main(String[] args) throws IOException
{
DatagramSocket ds = new DatagramSocket(1234);
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
while (true)
{
DpReceive = new DatagramPacket(receive, receive.length);
ds.receive(DpReceive);
System.out.println("Client:-" + data(receive));
if (data(receive).toString().equals("bye"))
{
System.out.println("Client sent bye.....EXITING");
break;
}
receive = new byte[65535];
}
}
public static StringBuilder data(byte[] a)
{
if (a == null)
return null;
StringBuilder ret = new StringBuilder();
int i = 0;
while (a[i] != 0)
{
ret.append((char) a[i]);
i++;
}
return ret;
}
}
Client side
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class udpBaseClient_2
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
while (true)
{
String inp = sc.nextLine();
buf = inp.getBytes();
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1234);
ds.send(DpSend);
if (inp.equals("bye"))
break;
}
}
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------------------------------------------------------
IPV6
_________
Flow Label (20-bits): Flow Label field is used by a source to label the
packets belonging to the same flow in order to request special handling
by intermediate IPv6 routers, such as non-default quality of service or
real-time service. In order to distinguish the flow, an intermediate
router can use the source address, a destination address, and flow label
of the packets.
Hop Limit (8-bits): Hop Limit field is the same as TTL in IPv4 packets.
It indicates the maximum number of intermediate nodes IPv6 packet is
allowed to travel. Its value gets decremented by one, by each node that
forwards the packet and the packet is discarded if the value decrements
to 0. This is used to discard the packets that are stuck in an infinite
loop because of some routing error.
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------------------------------------------------------
IPV4
________
Total Length: Length of header + Data (16 bits), which has a minimum
value 20 bytes and the maximum is 65,535 bytes.
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------------------------------------------------------