BCSL-056 (Network Programming and Administration Lab)

     BCSL-056  Network Programming and Administration Lab (Assignments Solution)

    Course Code                        :            BCS-056 

    Course Title                         :            Network Programming and Administration Lab

    Assignment Number            :           BCA(V)051/Assignment/2024-25 

    Maximum Marks                 :             50

    Weightage                            :            25% 

    Last Date of Submission      :           31stOctober,2024(For July, Session) 

                                                               30thApril, 2025(For January, Session)


Q1 (a): Write and execute a TCP client and a server program in C-language to perform the following tasks: 

Ans. 

TCP Server Program (C)

The server listens for connections from a client, accepts the connection, and then responds to the client's message.

// TCP Server in C


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>


#define PORT 8080

#define BUFFER_SIZE 1024


int main() {

    int server_fd, new_socket;

    struct sockaddr_in address;

    int addrlen = sizeof(address);

    char buffer[BUFFER_SIZE] = {0};

    char *response = "Hello from server";


    // Creating socket file descriptor

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {

        perror("socket failed");

        exit(EXIT_FAILURE);

    }


    // Binding the socket to the port

    address.sin_family = AF_INET;

    address.sin_addr.s_addr = INADDR_ANY;

    address.sin_port = htons(PORT);


    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {

        perror("bind failed");

        exit(EXIT_FAILURE);

    }


    // Listening for incoming connections

    if (listen(server_fd, 3) < 0) {

        perror("listen failed");

        exit(EXIT_FAILURE);

    }


    printf("Server is waiting for a connection...\n");


    // Accepting the incoming connection

    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {

        perror("accept failed");

        exit(EXIT_FAILURE);

    }


    // Reading the message from the client

    read(new_socket, buffer, BUFFER_SIZE);

    printf("Message from client: %s\n", buffer);


    // Sending a response to the client

    send(new_socket, response, strlen(response), 0);

    printf("Response sent to client\n");


    close(new_socket);

    close(server_fd);

    return 0;

}

TCP Client Program (C)

The client connects to the server, sends a message, and then waits for the server's response.

// TCP Client in C


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>


#define PORT 8080

#define BUFFER_SIZE 1024


int main() {

    int sock = 0;

    struct sockaddr_in serv_addr;

    char *message = "Hello from client";

    char buffer[BUFFER_SIZE] = {0};


    // Creating socket file descriptor

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {

        printf("\n Socket creation error \n");

        return -1;

    }


    serv_addr.sin_family = AF_INET;

    serv_addr.sin_port = htons(PORT);


    // Convert IPv4 and IPv6 addresses from text to binary form

    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {

        printf("\nInvalid address / Address not supported \n");

        return -1;

    }


    // Connecting to the server

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {

        printf("\nConnection failed \n");

        return -1;

    }


    // Sending message to the server

    send(sock, message, strlen(message), 0);

    printf("Message sent to server\n");


    // Receiving server's response

    read(sock, buffer, BUFFER_SIZE);

    printf("Response from server: %s\n", buffer);


    close(sock);

    return 0;

}

Steps to Compile and Run

  1. Compile the server:

gcc -o server tcp_server.c

Compile the client:

gcc -o client tcp_client.c

Run the server (in one terminal):

./server

Run the client (in another terminal):

./client

Q2. (a) Configure and test the Telnet server in Linux. (b) Configure the DHCP server on the Linux operating system. Write all the steps involved in configuration. Sort each column of the table and show the result. 

Ans.

(a) Configure and Test the Telnet Server in Linux

Telnet is an old protocol used to access remote servers. Here's how to configure and test a Telnet server on Linux.

Steps to Configure the Telnet Server:

  1. Install the Telnet server package: First, ensure that the Telnet server is installed. On most Linux distributions, you can install the telnetd package using a package manager.

    For Debian/Ubuntu:

         sudo apt update
         sudo apt install telnetd
 
     Enable the Telnet service: If you are using xinetd, you need to enable the Telnet service by editing       the configuration file.

     Open the /etc/xinetd.d/telnet file:

      sudo nano /etc/xinetd.d/telnet

  1. Add or modify the configuration based on your network settings:


    # Set the domain name and domain name servers option domain-name "example.com"; option domain-name-servers ns1.example.com, ns2.example.com; # Set the default lease time and maximum lease time default-lease-time 600; max-lease-time 7200; # Configure the network segment the DHCP server will serve subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.10 192.168.1.100; # IP range to assign option routers 192.168.1.1; # Default gateway option broadcast-address 192.168.1.255; }
  2. Assign Network Interfaces: Specify which network interfaces should run the DHCP service. Edit /etc/default/isc-dhcp-server (for Debian-based systems) or /etc/sysconfig/dhcpd (for CentOS/RHEL).

    Debian/Ubuntu:

    INTERFACESv4="eth0"

    CentOS/RedHat:

    DHCPDARGS="eth0"
  3. Restart the DHCP Service: Restart the DHCP server service to apply the new configuration.

    For Debian/Ubuntu:

    sudo systemctl restart isc-dhcp-server

    For RedHat/CentOS:

    sudo systemctl restart dhcpd
  4. Allow DHCP through the Firewall: If you have a firewall enabled, allow DHCP communication through the firewall.

    For ufw (on Debian/Ubuntu):

    sudo ufw allow 67/udp sudo ufw reload

    For firewalld (on CentOS/RHEL):


    sudo firewall-cmd --add-service=dhcp --permanent sudo firewall-cmd --reload
  5. Test the DHCP Server: Test the DHCP server by configuring a client to obtain an IP address automatically and checking if it gets an IP from the specified range.


Sorting a Table in Linux

You can sort columns of a table or a file by using the sort command in Linux. Here's how to sort a table:

  1. Create a sample file with tabular data: Let's say you have a file named data.txt with the following content:

    Name Age Salary John 28 5000 Jane 30 6000 Mike 25 4000 Amy 35 7000
  2. Sort the table by the second column (Age): Use the sort command with the -k option to specify the column number.

    sort -k2 -n data.txt
  3. Sort the table by the third column (Salary):

    sort -k3 -n data.txt

The sorted output will be printed on the screen. Use the -r option to sort in reverse order if needed:


sort -k3 -nr data.txt


  #CODEWITHCHIRAYU

                                                              Thank You 😊                                                            



Comments

Popular posts from this blog

IGNOU Term End December2024 Exam Result

Top 12 (Most Impotant Questions) BCS-051 : INTRODUCTION TO SOFTWARE

COMPANY SECRETARIES EXAMINATION – CS Examination Admit Cards