0% found this document useful (0 votes)
5 views16 pages

MCSL-223-EM-2022-23

The document outlines a lab assignment for the course MCSL-223, focusing on Computer Networks and Data Mining, with a maximum score of 100 marks. It includes tasks such as creating UDP client-server applications, establishing a network topology with TCP connections, and performing data classification using WEKA. The assignment emphasizes personal research and understanding rather than copying solutions directly.

Uploaded by

xapef74624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

MCSL-223-EM-2022-23

The document outlines a lab assignment for the course MCSL-223, focusing on Computer Networks and Data Mining, with a maximum score of 100 marks. It includes tasks such as creating UDP client-server applications, establishing a network topology with TCP connections, and performing data classification using WEKA. The assignment emphasizes personal research and understanding rather than copying solutions directly.

Uploaded by

xapef74624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Course Code: MCSL-223

Course Title: Computer Networks and Data Mining


Lab Assignment Number: MCA_NEW(II)/223/Assign/2022-23
Maximum Marks: 100
Weightage: 30%
Last Dates for Submission: 31st October, 2022 (for July session)
15th April, 2023 (for January session)

DISCLAIMER

Do not copy these assignments exactly, do your


personal research for better solutions, and also read
all answers properly. Remember this assignment is
only for your help.
Section 1: Computer Networks
Q1: (10 Marks)
Create a UDP client on a node n1 and a UDP server on a node n2. Perform the following tasks
for these node n1 and n2:
a) Send packets to node n2 from node n1 and plot the number of bytes received with respect to
time at node n2.
b) Show the pcap traces at node n2’s WiFi
interface. c) Show the pcap traces at node n1’s WiFi
interface.

udp−client.c
1. /* CS480, Spring 2016
2. *
3. * Simple UDP client, to demonstrate use of the sockets API
4. * Compile with:
5. * gcc −Wall −o udp−client udp−client.c
6. * or
7. * gcc −g −Wall −o udp−client udp−client.c # to support debugging
8. */
9.
10. #include <stdio.h>
11. #include <stdlib.h>
12. #include <sys/socket.h>
13. #include <netinet/in.h>
14. #include <string.h>
15. #include <assert.h>
16. #include <arpa/inet.h>
17. #include <netdb.h>
18. #include <limits.h>
19.
20. void handle_error(const char* s)
21. {
22. perror(s);
23. exit(1);
24. }
25.
26. int main(int argc, char** argv)
27. {
28. int sock_fd;
29. struct sockaddr_in addr;
30. struct hostent* hostentp;
31. char* endptr;
32. unsigned int portl;
33. unsigned short port;
34. size_t num_to_send;
35. size_t num_sent;
36.
37. if (argc != 4) {
38. fprintf(stderr, "usage: %s <hostname> <port> <message>\n", argv[0]);
39. exit(1);
40. }
41.
42. /* convert from string to int */
43. portl = strtol(argv[2], &endptr, 10);
44. if (endptr == NULL || portl == 0)
45. handle_error("strtol");
46.
47. assert(portl < USHRT_MAX);
48. port = (unsigned short)portl;
49.
50. /*
51. * Below, we use the C idiom for "assign to a variable
52. * and then check its value"
53. */
54. if (!(hostentp = gethostbyname(argv[1]))) {
55. herror("gethostbyname");
56. exit(1);
57. }
58.
59. if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
60. handle_error("socket");
61.
62. memset(&addr, 0, sizeof(addr));
63. addr.sin_family = AF_INET;
64. memcpy(&addr.sin_addr.s_addr,
65. hostentp−>h_addr_list[0],
66. sizeof(struct in_addr));
67. addr.sin_port = htons(port);
68.
69. printf("I am about to send %s to IP address %s and port %d\n",
70. argv[3], inet_ntoa(addr.sin_addr), atoi(argv[2]));
71.
72. num_to_send = strlen(argv[3]);
73.
74. num_sent = sendto(sock_fd, /* socket */
75. argv[3], /* buffer to send */
76. num_to_send, /* number of bytes to send */
77. 0, /* flags=0: bare−bones use case*/
78. (const struct sockaddr*)&addr, /* the destination */
79. sizeof(addr)); /* size of the destination struct */
80. if (num_sent != num_to_send) {
81. assert(num_sent < 0);
82. handle_error("sendto");
83. }
84.
85. printf("I just sent the bytes!\n");
86.
87. exit(0);
88. }
udp−server.c

1. /* CS480, Spring 2016


2. *
3. * Simple UDP server, to demonstrate use of the sockets API
4. * Compile with:
5. * gcc −Wall −o udp−server udp−server.c
6. * or
7. * gcc −g −Wall −o udp−server udp−server.c # for debugging support
8. */
9.
10. #include <stdio.h>
11. #include <stdlib.h>
12. #include <sys/types.h>
13. #include <sys/socket.h>
14. #include <netinet/in.h>
15. #include <string.h>
16. #include <assert.h>
17. #include <arpa/inet.h>
18. #include <netdb.h>
19. #include <limits.h>

20. void handle_error(const char* s)


21. {
22. perror(s);
23. exit(1);
24. }
25. int main(int argc, char** argv)
26. {
27. int sock_fd;
28. struct sockaddr_in my_addr, my_peer_addr;
29. char* endptr;
30. unsigned int portl;
31. unsigned short port;
32. int num_received;
33. char msg[1024];
34. socklen_t addrlen;
35.
36. if (argc != 2) {
37. fprintf(stderr, "usage: %s <port>\n", argv[0]);
38. exit(1);
39. }
40.
41. /* convert from string to int */
42. portl = strtol(argv[1], &endptr, 10);
43. if (endptr == NULL || portl == 0)
44. handle_error("strtol");
45.
46. assert(portl < USHRT_MAX);
47. port = (unsigned short)portl;
48.
49. if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
50. handle_error("socket");
51.
52. memset(&my_addr, 0, sizeof(my_addr));
53. my_addr.sin_family = AF_INET;
54. my_addr.sin_addr.s_addr = INADDR_ANY;
55. my_addr.sin_port = htons(port);
56.
57. if (bind(sock_fd,
58. (struct sockaddr*)&my_addr,
59. sizeof(struct sockaddr_in)) < 0)
60. handle_error("bind");
61.
62. while (1) {
63.
64. addrlen = sizeof(struct sockaddr_in);
65.
66. if ((num_received = recvfrom(sock_fd, /* socket */
67. msg, /* buffer */
68. sizeof(msg), /* size of buffer */
69. 0, /* flags = 0 */
70. /* who’s sending */
71. (struct sockaddr*)&my_peer_addr,
72. /* length of buffer to receive peer info */
73. &addrlen)) < 0)
74.
75. handle_error("recvfrom");
76.
77. assert(addrlen == sizeof(struct sockaddr_in));
78.
79. printf("I got message %.*s from host %s, src port %d\n",
80. num_received, msg,
81. inet_ntoa(my_peer_addr.sin_addr),
82. ntohs(my_peer_addr.sin_port));
83. }
84. exit(0);
85. }
Q2: (10 Marks)
Create a simple network topology having two client nodes on left side and two server nodes on
the right side. Connect both the client nodes with another client node n1. Similarly, connect
both the server nodes to a client node n2. Also connect nodes n1 and n2, thus forming a
dumbbell shape topology. Use point to point link only. Perform the following tasks using this
topology: a) Install a TCP socket instance connecting either of the client node with either of the
server node. b) Install TCP socket instance connecting remaining client nodes with the
remaining server nodes.
c) Send packets to respective clients from both the servers and monitor the traffic for both the
pair and plot the number of bytes received.
d) Also plot the packets received.

Server-Side Code
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <unistd.h>
5. #include <arpa/inet.h>

6. int main(){
7.
8. char *ip = "127.0.0.1";
9. int port =s 5566;
10.
11. int server_sock, client_sock;
12. struct sockaddr_in server_addr, client_addr;
13. socklen_t addr_size;
14. char buffer[1024];
15. int n;
16.
17. server_sock = socket(AF_INET, SOCK_STREAM, 0);
18. if (server_sock < 0){
19. perror("[-]Socket error");
20. exit(1);
21. }
22. printf("[+]TCP server socket created.\n");
23.
24. memset(&server_addr, '\0', sizeof(server_addr));
25. server_addr.sin_family = AF_INET;
26. server_addr.sin_port = port;
27. server_addr.sin_addr.s_addr = inet_addr(ip);
28. n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
29. if (n < 0){
30. perror("[-]Bind error");
31. exit(1);
32. }
33. printf("[+]Bind to the port number: %d\n", port);
34.
35. listen(server_sock, 5);
36. printf("Listening...\n");
37.
38. while(1){
39. addr_size = sizeof(client_addr);
40. client_sock = accept(server_sock, (struct sockaddr*)&client_addr,
&addr_size);
41. printf("[+]Client connected.\n");
42.
43. bzero(buffer, 1024);
44. recv(client_sock, buffer, sizeof(buffer), 0);
45. printf("Client: %s\n", buffer);
46.
47. bzero(buffer, 1024);
48. strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");
49. printf("Server: %s\n", buffer);
50. send(client_sock, buffer, strlen(buffer), 0);
51.
52. close(client_sock);
53. printf("[+]Client disconnected.\n\n");
54.
55. }
56. return 0;
57. }

Client-Side Code
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <unistd.h>
5. #include <arpa/inet.h>
6. int main(){
7.
8. char *ip = "127.0.0.1";
9. int port = 5566;
10.
11. int sock;
12. struct sockaddr_in addr;
13. socklen_t addr_size;
14. char buffer[1024];
15. int n;
16.
17. sock = socket(AF_INET, SOCK_STREAM, 0);
18. if (sock < 0){
19. perror("[-]Socket error");
20. exit(1);
21. }
22. printf("[+]TCP server socket created.\n");
23.
24. memset(&addr, '\0', sizeof(addr));
25. addr.sin_family = AF_INET;
26. addr.sin_port = port;
27. addr.sin_addr.s_addr = inet_addr(ip);
28.
29. connect(sock, (struct sockaddr*)&addr, sizeof(addr));
30. printf("Connected to the server.\n");
31.
32. bzero(buffer, 1024);
33. strcpy(buffer, "HELLO, THIS IS CLIENT.");
34. printf("Client: %s\n", buffer);
35. send(sock, buffer, strlen(buffer), 0);
36.
37. bzero(buffer, 1024);
38. recv(sock, buffer, sizeof(buffer), 0);
39. printf("Server: %s\n", buffer);
40.
41. close(sock);
42. printf("Disconnected from the server.\n");
43.
44. return 0;
45. }
Section 2: Data Mining Lab
Q1.
Perform the following:

a. Demonstrate data classification on dataset Customer.csv which includes creation of


a csv file, reading it into WEKA and using the WEKA explorer.

We can also load our CSV files directly in the Weka Explorer interface. This is handy if we are
in a hurry and want to quickly test out an idea. We can use the iris dataset again, to practice if
we do not have a CSV dataset to load.
1. Start the Weka GUI Chooser.
2. Launch the Weka Explorer by clicking the Explorer button.
3. Click the Open file... button.
4. Navigate to your current working directory. Change the Files of Type to CSV data files
(*.csv). Select your file and click the Open button.

We can work with the data directly. We can also save our dataset in ARFF format by clicking
the Save button and typing a filename.
b. Use WEKA to implement Decision Tree (J48), Naive Bayes and Random Forest
using individual datasets.

Weka Visualization of a Decision Tree


Decision trees can support classification and regression problems. Decision trees are more
recently referred to as Classification And Regression Trees (CART).
After the tree is constructed, it is pruned in order to improve the model’s ability to generalize to
new data.
Choose the decision tree algorithm:
1. Click the “Choose” button and select “REPTree” under the “trees” group.
2. Click on the name of the algorithm to review the algorithm configuration.
WEKA Configuration for the Naive Bayes Algorithm
This is an unrealistic assumption because we expect the variables to interact and be dependent,
although this assumption makes the probabilities fast and easy to calculate. Even under this
unrealistic assumption, Naive Bayes has been shown to be a very effective classification
algorithm. Naive Bayes calculates the posterior probability for each class and makes a
prediction for the class with the highest probability. As such, it supports both binary
classification and multiclass classification problems.
Choose the Naive Bayes algorithm:
1. Click the “Choose” button and select “NaiveBayes” under the “bayes” group.
2. Click on the name of the algorithm to review the algorithm configuration.
Weka Configuration for the Random Forest Algorithm
Random Forest is an improvement upon bagged decision trees that disrupts the greedy splitting
algorithm during tree creation so that split points can only be selected from a random subset of
the input attributes. This simple change can have a big effect decreasing the similarity between
the bagged trees and in turn the resulting predictions. Choose the random forest algorithm:
1. Click the Choose button and select RandomForest under the trees group.
2. Click on the name of the algorithm to review the algorithm configuration.
c. Illustrate and implement a java program using Naive Bayes Classification.

package com.namsor.oss.samples;

import com.namsor.oss.classify.bayes.ClassifyException;
import com.namsor.oss.classify.bayes.NaiveBayesClassifierMapImpl;
import com.namsor.oss.classify.bayes.PersistentClassifierException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.namsor.oss.classify.bayes.IClassification;
import com.namsor.oss.classify.bayes.IClassificationExplained;
import com.namsor.oss.classify.bayes.NaiveBayesExplainerImpl;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class MainSample1 {

public static final String YES = "Yes";


public static final String NO = "No";

public static final String[] colName = {


"outlook", "temp", "humidity", "wind", "play"
};

public static final String[][] data = {


{"Sunny", "Hot", "High", "Weak", "No"},
{"Sunny", "Hot", "High", "Strong", "No"},
{"Overcast", "Hot", "High", "Weak", "Yes"},
{"Rain", "Mild", "High", "Weak", "Yes"},
{"Rain", "Cool", "Normal", "Weak", "Yes"},
{"Rain", "Cool", "Normal", "Strong", "No"},
{"Overcast", "Cool", "Normal", "Strong", "Yes"},
{"Sunny", "Mild", "High", "Weak", "No"},
{"Sunny", "Cool", "Normal", "Weak", "Yes"},
{"Rain", "Mild", "Normal", "Weak", "Yes"},
{"Sunny", "Mild", "Normal", "Strong", "Yes"},
{"Overcast", "Mild", "High", "Strong", "Yes"},
{"Overcast", "Hot", "Normal", "Weak", "Yes"},
{"Rain", "Mild", "High", "Strong", "No"},};

public static final void main(String[] args) {

try {
String[] cats = {YES, NO};
// Create a new bayes classifier with string categories and string features.
NaiveBayesClassifierMapImpl bayes = new NaiveBayesClassifierMapImpl("tennis", cats);
// Examples to learn from.
for (int i = 0; i < data.length; i++) {
Map<String, String> features = new HashMap();
for (int j = 0; j < colName.length - 1; j++) {
features.put(colName[j], data[i][j]);
}
// learn ex. Category=Yes Conditions=Sunny, Cool, Normal and Weak.
bayes.learn(data[i][colName.length - 1], features);
}

Map<String, String> features = new HashMap();


features.put("outlook", "Sunny");
features.put("temp", "Cool");
features.put("humidity", "High");
features.put("wind", "Strong");

// Shall we play given weather conditions Sunny, Cool, Rainy and Windy
? IClassification predict = bayes.classify(features, true);
for (int i = 0; i < predict.getClassProbabilities().length; i++) {
System.out.println("P(" + predict.getClassProbabilities()[i].getCategory() + ")=" +
predict.getClassProbabilities()[i].getProbability());
}
if (predict.getExplanationData() != null) {
NaiveBayesExplainerImpl explainer = new NaiveBayesExplainerImpl();
IClassificationExplained explained = explainer.explain(predict);
System.out.println(explained.toString());

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();


ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
// JavaScript code from String
Double proba = (Double) scriptEngine.eval(explained.toString());
System.out.println("Result of evaluating mathematical expressions in String = " + proba);
}
} catch (PersistentClassifierException ex) {
Logger.getLogger(MainSample1.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassifyException ex) {
Logger.getLogger(MainSample1.class.getName()).log(Level.SEVERE, null, ex);
} catch (Throwable ex) {
Logger.getLogger(MainSample1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
d. Use WEKA to implement the following Clustering Algorithms: k-Means,
Agglomerative and Divisive.

Implementing K-Means using WEKA


The steps for implementation using WEKA are as follows:
1) Open WEKA Explorer and click on Open File in the Preprocess tab. Choose dataset
“vote.arff”.
2) Go to the “Cluster” tab and click on the “Choose” button. Select the clustering method as
“SimpleKMeans”.
3) Choose Settings and then set the following fields:
 Distance function as Euclidian
 The number of clusters as 6. With more number of clusters, the sum of squared error will
reduce.
Click on Ok and start the algorithm.
4) Click on Start in the left panel. The algorithm display results on the white screen.

Implementing Hierarchical clustering using WEKA

Step 1: Open the Weka explorer in the preprocessing interface and import the appropriate
dataset.
Step 2: To perform clustering, go to the explorer’s ‘cluster’ tab and select the select button. As a re
Step 3: Then press the text button to the right of the pick icon to bring up the popup window seen in
Step 4: One of the options has been selected. Before we execute the clustering method, we need to m
Step 5: The resulting window displays the centroid of each cluster, as well as data on the number an
Step 6: Visualizing the qualities of each cluster is another approach to grasp them. Right-click the re

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy