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
Compile the server:
(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:
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:
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
Comments
Post a Comment