wip new version still

This commit is contained in:
asus
2023-02-23 22:10:16 +01:00
parent baf9351a86
commit 3d74075a6a
2 changed files with 26 additions and 1 deletions

BIN
a.out

Binary file not shown.

View File

@@ -91,10 +91,31 @@ char *str_join(char *buf, char *add)
return (newbuf); return (newbuf);
} }
void add_client(t_client *first_client, int fd, int id) {
t_client *new_client;
t_client *client;
new_client = calloc(1, sizeof(*new_client));
if (!new_client)
error("Fatal error\n");
new_client->fd = fd;
new_client->id = id;
new_client->next = NULL;
if (!first_client)
first_client = new_client;
else {
client = first_client;
while (client)
client = client->next;
client->next = new_client;
}
}
int main(int ac, char **av) { int main(int ac, char **av) {
int sockfd; int sockfd;
int client_fd; int client_fd;
int maxfd; int maxfd;
int id;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
fd_set read_set; fd_set read_set;
fd_set write_set; fd_set write_set;
@@ -104,6 +125,7 @@ int main(int ac, char **av) {
if (ac != 2) if (ac != 2)
error("Wrong number of arguments\n"); error("Wrong number of arguments\n");
client = NULL; client = NULL;
id = 0;
first_client = client; first_client = client;
// socket create and verification // socket create and verification
@@ -129,6 +151,8 @@ int main(int ac, char **av) {
FD_ZERO(&write_set); FD_ZERO(&write_set);
client = first_client; client = first_client;
while(client) { while(client) {
write(1, "1", 1);
printf("id: %i\n", client->id);
FD_SET(client->fd, &read_set); FD_SET(client->fd, &read_set);
FD_SET(client->fd, &write_set); FD_SET(client->fd, &write_set);
if (client->fd > maxfd) if (client->fd > maxfd)
@@ -140,7 +164,8 @@ int main(int ac, char **av) {
if (FD_ISSET(sockfd, &read_set)) { if (FD_ISSET(sockfd, &read_set)) {
client_fd = accept(sockfd, NULL, NULL); client_fd = accept(sockfd, NULL, NULL);
add_client(first_client, client_fd, id);
id++;
} }
} }
return (0); return (0);