-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock_client.c
More file actions
107 lines (90 loc) · 3.25 KB
/
sock_client.c
File metadata and controls
107 lines (90 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static void usage(const char *progname) {
fprintf(stderr, "Usage: %s [-p port] server_ip\n", progname);
exit(1);
}
int main(int argc, char *argv[]) {
int sock, port = 9999;
struct sockaddr_in serv_addr;
char *host;
int choice, n, *x, *y, i;
float r;
char ch;
while ((ch = getopt(argc, argv, "p:")) != -1) {
switch (ch) {
case 'p': port = atoi(optarg); break;
default: usage(argv[0]);
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage(argv[0]);
host = argv[0];
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err(1, "socket");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0)
err(1, "inet_pton");
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
err(1, "connect");
while (1) {
printf("\033[38;2;255;0;80m<Option Catalog>\033[0m\n1: Dot product\n2: Averages\n3: r*X\n4: Exit\nChoice: ");
scanf("%d", &choice);
send(sock, &choice, sizeof(int), 0);
if (choice == 4)
break;
printf("\033[1;35mEnter n: \033[0m");
scanf("%d", &n);
x = malloc(n * sizeof(int));
if (choice == 3) {
printf("\033[1;35mEnter r: \033[0m");
scanf("%f", &r);
send(sock, &r, sizeof(float), 0);
}
printf("\033[1;35mEnter vector X:\033[0m\n");
for (i = 0; i < n; i++) scanf("%d", &x[i]);
if (choice == 1 || choice == 2) {
y = malloc(n * sizeof(int));
printf("\033[1;35mEnter vector Y:\033[0m\n");
for (i = 0; i < n; i++) scanf("%d", &y[i]);
}
send(sock, &n, sizeof(int), 0);
send(sock, x, n * sizeof(int), 0);
if (choice == 1 || choice == 2)
send(sock, y, n * sizeof(int), 0);
if (choice == 1) {
int result;
recv(sock, &result, sizeof(int), 0);
printf("\033[1;32m \n--------------------\nDot product: %d \n-------------------- \033[0m\n\n", result);
} else if (choice == 2) {
float results[2];
recv(sock, results, 2 * sizeof(float), 0);
printf("\033[1;32m\n-------------------------------------------");
printf("\nAverage X: %.3f, Average Y: %.3f \n", results[0], results[1]);
printf("-------------------------------------------\033[0m\n\n");
} else if (choice == 3) {
float *res = malloc(n * sizeof(float));
recv(sock, res, n * sizeof(float), 0);
printf("\033[1;32m \n---------------------------------------------------------------\nResult: [");
for (i = 0; i < n; i++)
printf("%.3f%s", res[i], i == n - 1 ? "" : ", ");
printf("] \n---------------------------------------------------------------\033[0m\n\n");
free(res);
}
free(x);
if (choice == 1 || choice == 2)
free(y);
}
close(sock);
return 0;
}