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:~$
*/

Categories
TE Computer Engineering

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.

Download The write up here

//—–AT CLIENT SIDE——

    import java.lang.System;
    import java.net.*;
    import java.io.*;

    public class Client {
        static Socket connection;

        public static void main(String a[]) throws SocketException {
            try {
                int v[] = new int[9];
                //int g[] = new int[8];
                int n = 0;
                InetAddress addr = InetAddress.getByName(“Localhost”);
                System.out.println(addr);
                connection = new Socket(addr, 8011);
                DataOutputStream out = new DataOutputStream(
                        connection.getOutputStream());
                DataInputStream in = new DataInputStream(
                        connection.getInputStream());
                int p = in.read();
                System.out.println(“No of frame is:” + p);

                for (int i = 0; i < p; i++) {
                    v[i] = in.read();
                    System.out.println(v[i]);
                    //g[i] = v[i];
                }
                v[5] = -1;
                for (int i = 0; i < p; i++)
                 {
                    System.out.println(“Received frame is: ” + v[i]);

                    }
                for (int i = 0; i < p; i++)
                    if (v[i] == -1) {
                System.out.println(“Request to retransmit packet no “
                                + (i+1) + ” again!!”);
                        n = i;
                        out.write(n);
                        out.flush();
                    }

                System.out.println();

                    v[n] = in.read();
                System.out.println(“Received frame is: ” + v[n]);



                System.out.println(“quiting”);
            } catch (Exception e) {
                System.out.println(e);
            }

        }
    }

/* OUTPUT 
iotlab@iotlab-Veriton-M200-B360:~/sliding window$ javac Client.java
iotlab@iotlab-Veriton-M200-B360:~/sliding window$ java Client
Localhost/127.0.0.1
No of frame is:9
30
40
50
60
70
80
90
100
110
Received frame is: 30
Received frame is: 40
Received frame is: 50
Received frame is: 60
Received frame is: 70
Received frame is: -1
Received frame is: 90
Received frame is: 100
Received frame is: 110
Request to retransmit packet no 6 again!!

Received frame is: 80
quiting
iotlab@iotlab-Veriton-M200-B360:~/sliding window$
*/

//—–AT SERVER SIDE—–

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketException;

    public class Server {
        static ServerSocket Serversocket;
        static DataInputStream dis;
        static DataOutputStream dos;

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

        try {
            int a[] = { 30, 40, 50, 60, 70, 80, 90, 100, 110 };
            Serversocket = new ServerSocket(8011);
            System.out.println(“waiting for connection”);
            Socket client = Serversocket.accept();
            dis = new DataInputStream(client.getInputStream());
            dos = new DataOutputStream(client.getOutputStream());
            System.out.println(“The number of packets sent is:” + a.length);
            int y = a.length;
            dos.write(y);
            dos.flush();

            for (int i = 0; i < a.length; i++) {
                dos.write(a[i]);
                dos.flush();
            }

            int k = dis.read();

            dos.write(a[k]);
            dos.flush();

            } catch (IOException e) {
                System.out.println(e);
            } finally {
                try {
                    dis.close();
                    dos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }

/*OUTPUT
iotlab@iotlab-Veriton-M200-B360:~/sliding window$ javac Server.java
iotlab@iotlab-Veriton-M200-B360:~/sliding window$ java Server
waiting for connection
The number of packets sent is:9
iotlab@iotlab-Veriton-M200-B360:~/sliding window$
*/

Categories
TE Computer Engineering

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

Download The write up here

1. CRC

#include <iostream>
using namespace std;

int main()
{
    int i,j,k,l;

        //Get Frame
        int fs;
        cout<<“\n Enter Frame size: “;
        cin>>fs;

        int f[20];

        cout<<“\n Enter Frame:”;
        for(i=0;i<fs;i++)
        {
        cin>>f[i];
        }

        //Get Generator
        int gs;
        cout<<“\n Enter Generator size: “;
        cin>>gs;

        int g[20];

        cout<<“\n Enter Generator:”;
        for(i=0;i<gs;i++)
        {
        cin>>g[i];
        }

        cout<<“\n Sender Side:”;
        cout<<“\n Frame: “;
        for(i=0;i<fs;i++)
        {
        cout<<f[i];
        }
        cout<<“\n Generator :”;
        for(i=0;i<gs;i++)
        {
        cout<<g[i];
        }

        //Append 0’s
        int rs=gs-1;
        cout<<“\n Number of 0’s to be appended: “<<rs;
        for (i=fs;i<fs+rs;i++)
        {
        f[i]=0;
        }

        int temp[20];
        for(i=0;i<20;i++)
        {
        temp[i]=f[i];
        }

        cout<<“\n Message after appending 0’s :”;
        for(i=0; i<fs+rs;i++)
        {
        cout<<temp[i];
        }

        //Division
        for(i=0;i<fs;i++)
        {
        j=0;
        k=i;
        //check whether it is divisible or not
        if (temp[k]>=g[j])
        {
            for(j=0,k=i;j<gs;j++,k++)
            {
                if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
                {
                    temp[k]=0;
                }
                else
                {
                    temp[k]=1;
                }
            }
        }
        }

        //CRC
        int crc[15];
        for(i=0,j=fs;i<rs;i++,j++)
        {
        crc[i]=temp[j];
        }

        cout<<“\n CRC bits: “;
        for(i=0;i<rs;i++)
        {
        cout<<crc[i];
        }

        cout<<“\n Transmitted Frame: “;
        int tf[15];
        for(i=0;i<fs;i++)
        {
        tf[i]=f[i];
        }
        for(i=fs,j=0;i<fs+rs;i++,j++)
        {
        tf[i]=crc[j];
        }
        for(i=0;i<fs+rs;i++)
        {
        cout<<tf[i];
        }

        cout<<“\n Receiver side : “;
        cout<<“\n Received Frame: “;
        for(i=0;i<fs+rs;i++)
        {
        cout<<tf[i];
        }

        for(i=0;i<fs+rs;i++)
        {
        temp[i]=tf[i];
        }

        //Division
        for(i=0;i<fs+rs;i++)
        {
        j=0;
        k=i;
        if (temp[k]>=g[j])
        {
            for(j=0,k=i;j<gs;j++,k++)
            {
                if((temp[k]==1 && g[j]==1) || (temp[k]==0 && g[j]==0))
                {
                    temp[k]=0;
                }
                else
                {
                    temp[k]=1;
                }
            }
        }
        }

        cout<<“\n Reaminder: “;
        int rrem[15];
        for (i=fs,j=0;i<fs+rs;i++,j++)
        {
        rrem[j]= temp[i];
        }
        for(i=0;i<rs;i++)
        {
        cout<<rrem[i];
        }

        int flag=0;
        for(i=0;i<rs;i++)
        {
        if(rrem[i]!=0)
        {
            flag=1;
        }
        }

        if(flag==0)
        {
        cout<<“\n Since Remainder Is 0 Hence Message Transmitted From Sender To Receriver Is Correct”;
        }
        else
        {
        cout<<“\n Since Remainder Is Not 0 Hence Message Transmitted From Sender To Receriver Contains Error”;
        }
        return 0;
}

/* OUTPUT
iotlab@iotlab-Veriton-M200-B360:~$ g++ crc1.cpp
iotlab@iotlab-Veriton-M200-B360:~$ ./a.out

 Enter Frame size: 8

 Enter Frame:1
0
1
1
0
1
1
1

 Enter Generator size: 4

 Enter Generator:1
0
1
0

Sender Side:
Frame: 10110111
Generator :1010
Number of 0’s to be appended: 3
Message after appending 0’s :10110111000
CRC bits: 110
Transmitted Frame: 10110111110
Receiver side :
Received Frame: 10110111110
Reaminder: 000
Since Remainder Is 0 Hence Message Transmitted From Sender To Receriver Is Correct
*/

2. Hamming Code

 #include<iostream>

using namespace std;

int main()
{
    int data[10];
    int dataatrec[10],c,c1,c2,c3,i;

        cout<<“Enter 4 bits of data one by one\n”;
        cin>>data[7];
        cin>>data[6];
        cin>>data[5];
        cin>>data[3];

        //Calculation of even parity
        data[4]=data[5]^data[6]^data[7];
        data[2]=data[3]^data[6]^data[7];
        data[1]=data[3]^data[5]^data[7];

        cout<<“\nEncoded data is\n”;
        for(i=1;i<=7;i++)
            cout<<data[i];

        cout<<“\n\nEnter received data bits one by one\n”;
        for(i=1;i<=7;i++)
            cin>>dataatrec[i];

        c1=dataatrec[1]^dataatrec[3]^dataatrec[5]^dataatrec[7];
        c2=dataatrec[2]^dataatrec[3]^dataatrec[6]^dataatrec[7];
        c3=dataatrec[4]^dataatrec[5]^dataatrec[6]^dataatrec[7];
        c=c3*4+c2*2+c1;
        if(c==0)
        {
        cout<<“\ncongratulations there is no error: “;
    }
    else
    {
        cout<<“\nerror on the postion:”<<c;
        cout<<“\nCorrect message is:”;
        if(dataatrec[c]==0)
                 dataatrec[c]=1;
        else
                 dataatrec[c]=0;
        for (i=1;i<=7;i++)
        {
            cout<<dataatrec[i];
        }
    }

        return 0;
}

/*OUTPUT
iotlab@iotlab-Veriton-M200-B360:~$ g++ Ham.cpp
iotlab@iotlab-Veriton-M200-B360:~$ ./a.out
Enter 4 bits of data one by one
1
1
0
0

Encoded data is
1000011

Enter received data bits one by one
1
1
0
0
0
1
1

error on the postion:2
Correct message is:1000011

iotlab@iotlab-Veriton-M200-B360:~$*/

Categories
TE Computer Engineering

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

Refer Lab Manual

Download The write up here

Categories
SE Computer Engineering

NASM Package Installation Process

NASM Package Installation Process

Step 1:
Click here to
Download package from here

Step2:
Extract package at home
Unpack the archive into a directory, which creates a subdirectory nasm-X. XX.

Step 3:
cd to nasm-X. XX and type “./configure” . This shell script will find the best C compiler to use and set up Makefiles accordingly.

Step 4:
Type make to build the nasm and ndisasm binaries.

Step 5:
Type “sudo make install” to install nasm and ndisasm in /usr/local/bin and to install the man pages.

Download Book for Assembly Language Practice

Categories
SE Computer Engineering

12. Write 80387 ALP to find the roots of the quadratic equation. All the possible cas es must be considered in calculating the roots.

section .data
msg1 db 10,13,”Complex Root”
msglen1 equ $-msg1

msg2 db 10,13,” Root1: “
msglen2 equ $-msg2

msg3 db 10,13,” Root2: “
msglen3 equ $-msg3

a dd 1.00
b dd 8.00
c dd 15.00
four dd 4.00
two dd 2.00

hdec dq 100
point db “.”

section .bss

    root1 resd 1
    root2 resd 1
    resbuff rest 1
    temp resb 2
    disc resd 1

%macro write 2            ;macro for display
    mov rax,1
    mov rdi,1
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro read 2            ;macro for input
    mov rax,0
    mov rdi,0
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro exit 0            ;macro for exit
    mov rax,60
    xor rdi,rdi
    syscall
%endmacro

section .text
  global _start
  _start:

  finit                ; initialise 80387 co-processor
  fld dword[b]            ; stack: b

  fmul dword[b]             ; stack: b*b

  fld dword[a]            ; stack: a, b*b

  fmul dword[c]             ; stack: a*c, b*b

  fmul dword[four]        ; stack: 4*a*c,b*b

  fsub                 ; stack: b*b – 4*a*c

  ftst                 ; compares ST0 and 0


  fstsw ax                ;Stores the coprocessor status word ;into either a word in memory or the AX register
  sahf        ;Stores the AH register into the FLAGS register.
  jb no_real_solutions         ; if disc < 0, no real solutions
  fsqrt             ; stack: sqrt(b*b – 4*a*c)

  fst dword[disc]     ; store disc= sqrt(b*b – 4*a*c)

  fsub dword[b]                 ; stack: disc-b

  fdiv dword[a]             ; stack: disc-b/2*a or (-b+disc)/2a

  fdiv dword[two]

  write msg2,msglen2
 call disp_proc
  fldz                ;stack:0
  fsub dword[disc]        ;stack:-disc
  fsub dword[b]             ; stack: -disc – b
  fdiv dword[a]        ; stack: (-b – disc)/(2*a)
  fdiv dword[two]

  write msg3,msglen3
  call disp_proc
  jmp exi

no_real_solutions:
write msg1,msglen1
exi :

mov rax,60
mov rdi,1
syscall

disp_proc:
    FIMUL dword[hdec]
    FBSTP tword[resbuff]
    mov rsi,resbuff+9
    mov rcx,09
  next1:

      push rcx
      push rsi

      mov bl,[rsi]
      call disp

      pop rsi
      pop rcx

       dec rsi
      loop next1
    push rsi
      write point,1
    pop rsi
      mov bl,[rsi]
      call disp
    ret

disp:
        mov edi,temp                ;mov dnum address into edi
        mov ecx,02                    ;initialize ecx with 2
        dispup1:
            rol bl,4                ;rotate bl by 4 bits
            mov dl,bl                ;move bl into dl
            and dl,0fh            ;and of dl with 0fh
            add dl,30h            ;add 30h into dl
            cmp dl,39h            ;compare dl with 39h
            jbe dispskip1            ;jump if below and equal to dispskip1
            add dl,07h            ;add 7h into dl
            dispskip1:
                mov [edi],dl        ;mov dl into dnum
                inc edi            ;increament edi by a byte
            loop dispup1        ;loop dispup1 while ecx not zero
            write temp,2            ;Display dnum by calling macro
          ret                    ;return from procedure

OUTPUT:

swlab@swlab-H81-M1:~$ nasm -f elf64 square.asm
swlab@swlab-H81-M1:~$
 ld -o mean mean.o
swlab@swlab-H81-M1:~$
 ./squareRoot1: 800000000000000003.00
Root2: 800000000000000005.00
swlab@swlab-H81-M1:~$

Categories
SE Computer Engineering

11. Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation Also plot the histogram for the data set.

section .data

msg1 db 10,13,’mean is:  ‘
msg1len equ $- msg1

msg2 db 10,13, ‘std deviation is:’
msg2len equ $- msg2

msg3 db 10,13, ‘variance is:’
msg3len equ $- msg3

data dd 9.0,1.0
datacnt dw 02
hdec dq 100

decpt db ‘.’

section .bss

res rest 01
mean resd 01
var resd 01
dispbuff resb 01

%macro disp 2
    mov eax,04
    mov ebx,01
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro
%macro accept 2
    mov eax,03
    mov ebx,00
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro

section .text
global _start
_start:

disp msg1,msg1len

       finit
    fldz
    mov rbx,data
    mov rsi,00
    xor rcx,rcx
    mov cx,[datacnt]

bk:    fadd dword [rbx+rsi*4]
    inc rsi
    loop bk

    fidiv word[datacnt]
    fst dword[mean]

    call dispres

    MOV RCX,00
    MOV CX,[datacnt]
    MOV RBX,data
    MOV RSI,00
    FLDZ
up1:    FLDZ
    FLD DWORD[RBX+RSI*4]
    FSUB DWORD[mean]
    FST ST1
    FMUL
    FADD
    INC RSI
    LOOP up1
    FIDIV word[datacnt]
    FST dWORD[var]
    FSQRT

    disp msg2,msg2len
    CALL dispres

    FLD dWORD[var]
    disp msg3,msg3len
    CALL dispres


exit:     mov eax,01
    mov ebx,00
    int 80h

disp8_proc:
    mov rdi,dispbuff
    mov rcx,02
back:    rol bl,04
    mov dl,bl
    and dl,0FH
    cmp dl,09
    jbe next1
    add dl,07H
next1:  add dl,30H
    mov [rdi],dl
    inc rdi
    loop back
    ret

dispres:
    fimul dword[hdec]
    fbstp tword[res]
    xor rcx,rcx
    mov rcx,09H
    mov rsi,res+9
up2:    push rcx
    push rsi
    mov bl,[rsi]
    call disp8_proc
    disp dispbuff,2 
    pop rsi
    dec rsi
    pop rcx
    loop up2
    disp decpt,1

    mov bl,[res]
    call disp8_proc
    disp dispbuff,2 
    ret

OUTPUT:
swlab@swlab-H81-M1:~$ nasm -f elf64 mean.asm
swlab@swlab-H81-M1:~$ ld -o mean mean.o
swlab@swlab-H81-M1:~$ ./mean

mean is:  000000000000000005.00
std deviation is:000000000000000004.00
variance is:000000000000000016.00

swlab@swlab-H81-M1:~$

Categories
SE Computer Engineering

10. Write x86 ALP to find the factorial of a given integer number on a command line by using recursion. Explicit stack manipulation is expected in the code.

;Problem Statement: Write x86 ALP to find the factorial of a given integer ;number on a command line by using
;recursion. Explicit stack manipulation is expected in the code.

%macro scall 4   ;common macro for input/output
 mov rax,%1
 mov rdi,%2
 mov rsi,%3
 mov rdx,%4
 syscall
%endmacro

section .data
 num db 00h
 msg db “Factorial is : “
 msglen equ $-msg
 msg1 db “*****Program to find Factorial of a number***** “,0Ah
   db “Enter the number : “,
 msg1len equ $-msg1

 zerofact db ” 00000001 “
 zerofactlen equ $-zerofact

section .bss
 dispnum resb 16
 result resb 4
 temp resb 3

section .text
global _start
_start:

 scall 1,1,msg1,msg1len
 scall 0,0,temp,3   ;accept number from user
 call convert       ;convert number from ascii to hex
 mov [num],dl

 scall 1,1,msg,msglen

 mov rdx,0        ;RMD
 mov rax,0        ;RMD
 mov al,[num]   ;store number in accumulator
 cmp al,01h 
 jbe endfact
 mov rbx,0        ;RMD
 mov bl,01h
 call factr    ;call factorial procedure
 call display

 call exit
endfact:
 scall 1,1,zerofact,zerofactlen
 call exit

 factr:    ;recursive procedure

   cmp rax,01h
   je retcon1
   push rax 
   dec rax

   call factr

  retcon:
   pop rbx
   mul ebx
   jmp endpr

  retcon1:   ;if rax=1 return
   pop rbx
   jmp retcon
  endpr:

 ret

 display:   ; procedure to convert hex to ascii

   mov rsi,dispnum+15
   mov rcx,0        ;RMD
   mov cl,16

  cont:
   mov rdx,0        ;RMD
   mov rbx,0        ;RMD
   mov bl,10h
   div ebx
   cmp dl,09h
   jbe skip
   add dl,07h
  skip:
   add dl,30h
   mov [rsi],dl
   dec rsi
   loop cont

   scall 1,1,dispnum,16

 ret

 convert:   ;procedure to convert ascii to hex
   mov rsi,temp
   mov cl,02h
   MOV rax,0        ;RMD
   mov rdx,0        ;RMD
  contc:
   rol dl,04h
   mov al,[rsi]
   cmp al,39h
   jbe skipc
   sub al,07h
  skipc:
   sub al,30h
   add dl,al
   inc rsi
   dec cl
   jnz contc

 ret

 exit:    ;exit system call

   mov rax,60
   mov rdi,0
   syscall

ret

OUTPUT:
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ nasm -f elf64 fact.asm
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ld -o fact fact.o
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ./fact
*****Program to find Factorial of a number*****
Enter the number : 03
Factorial is : 0000000000000006
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$