wip change architecture
This commit is contained in:
36
mini_serv.c
36
mini_serv.c
@@ -1,5 +1,3 @@
|
|||||||
//#include <errno.h>
|
|
||||||
//#include <netdb.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -8,35 +6,12 @@
|
|||||||
#include <stdio.h> //sprintf
|
#include <stdio.h> //sprintf
|
||||||
|
|
||||||
|
|
||||||
// accept
|
|
||||||
// atoi
|
|
||||||
// bind
|
|
||||||
// bzero
|
|
||||||
// calloc
|
|
||||||
// close
|
|
||||||
// exit
|
|
||||||
// free
|
|
||||||
// listen
|
|
||||||
// malloc
|
|
||||||
// memset
|
|
||||||
// realloc
|
|
||||||
// recv
|
|
||||||
// select
|
|
||||||
// send
|
|
||||||
// socket
|
|
||||||
// sprintf
|
|
||||||
// strcat
|
|
||||||
// strcpy
|
|
||||||
// strlen
|
|
||||||
// strstr
|
|
||||||
// write
|
|
||||||
|
|
||||||
#define BUFSIZE 42000
|
#define BUFSIZE 42000
|
||||||
|
|
||||||
typedef struct s_clients {
|
typedef struct s_client {
|
||||||
int id;
|
int id;
|
||||||
char msg[BUFSIZE - 20];
|
char msg[1024];
|
||||||
} t_clients;
|
} t_client;
|
||||||
|
|
||||||
void error(char *str) {
|
void error(char *str) {
|
||||||
write(2, str, strlen(str));
|
write(2, str, strlen(str));
|
||||||
@@ -80,8 +55,8 @@ int main(int ac, char **av) {
|
|||||||
int port;
|
int port;
|
||||||
int max_fd;
|
int max_fd;
|
||||||
int ret;
|
int ret;
|
||||||
t_clients clients[FD_SETSIZE];
|
t_client clients[FD_SETSIZE];
|
||||||
t_clients client;
|
t_client client;
|
||||||
|
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
char msg[BUFSIZE];
|
char msg[BUFSIZE];
|
||||||
@@ -103,6 +78,7 @@ int main(int ac, char **av) {
|
|||||||
FD_SET(server_fd, &fdset);
|
FD_SET(server_fd, &fdset);
|
||||||
max_fd = server_fd;
|
max_fd = server_fd;
|
||||||
client_id = 0;
|
client_id = 0;
|
||||||
|
bzero(&clients, sizeof(clients));
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
rdset = fdset;
|
rdset = fdset;
|
||||||
|
|||||||
125
mini_serv_01.c
Normal file
125
mini_serv_01.c
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define BUFSIZE 42000
|
||||||
|
|
||||||
|
typedef struct s_client {
|
||||||
|
int id;
|
||||||
|
char msg[1024];
|
||||||
|
} t_client;
|
||||||
|
|
||||||
|
// GLOBALS
|
||||||
|
int server_fd;
|
||||||
|
int id;
|
||||||
|
int max_fd;
|
||||||
|
socklen_t addr_len;
|
||||||
|
fd_set fdset;
|
||||||
|
t_client clients[FD_SETSIZE];
|
||||||
|
|
||||||
|
void error(char *msg) {
|
||||||
|
write(2, msg, strlen(msg));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
|
||||||
|
for(int i = 0; i <= max_fd; ++i) {
|
||||||
|
if (i == server_fd)
|
||||||
|
continue;
|
||||||
|
if (i == sender_fd)
|
||||||
|
continue;
|
||||||
|
if (FD_ISSET(i, set))
|
||||||
|
send(i, buf, strlen(buf), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_socket(struct sockaddr *addr) {
|
||||||
|
fd_set rdset;
|
||||||
|
int client_fd;
|
||||||
|
int ret;
|
||||||
|
t_client client;
|
||||||
|
char buf[BUFSIZE];
|
||||||
|
char msg[BUFSIZE];
|
||||||
|
|
||||||
|
rdset = fdset;
|
||||||
|
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (FD_ISSET(server_fd, &rdset)) {
|
||||||
|
client_fd = accept(server_fd, addr, &addr_len);
|
||||||
|
FD_SET(client_fd, &fdset);
|
||||||
|
if (client_fd > max_fd)
|
||||||
|
max_fd = client_fd;
|
||||||
|
client = clients[client_fd];
|
||||||
|
client.id = id;
|
||||||
|
bzero(client.msg, strlen(client.msg));
|
||||||
|
sprintf(buf, "server: client %d arrived\n", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
client_fd = 0;
|
||||||
|
while(client_fd < max_fd) {
|
||||||
|
if (FD_ISSET(client_fd, &rdset)) {
|
||||||
|
bzero(buf, BUFSIZE);
|
||||||
|
ret = recv(client_fd, buf, BUFSIZE, 0);
|
||||||
|
if (ret == 0) {
|
||||||
|
sprintf(msg, "server: client %d just left\n", clients[client_fd].id);
|
||||||
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
FD_CLR(client_fd, &fdset);
|
||||||
|
}
|
||||||
|
else if (ret > 0) {
|
||||||
|
|
||||||
|
client = clients[client_fd];
|
||||||
|
for(int i = 0, j = strlen(client.msg); i < ret; ++i, ++j) {
|
||||||
|
client.msg[j] = buf[i];
|
||||||
|
if (buf[i] == '\n') {
|
||||||
|
client.msg[j] = '\0';
|
||||||
|
sprintf(msg, "client %d: %s\n", client.id, client.msg);
|
||||||
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
bzero(client.msg, strlen(client.msg));
|
||||||
|
j = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client_fd++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av) {
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
int port;
|
||||||
|
|
||||||
|
if (ac != 2)
|
||||||
|
error("Fatal error\n");
|
||||||
|
port = atoi(av[1]);
|
||||||
|
addr_len = sizeof(addr);
|
||||||
|
|
||||||
|
// socket create and verification
|
||||||
|
if ( (server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
||||||
|
error("Fatal error\n");
|
||||||
|
bzero(&addr, addr_len);
|
||||||
|
|
||||||
|
// assign IP, PORT
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
// Binding newly created socket to given IP and verification
|
||||||
|
if (bind(server_fd, (const struct sockaddr *)&addr, addr_len) == -1)
|
||||||
|
error("Fatal error\n");
|
||||||
|
if (listen(server_fd, 10) == -1)
|
||||||
|
error("Fatal error\n");
|
||||||
|
|
||||||
|
FD_ZERO(&fdset);
|
||||||
|
FD_SET(server_fd, &fdset);
|
||||||
|
max_fd = server_fd;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
handle_socket((struct sockaddr *)&addr);
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user