「🏗️」 wip: aaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh why ???
This commit is contained in:
22
3
22
3
@ -1,22 +0,0 @@
|
||||
#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) {
|
||||
}
|
||||
}
|
@ -34,6 +34,10 @@
|
||||
];
|
||||
packages = with pkgs; [
|
||||
nixfmt-rfc-style
|
||||
glibcInfo
|
||||
man
|
||||
man-pages
|
||||
man-pages-posix
|
||||
valgrind
|
||||
gdb
|
||||
];
|
||||
|
37
mini_serv.c
37
mini_serv.c
@ -22,6 +22,21 @@ int sockfd;
|
||||
|
||||
fd_set main_fd, read_fd;
|
||||
|
||||
void append_client(int cli_fd) {
|
||||
lastid++;
|
||||
clients[cli_fd].id = lastid;
|
||||
clients[cli_fd].msg = NULL;
|
||||
FD_SET(cli_fd, &main_fd);
|
||||
}
|
||||
|
||||
void send_all() {
|
||||
size_t len = strlen(send_buf);
|
||||
|
||||
for (int i = 0; i <= maxfd; i++)
|
||||
if (FD_ISSET(i, &read_fd))
|
||||
send(i, send_buf, len, 0);
|
||||
}
|
||||
|
||||
int send_error(void) {
|
||||
for (int i = 0; i < FD_SETSIZE; i++)
|
||||
if (FD_ISSET(i, &main_fd))
|
||||
@ -37,17 +52,16 @@ int main(int ac, char **av) {
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int len;
|
||||
socklen_t 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]));
|
||||
|
||||
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());
|
||||
@ -56,6 +70,7 @@ int main(int ac, char **av) {
|
||||
return (send_error());
|
||||
|
||||
FD_ZERO(&main_fd);
|
||||
FD_ZERO(&read_fd);
|
||||
FD_SET(sockfd, &main_fd);
|
||||
maxfd = sockfd;
|
||||
|
||||
@ -68,9 +83,19 @@ int main(int ac, char **av) {
|
||||
for (int i = 0; i <= maxfd; i++) {
|
||||
if (FD_ISSET(i, &read_fd)) {
|
||||
if (i == sockfd) {
|
||||
len = sizeof(cli);
|
||||
int cli_fd = accept(i, (struct sockaddr *)&cli, &len);
|
||||
if (cli_fd < 0)
|
||||
return (send_error());
|
||||
|
||||
if (cli_fd > maxfd)
|
||||
maxfd = cli_fd;
|
||||
append_client(cli_fd);
|
||||
bzero(send_buf, 3000);
|
||||
sprintf(send_buf, "server: client %d just arrived\n",
|
||||
lastid);
|
||||
send_all();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user