/* Serwer strumieniowy, internetowy z danymi wysoko priorytetowymi. */ #include #include #include #include #include #include #include #include typedef struct sockaddr SockAddr; typedef struct sockaddr_in SockAddr_in; int gniazdo, gniazdo1; void al(int i) { if (i == SIGTERM || i == SIGINT) { printf("Koniec pracy serwera.\n"); close(gniazdo); exit(0); } } void sig_urg(int signo) { size_t n; double dd; printf("Przechwycony sygnal SIGURG.\n"); n = recv(gniazdo1, &dd, sizeof(double), MSG_OOB); printf("Przecztalem %lu OOB bajtow: %f.\n", n, dd); } int main(int argc, char *argv[]) { socklen_t i; SockAddr_in adres, adres_k; signal(SIGTERM, al); signal(SIGINT, al); sigignore(SIGCHLD); if ((gniazdo = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("Blad wywolania funkcji socket.\n"); return -1; } else { i = sizeof(SockAddr_in); adres.sin_family = AF_INET; if (argc > 1) { adres.sin_port = htons(strtol(argv[1], NULL, 10)); } else { adres.sin_port = htons(0); } adres.sin_port = htons(4040); adres.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(gniazdo, (SockAddr *)&adres, i) < 0) { perror("Blad dowiazania gniazda.\n"); close(gniazdo); return -1; } else { if (getsockname(gniazdo, (SockAddr *)&adres, &i) != 0) { perror("Blad funkcji getsockname.\n"); close(gniazdo); return -1; } else { printf("Nr portu : %d.\n", ntohs(adres.sin_port)); if (listen(gniazdo, 5) < 0) { perror("Blad nasluchu gniazda.\n"); close(gniazdo); return -1; } else { for (;;) { if ((gniazdo1 = accept(gniazdo, (SockAddr *)&adres_k, &i)) < 0) { perror("Blad funkcji accept.\n"); continue; } else { if (fork() == 0) { pid_t t; size_t n; double dd; char buf[100]; close(gniazdo); signal(SIGURG, sig_urg); fcntl(gniazdo1, F_SETOWN, getpid()); printf("Polaczenie od %s z portu %d.\n", inet_ntoa(adres_k.sin_addr), ntohs(adres_k.sin_port)); for (;;) { if ((n = read(gniazdo1, &dd, sizeof(double))) == 0) { printf("Koniec transmisji klienta.\n"); close(gniazdo1); break; } // buf[n] = 0; printf("Przeczytalem %lu bajtow: %f.\n", n, dd); } printf("Koniec pracy procesu potomnego.\n"); return 0; } else { sigignore(SIGCHLD); close(gniazdo1); } } } } } } } }