C Datastructures Networking OS

Saturday 20 April 2013

Usage of constant qualifier with pointer

Difference between  const * int ptr  and   int * const ptr...

First of all we need to understand what const qualifier is and how it is used.
            Declaration of variable with const qualifier specifies that its value will not be changed. It depends upon where const variables are stored ,we may change value of const variable by using pointer.The result is implementation-defined if an attempt is made to change a const.
                  #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                          const int a=100;
                          a+=100;                                //error
                          printf("value of a=%d",a);
                             
                  }
               
 o/p: assignment of read only variable 'a'
1)pointer to variable:-
          
                     int *ptr;
            In the above declaration the value of both variable and pointer can be changed.Both the pointer and the value pointed by pointer will be stored in read-write area of memory.

                 #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                           int a=100,b=200;
                           int *ptr;
                           ptr=&a;                                        //pointer to integer
                           printf("value of ptr=%d",*ptr);
                           ptr=&b;                                       //pointing to another variable
                           printf("value of ptr=%d",*ptr);
                           *ptr=500;                                   //changing the value of pointer
                            printf("value of ptr=%d",*ptr);
                  }
               
           o/p:
                     value of ptr=100;
                     value of ptr=200;
                     value of ptr=500;
2) Pointer to constant :
          const int * ptr;
                  or
          int const * ptr;

We change pointer to point to another variable,but we cannot change the value of the variable which is pointed by the pointer.Pointer is stored in read-write memory and variable is stored in read-only memory.
                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const int *ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         ptr=&j;                                             //valid
                         printf("value of ptr=%d",*ptr);
               
                        *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                 }
            
o/p:- error.assignment to read only location *ptr
3)Constant Pointer to variable:-

                int  *const ptr;

             We can change the value of variable pointed by the pointer, but we cannot change the pointer to point to another variable.

                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // valid.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 }     

         o/p: error in assignment to read only location ptr
4) constant pointer to constant variable:
              We cannot change both the value of variable pointed by the pointer and  also pointer to point to another variable.

                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const  int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 } 
 o/p:     error.assignment to read only location *ptr
            error in assignment to read only location ptr

Wednesday 10 April 2013

Socket vs Port...

SOCKET : -

A TCP connection is a 5-tuple organisation.
      It consists of Client Ip,Client port number,Server Ip,Server port,protocol.
     Combination of IP address and Port number is known as endpoint and sometimes called as socket.
    
PORT:-
    The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that     a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single         network interface possible.

TCP VS UDP

TCP (Transmission Control Protocol):-
It is the most commonly used protocol on the Internet. The reason for this is because TCP offers error correction.
 When the TCP protocol is used there is a "guaranteed delivery." This is due largely in part to a method called "flow control."
 Flow control determines when data needs to be re-sent, and stops the flow of data until previous packets are successfully transferred. 
This works because if a packet of data is sent, a collision may occur. 
When this happens, the client re-requests the packet from the server until the whole packet is complete and is identical to its original. 

UDP (User Datagram Protocol): -


This is another commonly used protocol on the Internet. However, UDP is never used to send important data such as webpages, database information, etc;
 UDP is commonly used for streaming audio and video. Streaming media such as Windows Media audio files (.WMA) , Real Player (.RM), and others use UDP because it offers speed! The reason 

UDP is faster than TCP is because there is no form of flow control or error correction. The data sent over the Internet is affected by collisions, and errors will be present. Remember that UDP is only concerned with speed. This is the main reason why streaming media is not high quality.