uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

ex3_client.c (2154B)


      1 #include <sys/socket.h>
      2 #include <sys/types.h>
      3 #include <sys/un.h>
      4 
      5 #include <err.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <unistd.h>
     10 
     11 /*
     12  * Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 3 (client) / 2020-2021
     13  * Ονοματεπώνυμο: Χρήστος Μαργιώλης
     14  * ΑΜ: 19390133
     15  * Τρόπος μεταγλώττισης: `cc ex3_client.c -o ex3_client`
     16  */
     17 
     18 /* Results from server. */
     19 struct pack_res {
     20 	char str[32];
     21 	float avg;
     22 };
     23 
     24 static char *argv0;
     25 
     26 static void *
     27 emalloc(size_t nb)
     28 {
     29 	void *p;
     30 
     31 	if ((p = malloc(nb)) == NULL)
     32 		err(1, "malloc");
     33 	return p;
     34 }
     35 
     36 int
     37 main(int argc, char *argv[])
     38 {
     39 	struct pack_res *res;
     40 	struct sockaddr_un sun;
     41 	char *sockfile = "/tmp/cool.sock";
     42 	int *arr;
     43 	int fd, i, n;
     44 	char ch;
     45 
     46 	argv0 = *argv;
     47 	while ((ch = getopt(argc, argv, "s:")) != -1) {
     48 		switch (ch) {
     49 		case 's':
     50 			sockfile = optarg;
     51 			break;
     52 		case '?':
     53 		default:
     54 			fprintf(stderr, "usage: %s [-s sockfile]\n", argv0);
     55 			break;
     56 		}
     57 	}
     58 	argc -= optind;
     59 	argv += optind;
     60 
     61 
     62 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
     63 		err(1, "socket");
     64 	(void)memset(&sun, 0, sizeof(sun));
     65 	sun.sun_family = AF_UNIX;
     66 	(void)strncpy(sun.sun_path, sockfile, sizeof(sun.sun_path) - 1);
     67 	if (connect(fd, (struct sockaddr *)&sun, sizeof(struct sockaddr_un)) < 0)
     68 		err(1, "connect");
     69 
     70 	res = emalloc(sizeof(struct pack_res));
     71 
     72 	for (;;) {
     73 
     74 		/* Remove any previous junk. */
     75 		(void)memset(res, 0, sizeof(struct pack_res));
     76 
     77 		printf("%s> n: ", argv0);
     78 		scanf("%d", &n);
     79 		/* Flush buffer */
     80 		(void)getchar();
     81 		arr = emalloc(n * sizeof(int));
     82 		for (i = 0; i < n; i++) {
     83 			printf("%s> arr[%d]: ", argv0, i);
     84 			scanf("%d", &arr[i]);
     85 		}
     86 		(void)getchar();
     87 		if (send(fd, &n, sizeof(int), 0) < 0)
     88 			err(1, "send");
     89 		if (send(fd, arr, n * sizeof(int), 0) < 0)
     90 			err(1, "send");
     91 		if (recv(fd, res, sizeof(struct pack_res), 0) < 0)
     92 			err(1, "recv");
     93 		printf("response: %s\tavg: %.2f\n", res->str, res->avg);
     94 
     95 		printf("%s> continue (y/n)? ", argv0);
     96 		ch = getchar();
     97 		if (send(fd, &ch, 1, 0) < 0)
     98 			err(1, "send");
     99 		if (ch == 'n')
    100 			break;
    101 	}
    102 
    103 	free(res);
    104 	(void)close(fd);
    105 
    106 	return 0;
    107 }