wip new version with memory allocation
This commit is contained in:
149
mini_serv_2.c
Normal file
149
mini_serv_2.c
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// accept
|
||||
// atoi
|
||||
// bind
|
||||
// bzero
|
||||
// calloc
|
||||
// close
|
||||
// exit
|
||||
// free
|
||||
// listen
|
||||
// malloc
|
||||
// memset
|
||||
// realloc
|
||||
// recv
|
||||
// select
|
||||
// send
|
||||
// socket
|
||||
// sprintf
|
||||
// strcat
|
||||
// strcpy
|
||||
// strlen
|
||||
// strstr
|
||||
// write
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
typedef struct s_client {
|
||||
int fd;
|
||||
int id;
|
||||
char *msg;
|
||||
struct s_client *next;
|
||||
} t_client;
|
||||
|
||||
void error(char *msg) {
|
||||
write(2, msg, strlen(msg));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int extract_message(char **buf, char **msg)
|
||||
{
|
||||
char *newbuf;
|
||||
int i;
|
||||
|
||||
*msg = 0;
|
||||
if (*buf == 0)
|
||||
return (0);
|
||||
i = 0;
|
||||
while ((*buf)[i])
|
||||
{
|
||||
if ((*buf)[i] == '\n')
|
||||
{
|
||||
newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
|
||||
if (newbuf == 0)
|
||||
return (-1);
|
||||
strcpy(newbuf, *buf + i + 1);
|
||||
*msg = *buf;
|
||||
(*msg)[i + 1] = 0;
|
||||
*buf = newbuf;
|
||||
return (1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *str_join(char *buf, char *add)
|
||||
{
|
||||
char *newbuf;
|
||||
int len;
|
||||
|
||||
if (buf == 0)
|
||||
len = 0;
|
||||
else
|
||||
len = strlen(buf);
|
||||
newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
|
||||
if (newbuf == 0)
|
||||
return (0);
|
||||
newbuf[0] = 0;
|
||||
if (buf != 0)
|
||||
strcat(newbuf, buf);
|
||||
free(buf);
|
||||
strcat(newbuf, add);
|
||||
return (newbuf);
|
||||
}
|
||||
|
||||
int main(int ac, char **av) {
|
||||
int sockfd;
|
||||
int client_fd;
|
||||
int maxfd;
|
||||
struct sockaddr_in servaddr;
|
||||
fd_set read_set;
|
||||
fd_set write_set;
|
||||
t_client *client;
|
||||
t_client *first_client;
|
||||
|
||||
if (ac != 2)
|
||||
error("Wrong number of arguments\n");
|
||||
client = NULL;
|
||||
first_client = client;
|
||||
|
||||
// socket create and verification
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd == -1)
|
||||
error("Fatal error\n");
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
|
||||
// assign IP, PORT
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||
servaddr.sin_port = htons(atoi(av[1]));
|
||||
|
||||
// Binding newly created socket to given IP and verification
|
||||
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) == -1)
|
||||
error("Fatal error\n");
|
||||
if (listen(sockfd, 10) == -1)
|
||||
error("Fatal error\n");
|
||||
|
||||
while(1) {
|
||||
maxfd = sockfd;
|
||||
FD_ZERO(&read_set);
|
||||
FD_ZERO(&write_set);
|
||||
client = first_client;
|
||||
while(client) {
|
||||
FD_SET(client->fd, &read_set);
|
||||
FD_SET(client->fd, &write_set);
|
||||
if (client->fd > maxfd)
|
||||
maxfd = client_fd;
|
||||
client = client->next;
|
||||
}
|
||||
|
||||
select(maxfd + 1, &read_set, &write_set, NULL, NULL);
|
||||
|
||||
if (FD_ISSET(sockfd, &read_set)) {
|
||||
client_fd = accept(sockfd, NULL, NULL);
|
||||
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user