MCSL-223-EM-2022-23
MCSL-223-EM-2022-23
DISCLAIMER
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
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:
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.
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;
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);
}
// 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());
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