CS56 Networks Lab

Embed Size (px)

Citation preview

  • 7/27/2019 CS56 Networks Lab

    1/12

    EINSTEINCOLLEGE OF ENGINEERINGSir.C.V.Raman Nagar, Tirunelveli-12

    Department of CSESubject Code: CS56

    NETWORKS LAB

    Name :

    Reg No :

    Branch :

    Year & Semester :

  • 7/27/2019 CS56 Networks Lab

    2/12

    CS56

    Einstein College of Engineering

    Page 2 of 12

    Ex.No

    Date:

    Echo Server and Client

    Aim:

    Aim of this exercise is to implement Echo server and client using TCP socket

    Algorithm- Server- echoes message sent by client

    1. Create a TCP server socket.

    2. Allocate memory using memset for data exchange.

    3. Set initial parameters like sin_family and sin_port of server socket.4. Bind the server socket.

    5. Keep listening for client connection.

    6. Once an invitation for connection is sensed, accept the connection.

    7. Receive bytes from the accepted connection and echo it.8. If q or Q is received from client, close the connection.

    9. Terminate the program.

    Algorithm- Client- sends message to server

    1. Create a TCP client socket.2. Allocate memory using memset for data exchange.

    3. Set initial parameters like sin_family and sin_port of server socket.

    4. Read data to be echoed in the server.

    5. Connect the client socket to server.

    6. Send data through the established connection.

    7. Send q or Q to client when all data have been sent.8. Close the connection.

  • 7/27/2019 CS56 Networks Lab

    3/12

    CS56

    Einstein College of Engineering

    Page 3 of 12

    Ex.No

    Date:

    Date and Time using TCP

    Aim:

    Aim of this exercise is to implement Date time server and client using TCP socket

    Algorithm- Server- echoes date and time sent by client

    1. Create a TCP server socket.

    2. Allocate memory using memset for data exchange.

    3. Set initial parameters like sin_family and sin_port of server socket.

    4. Bind the server socket.

    5. Keep listening for client connection.6. Once an invitation for connection is sensed, accept the connection.

    7. Receive date and time from the accepted connection and echo it.8. If q or Q is received from client, close the connection.

    9. Terminate the program.

    Algorithm- Client- sends date and time to server

    1. Create a TCP client socket.

    2. Allocate memory using memset for data exchange.

    3. Set initial parameters like sin_family and sin_port of client socket.

    4. Read system date and time to be echoed in the server.

    5. Connect the client socket to server.

    6. Send date and time through the established connection.

    7. Send q or Q to client when all data have been sent.8. Close the connection.

  • 7/27/2019 CS56 Networks Lab

    4/12

    CS56

    Einstein College of Engineering

    Page 4 of 12

    Ex.No

    Date:

    Echo Server and Client using UDP

    Aim:

    Aim of this exercise is to implement echo server and client using UDP socket

    Algorithm- Server- echoes data sent by client

    1. Create a UDP socket.

    2. Set initial parameters like sin_family ,sin_port and sin_addr of server

    socket.

    3. Bind the socket to client.

    4. Waits for client in the port number.5. Once data reaches through the port, it is received and echoed.

    6. Terminate the program.

    Algorithm- Client- sends data to server

    1. Create a UDP client socket.2. Set initial parameters like sin_family ,sin_port and sin_addr of client

    socket.

    3. Read input message.

    4. Send to server.

    5. Terminate the program.

  • 7/27/2019 CS56 Networks Lab

    5/12

    CS56

    Einstein College of Engineering

    Page 5 of 12

    Ex.No

    Date:

    Domain Name Server using TCP

    Aim:

    Aim of this exercise is to implement Domain Name Server using TCP socket

    Algorithm-Input File Creation

    1. Declare a file pointer and a file is opened in w mode.

    2. A set of entries to be filled up in DNS table are read one by one.

    3. The records are written in file using file pointer.

    4. Once the writing process is over, close the file pointer.

    Algorithm-Client Client resolves IP Address to Host Name and sends to Server

    Each DNS record is declared as a structure with the following fields.

    1. IP Address and2. Host Name

    1. A TCP client socket is created.2. Initial parameters like sin_family, sin_port and sin_addr are set.

    3. Connect the client socket with the server.

    4. Receive the IP address sent by server.

    5. Open the input file created in r mode.

    6. Read each record compare the received IP address with the IP Address field in

    the DNS record.

    7. If a match is found, print the Host Name.

    8. Send Host Name to server.9. Close the TCP connection.

    Algorithm-Server Reads IP Address form user and sends to Client

    1. Create a TCP server socket.

    2. Initial parameters like sin_family, sin_port and sin_addr are set.

    3. Bind the server socket with the client.4. Listen in the port for client connection.

    5. Accept the clients connection when client initiates.

    6. Read IP Address from user.

    7. Send it to client.

    8. If q or Q is sent by client, server closes the connection.9. Else prints the Host Name sent by client.

    10. Loop back to step 6.

  • 7/27/2019 CS56 Networks Lab

    6/12

    CS56

    Einstein College of Engineering

    Page 6 of 12

    Ex.No

    Date:

    Remote Procedure Call

    Aim:

    Aim of this exercise is to implement Remote Procedure Call (RPC).

    Algorithm

    (i) msg.x

    1. The prototype of the remote procedure PRINTMSG is given. It takes up a string

    argument and returns an integer.

    2. Every remote procedure is assigned a unique hexa decimal number like0x20000099.

    (ii) msg_server.c (the implementation of the remote procedure (i.e) the callee

    program in remote server)

    1. The string message received as input argument from the client machine is printed inthe remote server.

    2. Once it is successfully printed in the remote server, a static integer is set to 1 and

    returned to the caller program in client machine.

    (iii) msg_client.c (the caller program in client machine)

    1. Create a client handle.

    2. The input message to be displayed is accepted as third argument from the clientmachine.

    3. Call the remote procedure in remote server (RPC) from client machine with theinput message as the input argument.

    4. If static integer result sent from remote server holds 1, then the message is

    successfully delivered. Else an error has occurred.

    Procedure for compiling and running the program

    Type msg.x and compile using rpcgen as follows.

    >rpcegen msg.x

    >lsmsg_svc.c msg_clnt.c msg.x msg.h

    >rpcgen Ss msg.x

    RPC server code is displayed. Copy and save in msg_server.c

    >rpcgen Sc msg.x

  • 7/27/2019 CS56 Networks Lab

    7/12

    CS56

    Einstein College of Engineering

    Page 7 of 12

    RPC client code is displayed. Copy and save in msg_client.c

    To Compile

    >gcc o msg_server msg_server.c msg_svc.c lnsl

    >gcc o msg_client msg_client.c msg.clnt.c lnsl

    To run

    localhost> msg_server root

    localhost> msg_client localhost HAI

    where HAI is the input message to be displayed in the remote server.

  • 7/27/2019 CS56 Networks Lab

    8/12

    CS56

    Einstein College of Engineering

    Page 8 of 12

    Ex.No

    Date:

    Simulation of Sliding Window

    Aim:

    Aim of this exercise is to simulate the functioning of Sliding Window, flow control

    protocol.

    Algorithm Receiver

    Each Data frame is declared as a structure with the following fields.

    1. Sequence Number

    2. Left Pointer and

    3. Right Pointer

    Steps:

    1. TCP socket is created.

    2. Initial parameters like sin_family, sin_port and sin_addr are set.3. Allocate memory for message transfer.

    4. Connect to the senders socket.5. Receive data frame from sender.

    6. Check if it is q or Q to close the connection else continue.7. Print the received frame and its sequence number.

    8. Increment the left pointer of the sliding window.

    9. Display the sliding window entries between left and right pointers.

    10. Send string ACK to sender. If connection is to be closed, send q or Q to

    sender.

    11. Increment right pointer and loop back to step 5.

    Algorithm Sender

    Steps:

    1. TCP client socket is created.

    2. Initial parameters like sin_family, sin_port and sin_addr are set.3. Bind server socket.

    4. Listen in the port for connection.5. Accept the receivers connection.

    6. Data frame is read and sent to the receiver.

    7. Left pointer and sequence number are incremented.

    8. Receive the ACK sent by receiver.

    9. Check if it is q or Q, and then close the connection.

    10. Else loop back form step 6.

  • 7/27/2019 CS56 Networks Lab

    9/12

    CS56

    Einstein College of Engineering

    Page 9 of 12

    Ex.No

    Date:

    Raw Sockets

    Aim:

    Aim of this exercise is to implement a C program using raw sockets.

    Algorithm_Checksum Calculation

    1. The function takes up the 16 bit binary number and its length as the input

    arguments.

    2. The sum of individual 16 bit data units is found.

    3. If carry is generated exceeding its length equal to 16 bits, then 1 is added to LSB of

    sum.4. Invert the sum value and that gives the checksum.

    Algorithm_Display

    1. Open up the message and display the message along with the IP and ICMP header

    details into which it is present.

    Algorithm Listener

    1. Create a raw socket.

    2. ICMP request/reply is received.

    3. displays bytes received

    Algorithm-Ping

    1. Create a raw socket.2. Receive packet.

    3. Display Got Message

    4. Set the following fields of ICMP Packet header like type, id, msg, sequence

    and checksum.

    5. Sleep6. Loop back to step 2.

    Algorithm Main

    1. Set Protocol as ICMP.2. Set host value.

    3. Initial parameters like sin_family, sin_port and sin_addr are set.

    4. If fork() returns 0 means parent(listener()) is executed else if positive value is

    returned a child process is created and ping() is executed. When ping() sleeps,

    listener() revives.

  • 7/27/2019 CS56 Networks Lab

    10/12

    CS56

    Einstein College of Engineering

    Page 10 of 12

    Built-in Structures and Important Functions

    1. struct in_addr {in_addr_t s_addr; // 32 bit network IP v4 addr

    // network byte offered

    };2. IPv4 socket address structure: sockaddr_in

    struct sockaddr_in{

    uint8_t sin_len; //length of structure (16)

    sa_family_t sin_family // AF_INET

    in_port_t sin_port //16 bit UDP or TCP port number

    struct in_addr sin_addr // 32 bit IPv4 address

    //network byte offeredchar sin_zero(8) //unused

    };

    3. Byte manipulation functions

    # include

    void bzero(void*dest, size_t nbytes);

    bzero sets the specified number of bytes to 0 in the destination. This function

    is used to initialize a socket address structure to 0.

    # include

    void *memset(void*dest, int c, size_t len);memset sets the specified number of bytes to value c in the destination.

    4. Address Conversion Functions

    # include

    int inet_aton(const char*strptr, struct in_addr*addrptr);

    in_addr_t inet_addr(const char*strptr);

    char *inet_ntoa(struct in_addrinaddr);

    inet_aton converts the character string pointed to by strptr into its 32 bit

    binary network byte ordered value which is stored through addrptr. If

    successful 1 is returned else 0 is returned.inet_addr function returns the 32 bit binary network byte ordered value. This

    function returns INADDR_NONE on error.

    inet_ntoa converts the 32 bit binary network byte ordered value into its its

    corresponding dotted decimal string.

    5. Socket function# include

    int socket(int family, int type, int protocol)

    returns positive value if OK else returns -1.

    family - AF_INET IPv4 Protocol

  • 7/27/2019 CS56 Networks Lab

    11/12

    CS56

    Einstein College of Engineering

    Page 11 of 12

    type -SOCK_STREAM - Stream socketSOCK_DGRAM - datagram socket

    SOCK_RAW -raw socketAF_INET with SOCK_STREAM supports TCP

    AF_INET with SOCK_DGRAM supports UDP

    6. CONNECT functionThe CONNECT function is used by a TCP client to establish a connection with a TCP

    server.

    # include

    int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

    returns 0 if OK, else returns -1.7. BIND function

    The BIND function returns a protocol address to a socket. A process can bind a

    specific

    IP address to its socket.

    # include

    int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);returns 0 if OK, else returns -1.

    8. LISTEN function

    The LISTEN function is used by TCP server.

    # includeint listen(int sockfd, int backlog);

    returns 0 if OK, else returns -1.

    9. ACCEPT function

    The ACCEPT function is called by a TCP server to return the next completed

    connection from the front of the completed connection queue.

    # include

    int connect(int sockfd, const struct sockaddr *cliaddr, socklen_t addrlen);

    returns 0 if OK, else returns -1.

    10. # include

    pid_t fork(void)returns 0 in child, process ID of child in parent, -1 on error.

    11. sendto andrecvfrom functions

    # include

    ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags,

    struct sockaddr *from,socklen_t *addrlen);

    ssize_t sendto(int sockfd, void *buff, size_t nbytes, int flags,const struct sockaddr *to,socklen_t *addrlen);

    both return number of bytes read/written if OK, else returns -1.

    sockfd -Descriptor

    *buff -Pointer to buffer to read into/write from

    nbytes - Number of bytes

    flags -

    For sendto

    *from -socket data structure containing the protocol address where

    data is to be sent

    addrlen - size of the above data structureFor recvfrom

  • 7/27/2019 CS56 Networks Lab

    12/12

    CS56

    Einstein College of Engineering

    Page 12 of 12

    *to - socket data structure containing the protocol address givingwho sent the datagram

    addrlen - size of the above data structure