/* 25Oct23: Network-client Beispiel + Aufgabe aus den Folien * * Keine Garantie auf Richtigkeit! Stand unverändert aus der * Tafelübung, am besten nichts kopieren sondern selbst schreiben! */ #include #include #include #include #include #include #include void process(FILE *rx, FILE *tx) { int mode = fgetc(rx), c; int (*conv)(int); switch (mode) { case 'l': conv = tolower; break; case 'u': conv = toupper; break; default: return; } while ((c = fgetc(rx)) != EOF) { /* FEHLER */ fputc(conv(c), tx); /* FEHLER */ fflush(tx); } } int main(int argc, char *argv[]) { struct addrinfo *res, *iter; int err, sock, copy; err = getaddrinfo(argv[1], argv[2], &(struct addrinfo) { .ai_socktype = SOCK_STREAM, .ai_family = AF_UNSPEC, .ai_flags = AI_ADDRCONFIG, }, &res); /* FEHLERBEHANDLUNG */ (void) err; for (iter = res; iter != NULL; iter = iter->ai_next) { sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol); if (0 == connect(sock, iter->ai_addr, iter->ai_addrlen)) { break; } close(sock); } if (iter == NULL) { /* FEHLERBEHANDLUNG mit gai_strerror */ return EXIT_FAILURE; } FILE *rx = fdopen(sock, "r"); copy = dup(sock); FILE *tx = fdopen(copy, "w"); process(rx, tx); fclose(rx); fclose(tx); return 0; }