TCP Socket with select

This commit is contained in:
Dawid Pietrykowski 2023-02-11 11:00:14 +01:00
parent 92168e0782
commit 609ddd3c87
3 changed files with 150 additions and 0 deletions

51
processes/c2-csocki.c Normal file
View File

@ -0,0 +1,51 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr SockAddr;
typedef struct sockaddr_in SockAddr_in;
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
int main()
{
SockAddr_in adres;
int gniazdo2;
socklen_t rozmiar = sizeof(SockAddr_in);
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
handle_error("socket");
memset(&adres, 0, sizeof(SockAddr_in));
adres.sin_family = AF_INET;
adres.sin_port = htons(4040);
inet_pton(AF_INET, "127.0.0.1", &adres.sin_addr.s_addr);
if ((gniazdo2 = connect(sock, (SockAddr *)&adres, rozmiar)) == -1)
handle_error("connect");
ssize_t liczbaBajtow;
pid_t pid = getpid();
if ((liczbaBajtow = write(sock, &pid, sizeof(pid_t))) == -1)
handle_error("write");
printf("Wysłałem wiadomość: %d\n", pid);
return 0;
}

View File

@ -5,7 +5,9 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
const char *path = "/tmp/socket_test";
void handle_error(char *txt)
{
perror(txt);

97
processes/c2-ssocki.c Normal file
View File

@ -0,0 +1,97 @@
// SERWER
#include <sys/select.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct sockaddr SockAddr;
typedef struct sockaddr_in SockAddr_in;
typedef struct sockaddr_in6 SockAddr_in6;
void handle_error(char *txt)
{
perror(txt);
exit(-1);
}
int main()
{
fd_set set;
FD_ZERO(&set);
SockAddr_in adres4;
SockAddr_in6 adres6;
int gniazdo2;
socklen_t rozmiar4 = sizeof(SockAddr_in);
socklen_t rozmiar6 = sizeof(SockAddr_in6);
int sock6, sock4;
if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
handle_error("socket - 6");
if ((sock4 = socket(AF_INET, SOCK_STREAM, 0)) == -1)
handle_error("socket - 4");
int max;
max = (sock4 < sock6 ? sock6 : sock4);
memset(&adres4, 0, sizeof(SockAddr_in));
memset(&adres6, 0, sizeof(SockAddr_in6));
adres6.sin6_family = AF_INET6;
adres4.sin_family = AF_INET;
adres4.sin_port = htons(4040);
adres6.sin6_port = htons(4041);
inet_pton(AF_INET, "127.0.0.1", &adres4.sin_addr.s_addr);
inet_pton(AF_INET6, "::1", &adres6.sin6_addr.s6_addr);
if (bind(sock4, (SockAddr *)&adres4, rozmiar4) == -1)
handle_error("bind - 4");
if (bind(sock6, (SockAddr *)&adres6, rozmiar6) == -1)
handle_error("bind sock 6");
if (listen(sock4, 5) == -1)
handle_error("listen - 4");
if (listen(sock6, 5) == -1)
handle_error("listen - 4");
while (1)
{
FD_SET(sock4, &set);
FD_SET(sock6, &set);
select(max + 1, &set, NULL, NULL, NULL);
if (FD_ISSET(sock4, &set))
{
gniazdo2 = accept(sock4, (SockAddr *)&adres4, &rozmiar4);
}
else if (FD_ISSET(sock6, &set))
{
gniazdo2 = accept(sock6, (SockAddr *)&adres6, &rozmiar6);
}
int i;
if ((i = fork()) == -1)
{
handle_error("fork");
}
else if (i == 0)
{
pid_t pid;
ssize_t liczbaBajtow;
if ((liczbaBajtow = read(gniazdo2, &pid, sizeof(pid_t))) == -1)
handle_error("read");
printf("%d %d\n", liczbaBajtow, pid);
}
else
{
sigignore(SIGCHLD);
}
}
return 0;
}