Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Course Code : BCSL-056

Course Title : Network Programming and Administration Lab


Assignment Number : BCA(V)/L-056/Assignment/2023-24
Maximum Marks : 50
Weightage : 25%

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. UDP Server (server.c):


1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <unistd.h>
5. #include <arpa/inet.h>
6.

7. #define PORT 8080


8. #define BUFFER_SIZE 1024
9.

1
10. int main() {
11. struct sockaddr_in server_addr, client_addr;
12. int sockfd, n;
13. char buffer[BUFFER_SIZE];
14.

15. // Creating socket


16. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
17. if (sockfd < 0) {
18. perror("Error in socket creation");
19. exit(1);
20. }
21.

22. memset(&server_addr, 0, sizeof(server_addr));


23. memset(&client_addr, 0, sizeof(client_addr));
24.

25. // Server information


26. server_addr.sin_family = AF_INET;
27. server_addr.sin_addr.s_addr = INADDR_ANY;
28. server_addr.sin_port = htons(PORT);
29.

30. // Binding socket to server address


31. if (bind(sockfd, (const struct sockaddr
*)&server_addr, sizeof(server_addr)) < 0) {
32. perror("Error in binding");
33. exit(1);
34. }
35.

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


37.

2
38. while (1) {
39. socklen_t len = sizeof(client_addr);
40.

41. // Receiving message from client


42. n = recvfrom(sockfd, (char *)buffer, BUFFER_SIZE,
MSG_WAITALL, (struct sockaddr *)&client_addr, &len);
43. buffer[n] = '\0';
44.

45. printf("Client: %s\n", buffer);


46.

47. // Sending acknowledgment to client


48. sendto(sockfd, "Message received",
strlen("Message received"), MSG_CONFIRM, (const
struct sockaddr *)&client_addr, len);
49. }
50.

51. close(sockfd);
52. return 0;
53. }

UDP Client (client.c):


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

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);

// Sending message to server


printf("Enter a message to send to server: ");
fgets(buffer, BUFFER_SIZE, stdin);

sendto(sockfd, (const char *)buffer, strlen(buffer),


MSG_CONFIRM, (const struct sockaddr *)&server_addr,
sizeof(server_addr));

// Receiving acknowledgment from server


n = recvfrom(sockfd, (char *)buffer, BUFFER_SIZE,
MSG_WAITALL, NULL, NULL);
buffer[n] = '\0';

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

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>

#define PORT 8080


#define BUFFER_SIZE 1024

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);

// Binding socket to server address


if (bind(sockfd, (const struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
perror("Error in binding");
exit(1);
}

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

while (1) {
socklen_t len = sizeof(client_addr);

// Receiving message from client


n = recvfrom(sockfd, (char *)buffer, BUFFER_SIZE,
MSG_WAITALL, (struct sockaddr *)&client_addr, &len);
buffer[n] = '\0';

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

// Sending acknowledgment to client


sendto(sockfd, "Message received", strlen("Message
received"), MSG_CONFIRM, (const struct sockaddr
*)&client_addr, len);
}

close(sockfd);
return 0;
}
UDP Client (client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080


#define SERVER_IP "127.0.0.1"

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);

// Sending message to server


printf("Enter a message to send to server: ");
fgets(buffer, BUFFER_SIZE, stdin);

sendto(sockfd, (const char *)buffer, strlen(buffer),


MSG_CONFIRM, (const struct sockaddr
*)&server_addr, sizeof(server_addr));

// Receiving acknowledgment from server


n = recvfrom(sockfd, (char *)buffer, BUFFER_SIZE,
MSG_WAITALL, NULL, NULL);
buffer[n] = '\0';

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

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>

#define PORT 12345


#define BUFFER_SIZE 1024

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);
}

// Server address configuration


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

// Bind the socket to the server address


if (bind(sockfd, (const struct
sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(sockfd);
exit(EXIT_FAILURE);
}

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

12
while (1) {
unsigned int len = sizeof(client_addr);

// Receive data from the client


ssize_t bytes_received = recvfrom(sockfd,
buffer, BUFFER_SIZE, 0,
(struct
sockaddr*)&client_addr, &len);

if (bytes_received < 0) {
perror("Error in recvfrom");
continue;
}

// Null-terminate the received data to treat it


as a string
buffer[bytes_received] = '\0';

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

13
// Find the length of the received string
size_t str_len = strlen(buffer);

// Send the length back to the client


ssize_t bytes_sent = sendto(sockfd, &str_len,
sizeof(size_t), 0,
(const struct
sockaddr*)&client_addr, len);

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>

#define PORT 8888


#define MAX_BUFFER_SIZE 1024

int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[MAX_BUFFER_SIZE];

// Create UDP socket


if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))
< 0) {

15
perror("socket creation failed");
exit(EXIT_FAILURE);
}

// Set up the server address


memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

// Bind the socket to the specified address and


port
if (bind(sockfd, (const struct
sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind 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);

buffer[received_len] = '\0'; // Null-terminate


the received string

// Calculate the length of the received string


int length = strlen(buffer);

// Send the length back to the client


sendto(sockfd, (const char*)&length,
sizeof(length), 0,

17
(const struct sockaddr*)&client_addr,
sizeof(client_addr));

// Wait for acknowledgment from the client


char ack[MAX_BUFFER_SIZE];
recvfrom(sockfd, (char *)ack,
MAX_BUFFER_SIZE,
MSG_WAITALL, (struct
sockaddr*)&client_addr, &addr_len);

printf("Received acknowledgment from the


client: %s\n", ack);
}

return 0;
}
UDP Client (client.c):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
18
#include <unistd.h>
#include <arpa/inet.h>

#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);
}

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);

// Sending message to server


printf("Enter a message to send to server: ");
fgets(buffer, BUFFER_SIZE, stdin);

sendto(sockfd, (const char *)buffer,


strlen(buffer), MSG_CONFIRM, (const struct
sockaddr *)&server_addr, sizeof(server_addr));

// Receiving acknowledgment from server


n = recvfrom(sockfd, (char *)buffer,
BUFFER_SIZE, MSG_WAITALL, NULL, NULL);
buffer[n] = '\0';
20
printf("Server acknowledgment: %s\n", buffer);

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)

To install a network monitor application on Unix/Linux, we'll use


Wireshark as an example. Wireshark is a popular network
protocol analyzer that allows you to capture and analyze
network traffic. Here are the steps to install Wireshark and
demonstrate the use of capture and display filters:

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.

To apply the group changes, you will need


to log out and log back in or restart your
computer.

Step 2: Capture Filters:


Capture filters allow you to selectively capture
specific network traffic based on certain criteria.
They help in reducing the amount of captured
data, focusing only on relevant packets.

Example 1: Capture all HTTP (port 80) traffic:


sudo tcpdump -i eth0 port 80 -w http_traffic.pcap
In this example, we are capturing all the HTTP traffic
(TCP port 80) on the "eth0" interface and saving it to a
file called "http_traffic.pcap."

22
Step 3: Display Filters
Display filters allow you to filter and display
specific packets from the captured data based on
specific criteria.

Example 2: Display only HTTP (port 80) traffic


in Wireshark:
-Start Wireshark by running the command:
Wireshark

-Open the "http_traffic.pcap" file

-In the "Filter" box at the top, enter the filter:


tcp.port == 80

Example 3: Display HTTP GET requests:


-With Wireshark still open and the same
"http_traffic.pcap" file loaded:

-In the "Filter" box, enter the filter:


http.request.method == "GET"

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

2.sort- (sort lines of text files):


For example, if you have a file named "numbers.txt" with
the following content:
4
2
1
3
Running sort numbers.txt would output:
1
2
3
4

3.ping- (send ICMP echo requests to a network host):

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

4.more- (display text one screen at a time):


For example, if you have a file named "long_text.txt" with
a large amount of content, running more long_text.txt
would display the text one screen at a time, allowing you
to scroll through it using the Enter key.
This is line 1 of the long text.
This is line 2 of the long text.
...
-- More --(10%)

25

You might also like