Urgent sockets

This commit is contained in:
Dawid Pietrykowski 2023-02-11 14:57:30 +01:00
parent 5c3cc72f24
commit 305a058d0c
7 changed files with 548 additions and 0 deletions

View File

@ -0,0 +1,59 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
}

View File

@ -0,0 +1,60 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
}

View File

@ -0,0 +1,60 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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;
}

54
processes/sockets/curg.c Normal file
View File

@ -0,0 +1,54 @@
/*
Klient strumieniowy, internetowy z danymi wysoko priorytetowymi.
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <arpa/inet.h>
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;
}

View File

@ -0,0 +1,112 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
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;
}

View File

@ -0,0 +1,68 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
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;
}

135
processes/sockets/surg.c Normal file
View File

@ -0,0 +1,135 @@
/*
Serwer strumieniowy, internetowy z danymi wysoko priorytetowymi.
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
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);
}
}
}
}
}
}
}
}