BCSL-056 Solved Free Assignment 2024-25 Sem 5

Q1 

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

 The TCP client program sends two strings to the TCP server program to find length of these to strings and return the sum of lenghts of these two strings. Also the TCP server program sends the concatenated strings to the client. 

(b) Run the following Linux commands on your machine and show the output: 

 cat 

 sort 

 ping 

 more

  df-h 

 tail - f

Ans:-    (a): TCP Client and Server Program in C


Here’s a simple example of a TCP client and server in C, where the client sends two strings to the server. The server calculates the lengths, returns the sum, and sends back the concatenated strings.


TCP Server Code (server.c)


```c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <netinet/in.h>

#include <sys/socket.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 str1[BUFFER_SIZE], str2[BUFFER_SIZE];

    

    // Create socket

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

        perror("Socket failed");

        exit(EXIT_FAILURE);

    }

    

    // Define the server address

    address.sin_family = AF_INET;

    address.sin_addr.s_addr = INADDR_ANY;

    address.sin_port = htons(PORT);

    

    // Bind socket to address

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

        perror("Bind failed");

        exit(EXIT_FAILURE);

    }

    

    // Listen for incoming connections

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

        perror("Listen failed");

        exit(EXIT_FAILURE);

    }

    

    printf("Server is listening on port %d\n", PORT);

    

    // Accept incoming connection

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

        perror("Accept failed");

        exit(EXIT_FAILURE);

    }

    

    // Read strings from client

    read(new_socket, buffer, BUFFER_SIZE);

    sscanf(buffer, "%s %s", str1, str2);

    

    // Calculate lengths and concatenate strings

    int len1 = strlen(str1);

    int len2 = strlen(str2);

    int total_len = len1 + len2;

    char concatenated[BUFFER_SIZE];

    snprintf(concatenated, BUFFER_SIZE, "%s%s", str1, str2);

    

    // Send response to client

    snprintf(buffer, BUFFER_SIZE, "Length Sum: %d\nConcatenated String: %s", total_len, concatenated);

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

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

    

    // Close sockets

    close(new_socket);

    close(server_fd);

    

    return 0;

}

```


 TCP Client Code (client.c)


```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 buffer[BUFFER_SIZE] = {0};

    char str1[BUFFER_SIZE], str2[BUFFER_SIZE];

    

    // Get input strings from user

    printf("Enter first string: ");

    scanf("%s", str1);

    printf("Enter second string: ");

    scanf("%s", str2);

    

    // Create socket

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

        printf("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("Invalid address/ Address not supported\n");

        return -1;

    }

    

    // Connect to server

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

        printf("Connection Failed\n");

        return -1;

    }

    

    // Send strings to server

    snprintf(buffer, BUFFER_SIZE, "%s %s", str1, str2);

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

    

    // Receive response from server

    int valread = read(sock, buffer, BUFFER_SIZE);

    buffer[valread] = '\0';

    printf("Server response: %s\n", buffer);

    

    // Close socket

    close(sock);

    

    return 0;

}

```


 Compilation and Execution

1. Compile the server and client programs:

   ```bash

   gcc server.c -o server

   gcc client.c -o client

   ```


2. Run the server in one terminal:

   ```bash

   ./server

   ```


3. In another terminal, run the client:

   ```bash

   ./client

   ```


 Explanation

- The client sends two strings to the server.

- The server calculates the lengths of these strings, sums them, and concatenates the strings.

- It then sends the length sum and concatenated string back to the client.

- The client displays the server’s response.


---


(b): Linux Commands Execution


To demonstrate output, here’s a brief on each command and sample usage:


1. **`cat`**:

   - Displays the contents of a file. Usage example:

     ```bash

     cat filename.txt

     ```


2. **`sort`**:

   - Sorts the lines of a text file alphabetically. Usage example:

     ```bash

     sort filename.txt

     ```


3. **`ping`**:

   - Sends network packets to a specified address to check network connectivity. Usage example:

     ```bash

     ping google.com

     ```


4. **`more`**:

   - Paginates the display of a file’s content, allowing the user to scroll through it. Usage example:

     ```bash

     more filename.txt

     ```


5. **`df -h`**:

   - Displays available disk space on each mounted filesystem in a human-readable format. Usage example:

     ```bash

     df -h

     ```


6. **`tail -f`**:

   - Continuously displays the last part of a file, useful for monitoring log files in real time. Usage example:

     ```bash

     tail -f logfile.txt

     ```

By running each command in the terminal, you’ll receive output directly from your machine.


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 a protocol for remotely accessing and managing a server. Here’s a guide for configuring and testing a Telnet server in Linux.


 Steps to Configure Telnet Server


1. **Install Telnet Server Package:**

   - Open a terminal and install the `telnetd` server package:

     ```bash

     sudo apt update

     sudo apt install telnetd -y

     ```

   - If you're using a different Linux distribution (like CentOS), you may need to use:

     ```bash

     sudo yum install telnet-server -y

     ```


2. **Enable the Telnet Service:**

   - Start and enable the Telnet service:

     ```bash

     sudo systemctl start inetd

     sudo systemctl enable inetd

     ```


3. **Allow Telnet Through Firewall (If Required):**

   - Open port 23 in the firewall to allow Telnet connections:

     ```bash

     sudo ufw allow 23/tcp

     sudo ufw reload

     ```


4. **Configure `telnetd` to Allow Specific Users (Optional):**

   - Edit the Telnet configuration file to specify which users can access Telnet:

     ```bash

     sudo nano /etc/hosts.allow

     ```

   - Add an entry like:

     ```

     in.telnetd: ALL

     ```

   - Restart the service if necessary:

     ```bash

     sudo systemctl restart inetd

     ```


 Testing the Telnet Server


1. **Connect to the Telnet Server from Another Machine:**

   - On a client machine, open the terminal and run:

     ```bash

     telnet <server_ip>

     ```

   - Replace `<server_ip>` with the IP address of the Telnet server.


2. **Log In Using Credentials:**

   - After connecting, you’ll be prompted to enter a username and password. Use a valid system user account.


3. **Verify Connection:**

   - Once logged in, you can execute commands on the remote server through Telnet.


#### Note:

Telnet is an unencrypted protocol and not recommended for secure environments. Use SSH for secure remote access.


---


 (b): Configure DHCP Server on Linux


A DHCP server automatically assigns IP addresses to devices on a network. Here are the steps to configure a DHCP server on Linux.


Steps to Configure DHCP Server


1. **Install DHCP Server Package:**

   - Install the `isc-dhcp-server` package:

     ```bash

     sudo apt update

     sudo apt install isc-dhcp-server -y

     ```


2. **Edit the DHCP Configuration File:**

   - Open the DHCP configuration file in a text editor:

     ```bash

     sudo nano /etc/dhcp/dhcpd.conf

     ```


3. **Define the DHCP Parameters:**

   - Add the following configurations to specify the IP range and network settings. For example:

     ```bash

     subnet 192.168.1.0 netmask 255.255.255.0 {

         range 192.168.1.10 192.168.1.100;

         option routers 192.168.1.1;

         option subnet-mask 255.255.255.0;

         option domain-name-servers 8.8.8.8, 8.8.4.4;

         option domain-name "example.com";

     }

     ```

   - Replace IP addresses and DNS servers as appropriate for your network.


4. **Specify the Network Interface for DHCP Server:**

   - Edit the network interfaces file to specify the interface on which DHCP should listen:

     ```bash

     sudo nano /etc/default/isc-dhcp-server

     ```

   - Set `INTERFACESv4` to the interface (e.g., `eth0`):

     ```bash

     INTERFACESv4="eth0"

     ```


5. **Restart the DHCP Server:**

   - After saving the configuration, restart the DHCP server to apply changes:

     ```bash

     sudo systemctl restart isc-dhcp-server

     ```


6. **Check the Status of DHCP Server:**

   - Verify the DHCP server status:

     ```bash

     sudo systemctl status isc-dhcp-server

     ```

   - Make sure it’s running without errors.


7. **Configure DHCP Server to Start on Boot:**

   - Enable the DHCP server to start at boot:

     ```bash

     sudo systemctl enable isc-dhcp-server

     ```


 Testing DHCP Server


1. **Connect a Client to the Network:**

   - Connect a device to the network and set it to use DHCP for IP assignment.

   - Verify that it receives an IP address within the range defined in your DHCP configuration.


2. **Verify IP Assignment:**

   - Check the device’s IP settings to ensure it received the correct IP, gateway, and DNS server information from the DHCP server.


Sorting Table Results

If your DHCP configuration logs IPs or leases in a table, you can sort this data by columns using the `sort` command.


For example, if the lease details are in a file (`leases.txt`), you can sort by IP addresses as follows:


```bash

sort -k1 leases.txt

```

This sorts the table by the first column, which is often the IP address column. You can change `-k1` to `-k2`, `-k3`, etc., to sort by other columns.

No comments: