「🏗️」 wip: wtf is going on
This commit is contained in:
5
.clang-format
Normal file
5
.clang-format
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
UseTab: Always
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
AlignConsecutiveDeclarations: true
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
22
3
Normal file
22
3
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include <sys/select.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
char recv_buf[3000];
|
||||||
|
char send_buf[3000];
|
||||||
|
|
||||||
|
int maxfd;
|
||||||
|
int lastid = 0;
|
||||||
|
int sockfd;
|
||||||
|
|
||||||
|
int main(int ac, char **av) {
|
||||||
|
|
||||||
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
}
|
||||||
|
}
|
78
mini_serv.c
78
mini_serv.c
@ -0,0 +1,78 @@
|
|||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int id;
|
||||||
|
char *msg;
|
||||||
|
} t_client;
|
||||||
|
|
||||||
|
t_client clients[FD_SETSIZE];
|
||||||
|
|
||||||
|
char recv_buf[3000];
|
||||||
|
char send_buf[3000];
|
||||||
|
|
||||||
|
int maxfd;
|
||||||
|
int lastid = 0;
|
||||||
|
int sockfd;
|
||||||
|
|
||||||
|
fd_set main_fd, read_fd;
|
||||||
|
|
||||||
|
int send_error(void) {
|
||||||
|
for (int i = 0; i < FD_SETSIZE; i++)
|
||||||
|
if (FD_ISSET(i, &main_fd))
|
||||||
|
close(i);
|
||||||
|
close(sockfd);
|
||||||
|
write(STDERR_FILENO, "Fatal error\n", 12);
|
||||||
|
return (EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av) {
|
||||||
|
if (ac != 2) {
|
||||||
|
write(STDERR_FILENO, "Wrong number of argument\n", 25);
|
||||||
|
return (EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int len;
|
||||||
|
struct sockaddr_in servaddr, cli;
|
||||||
|
|
||||||
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sockfd == -1)
|
||||||
|
return (send_error());
|
||||||
|
|
||||||
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||||
|
servaddr.sin_port = htons(atoi(av[1]));
|
||||||
|
|
||||||
|
|
||||||
|
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
|
||||||
|
return (send_error());
|
||||||
|
|
||||||
|
if (listen(sockfd, 10) != 0)
|
||||||
|
return (send_error());
|
||||||
|
|
||||||
|
FD_ZERO(&main_fd);
|
||||||
|
FD_SET(sockfd, &main_fd);
|
||||||
|
maxfd = sockfd;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
read_fd = main_fd;
|
||||||
|
int ret = select(maxfd + 1, &read_fd, NULL, NULL, NULL);
|
||||||
|
if (ret < 0)
|
||||||
|
return (send_error());
|
||||||
|
|
||||||
|
for (int i = 0; i <= maxfd; i++) {
|
||||||
|
if (FD_ISSET(i, &read_fd)) {
|
||||||
|
if (i == sockfd) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user