From 305a058d0cc3e3c67773d87bb982a4005faebd87 Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Sat, 11 Feb 2023 14:57:30 +0100 Subject: [PATCH] Urgent sockets --- processes/sockets/c-inet-tcp-urgent.c | 59 +++++++++++ processes/sockets/c-inet-udp-urgent.c | 60 ++++++++++++ processes/sockets/c-inet-udp.c | 60 ++++++++++++ processes/sockets/curg.c | 54 +++++++++++ processes/sockets/s-inet-tcp-urgent.c | 112 +++++++++++++++++++++ processes/sockets/s-inet-udp.c | 68 +++++++++++++ processes/sockets/surg.c | 135 ++++++++++++++++++++++++++ 7 files changed, 548 insertions(+) create mode 100644 processes/sockets/c-inet-tcp-urgent.c create mode 100644 processes/sockets/c-inet-udp-urgent.c create mode 100644 processes/sockets/c-inet-udp.c create mode 100644 processes/sockets/curg.c create mode 100644 processes/sockets/s-inet-tcp-urgent.c create mode 100644 processes/sockets/s-inet-udp.c create mode 100644 processes/sockets/surg.c diff --git a/processes/sockets/c-inet-tcp-urgent.c b/processes/sockets/c-inet-tcp-urgent.c new file mode 100644 index 0000000..32699e4 --- /dev/null +++ b/processes/sockets/c-inet-tcp-urgent.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr_in SockAddr_in; +typedef struct sockaddr SockAddr; + +void handle_error(char *txt) +{ + perror(txt); + exit(-1); +} + +int main() +{ + int socket_fd; + if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + handle_error("socket"); + + SockAddr_in saddr; + socklen_t addr_size = sizeof(SockAddr_in); + memset(&saddr, 0, addr_size); + saddr.sin_port = htons(4040); + saddr.sin_family = AF_INET; + inet_pton(AF_INET, "127.0.0.1", &(saddr.sin_addr.s_addr)); + + // memset(&caddr, 0, addr_size); + // caddr.sin_port = htons(4041); + // caddr.sin_family = AF_INET; + // caddr.sin_addr.s_addr = INADDR_ANY; + // inet_pton(AF_INET, "127.0.0.1", &(caddr.sin_addr.s_addr)); + + // if (bind(socket_fd, (SockAddr *)&caddr, addr_size) == -1) + // handle_error("bind"); + + if (connect(socket_fd, (SockAddr *)&saddr, addr_size) == -1) + handle_error("connect"); + + printf("connected\n"); + + double dd = 21.21; + if (write(socket_fd, &dd, sizeof(double)) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + usleep(100); + dd = 111.1; + if (send(socket_fd, &dd, sizeof(double), MSG_OOB) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + if (close(socket_fd) == -1) + handle_error("close"); + + return 0; +} \ No newline at end of file diff --git a/processes/sockets/c-inet-udp-urgent.c b/processes/sockets/c-inet-udp-urgent.c new file mode 100644 index 0000000..e897884 --- /dev/null +++ b/processes/sockets/c-inet-udp-urgent.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr_in SockAddr_in; +typedef struct sockaddr SockAddr; + +void handle_error(char *txt) +{ + perror(txt); + exit(-1); +} + +int main() +{ + int socket_fd; + if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) + handle_error("socket"); + + SockAddr_in caddr; + SockAddr_in saddr; + socklen_t addr_size = sizeof(SockAddr_in); + memset(&saddr, 0, addr_size); + saddr.sin_port = htons(4040); + saddr.sin_family = PF_INET; + inet_pton(PF_INET, "127.0.0.1", &(saddr.sin_addr.s_addr)); + + memset(&caddr, 0, addr_size); + caddr.sin_port = htons(4041); + caddr.sin_family = PF_INET; + caddr.sin_addr.s_addr = INADDR_ANY; + inet_pton(PF_INET, "127.0.0.1", &(caddr.sin_addr.s_addr)); + + if (bind(socket_fd, (SockAddr *)&caddr, addr_size) == -1) + handle_error("bind"); + + double dd = 21.21; + if (sendto(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + dd = 111.1; + if (sendto(socket_fd, &dd, sizeof(double), MSG_OOB, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + dd = 321.1; + if (sendto(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + if (close(socket_fd) == -1) + handle_error("close"); + + return 0; +} \ No newline at end of file diff --git a/processes/sockets/c-inet-udp.c b/processes/sockets/c-inet-udp.c new file mode 100644 index 0000000..76f838b --- /dev/null +++ b/processes/sockets/c-inet-udp.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr_in SockAddr_in; +typedef struct sockaddr SockAddr; + +void handle_error(char *txt) +{ + perror(txt); + exit(-1); +} + +int main() +{ + int socket_fd; + if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) + handle_error("socket"); + + SockAddr_in caddr; + SockAddr_in saddr; + socklen_t addr_size = sizeof(SockAddr_in); + memset(&saddr, 0, addr_size); + saddr.sin_port = htons(4040); + saddr.sin_family = PF_INET; + inet_pton(PF_INET, "127.0.0.1", &(saddr.sin_addr.s_addr)); + + memset(&caddr, 0, addr_size); + caddr.sin_port = htons(4041); + caddr.sin_family = PF_INET; + caddr.sin_addr.s_addr = INADDR_ANY; + inet_pton(PF_INET, "127.0.0.1", &(caddr.sin_addr.s_addr)); + + if (bind(socket_fd, (SockAddr *)&caddr, addr_size) == -1) + handle_error("bind"); + + double dd = 21.21; + if (sendto(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + dd = 111.1; + if (sendto(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + dd = 321.1; + if (sendto(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&saddr, addr_size) == -1) + handle_error("sendto"); + printf("sent: %f\n", dd); + + if (close(socket_fd) == -1) + handle_error("close"); + + return 0; +} \ No newline at end of file diff --git a/processes/sockets/curg.c b/processes/sockets/curg.c new file mode 100644 index 0000000..d0be9ac --- /dev/null +++ b/processes/sockets/curg.c @@ -0,0 +1,54 @@ +/* +Klient strumieniowy, internetowy z danymi wysoko priorytetowymi. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr SockAddr; +typedef struct sockaddr_in SockAddr_in; + +int main(int argc, char *argv[]) +{ + int gniazdo; + socklen_t i; + SockAddr_in adres; + + if ((gniazdo = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + perror("Blad wywolania funkcji socket.\n"); + return -1; + } + struct hostent *Adres; + + i = sizeof(SockAddr_in); + adres.sin_family = AF_INET; + adres.sin_port = htons(4040); + // Adres = gethostbyname(argv[1]); + inet_pton(AF_INET, "127.0.0.1", &(adres.sin_addr.s_addr)); + // adres.sin_addr.s_addr = *(long *)(Adres->h_addr); + if (connect(gniazdo, (SockAddr *)&adres, i) < 0) + { + perror("Blad funkcji connect.\n"); + close(gniazdo); + return -1; + } + + double dd = 21.21; + + usleep(1); + write(gniazdo, &dd, sizeof(double)); + + usleep(100); + send(gniazdo, &dd, sizeof(double), MSG_OOB); + + close(gniazdo); + return 0; +} \ No newline at end of file diff --git a/processes/sockets/s-inet-tcp-urgent.c b/processes/sockets/s-inet-tcp-urgent.c new file mode 100644 index 0000000..b212424 --- /dev/null +++ b/processes/sockets/s-inet-tcp-urgent.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr_in SockAddr_in; +typedef struct sockaddr SockAddr; + +SockAddr_in addr; +SockAddr_in addr_client; +socklen_t addr_size; +int socket_fd; +int client_socket; + +void handle_error(char *txt) +{ + perror(txt); + exit(-1); +} + +void receive_urgent(int signo) +{ + double dd; + printf("OOB received\n"); + if (recv(client_socket, &dd, sizeof(double), MSG_OOB) == -1) + handle_error("recv urgent"); + printf("urgent dd: %f\n", dd); + + signal(SIGURG, receive_urgent); +} + +void end_connection() +{ + if (close(socket_fd) == -1) + handle_error("close"); + exit(0); +} + +int main() +{ + signal(SIGINT, end_connection); + signal(SIGTERM, end_connection); + + if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + handle_error("socket"); + printf("socket success\n"); + + addr_size = sizeof(SockAddr_in); + memset(&addr, 0, addr_size); + addr.sin_port = htons(4040); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(socket_fd, (SockAddr *)&addr, addr_size) == -1) + handle_error("bind"); + printf("bind success\n"); + + if (listen(socket_fd, 10) == -1) + handle_error("listen"); + printf("listen success\n"); + + double dd; + for (;;) + { + if ((client_socket = accept(socket_fd, (SockAddr *)&addr_client, &addr_size)) == -1) + handle_error("accept"); + printf("accept success\n"); + + int i; + if ((i = fork()) == -1) + handle_error("fork"); + printf("fork success\n"); + if (i == 0) + { + close(socket_fd); + signal(SIGURG, receive_urgent); + fcntl(client_socket, F_SETOWN, getpid()); + + for (;;) + { + int bytes = read(client_socket, &dd, sizeof(double)); + if (bytes == -1) + handle_error("recvfrom"); + if (bytes == 0) + { + printf("EOF\n"); + break; + } + else + { + printf("dd: %f\n", dd); + } + } + + close(client_socket); + + return 0; + } + else + { + sigignore(SIGCHLD); + close(client_socket); + } + } + + return 0; +} \ No newline at end of file diff --git a/processes/sockets/s-inet-udp.c b/processes/sockets/s-inet-udp.c new file mode 100644 index 0000000..310542d --- /dev/null +++ b/processes/sockets/s-inet-udp.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct sockaddr_in SockAddr_in; +typedef struct sockaddr SockAddr; + +SockAddr_in addr; +socklen_t addr_size; +int socket_fd; + +void handle_error(char *txt) +{ + perror(txt); + exit(-1); +} + +void receive_urgent() +{ + double dd; + if (recvfrom(socket_fd, &dd, sizeof(double), MSG_OOB, (SockAddr *)&addr, &addr_size) == -1) + handle_error("recvfrom urgent"); + printf("dd: %f\n", dd); + + signal(SIGURG, receive_urgent); +} + +int main() +{ + if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) + handle_error("socket"); + + addr_size = sizeof(SockAddr_in); + memset(&addr, 0, addr_size); + addr.sin_port = htons(4040); + addr.sin_family = PF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + // inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr.s_addr)); + + if (bind(socket_fd, (SockAddr *)&addr, addr_size) == -1) + handle_error("bind"); + + double dd; + for (;;) + { + fcntl(socket_fd, F_SETOWN, getpid()); + signal(SIGURG, receive_urgent); + int bytes = recvfrom(socket_fd, &dd, sizeof(double), 0, (SockAddr *)&addr, &addr_size); + if (bytes == -1) + handle_error("recvfrom"); + if (bytes == 0) + { + printf("EOF\n"); + } + printf("dd: %f\n", dd); + } + + if (close(socket_fd) == -1) + handle_error("close"); + + return 0; +} \ No newline at end of file diff --git a/processes/sockets/surg.c b/processes/sockets/surg.c new file mode 100644 index 0000000..b9eaf9a --- /dev/null +++ b/processes/sockets/surg.c @@ -0,0 +1,135 @@ +/* +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); + } + } + } + } + } + } + } +} \ No newline at end of file