BCSL056
BCSL056
BCSL056
Q1. (a) Write a UDP client and UDP server program in C language on
Unix/Linux, where client program interact with the Server as given
below: (12)
1. The client will send a string to the client.
2. Server program send an acknowledgement for receiving the
string.
3. Sever program will find the length of the sting and send it to
respective client.
4. Client will send the acknowledgement for receiving the response
to the server.
1
10. int main() {
11. struct sockaddr_in server_addr, client_addr;
12. int sockfd, n;
13. char buffer[BUFFER_SIZE];
14.
2
38. while (1) {
39. socklen_t len = sizeof(client_addr);
40.
51. close(sockfd);
52. return 0;
53. }
3
#define PORT 8080
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
int main() {
struct sockaddr_in server_addr;
int sockfd, n;
char buffer[BUFFER_SIZE];
// Creating socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Error in socket creation");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
// Server information
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
4
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
close(sockfd);
return 0;
}
2. UDP Server (server.c):
#include <stdio.h>
5
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
struct sockaddr_in server_addr, client_addr;
int sockfd, n;
char buffer[BUFFER_SIZE];
// Creating socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Error in socket creation");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
6
// Server information
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
while (1) {
socklen_t len = sizeof(client_addr);
7
printf("Received from client: %s\n", buffer);
close(sockfd);
return 0;
}
UDP Client (client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
8
#define BUFFER_SIZE 1024
int main() {
struct sockaddr_in server_addr;
int sockfd, n;
char buffer[BUFFER_SIZE];
// Creating socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Error in socket creation");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
// Server information
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
9
inet_pton(AF_INET, SERVER_IP,
&server_addr.sin_addr);
close(sockfd);
return 0;
10
3. . UDP Server (server.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
// Create socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
11
exit(EXIT_FAILURE);
}
12
while (1) {
unsigned int len = sizeof(client_addr);
if (bytes_received < 0) {
perror("Error in recvfrom");
continue;
}
13
// Find the length of the received string
size_t str_len = strlen(buffer);
if (bytes_sent < 0) {
perror("Error in sendto");
continue;
}
}
close(sockfd);
return 0;
}
14
4. UDP Server (server.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[MAX_BUFFER_SIZE];
15
perror("socket creation failed");
exit(EXIT_FAILURE);
}
16
printf("Server is running and waiting for data...\
n");
while (1) {
// Receive data from the client
int received_len = recvfrom(sockfd, (char
*)buffer, MAX_BUFFER_SIZE,
MSG_WAITALL, (struct
sockaddr*)&client_addr, &addr_len);
17
(const struct sockaddr*)&client_addr,
sizeof(client_addr));
return 0;
}
UDP Client (client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
18
#include <unistd.h>
#include <arpa/inet.h>
int main() {
struct sockaddr_in server_addr;
int sockfd, n;
char buffer[BUFFER_SIZE];
// Creating socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Error in socket creation");
exit(1);
}
19
memset(&server_addr, 0, sizeof(server_addr));
// Server information
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_IP,
&server_addr.sin_addr);
close(sockfd);
return 0;
}
Q1. (b) Write the steps to install network monitor
application on Unix/Linux. Demonstrate the use of capture
filter and display filter with the help of examples for each.
(8)
Step 1: Installation:
-Open a terminal window.
-Update the package list:
sudo apt update
Install Wireshark:
21
sudo apt install wireshark
During the installation, you will be prompted to
configure Wireshark to allow non-superusers to
capture packets. Choose "Yes" to let non-root
users capture packets. This will add the user to
the "wireshark" group.
22
Step 3: Display Filters
Display filters allow you to filter and display
specific packets from the captured data based on
specific criteria.
23
Q2. (a) Run the following Linux commands on
your machine and show the output: (8)
1.cat- (concatenate and display content of a file):
For example, if you have a file named "example.txt" with
the following content:
Hello, this is an example text file.
Running cat example.txt would output:
Hello, this is an example text file
24
Running ping www.google.com would send ICMP echo
requests to Google's servers, and you would see output
similar to this:
PING www.google.com (142.250.185.132) 56(84) bytes of
data.
64 bytes from dfw28s05-in-f4.1e100.net (142.250.185.132):
icmp_seq=1 ttl=117 time=20.1 ms
64 bytes from dfw28s05-in-f4.1e100.net (142.250.185.132):
icmp_seq=2 ttl=117 time=19.9 ms
64 bytes from dfw28s05-in-f4.1e100.net (142.250.185.132):
icmp_seq=3 ttl=117 time=20.2 ms
25