wip wup
This commit is contained in:
30
mini_serv.c
30
mini_serv.c
@@ -29,6 +29,8 @@
|
||||
// strstr
|
||||
// write
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
void putstr(int fd, char *str) {
|
||||
write(fd, str, strlen(str));
|
||||
}
|
||||
@@ -60,29 +62,31 @@ int main(int ac, char **av) {
|
||||
int port;
|
||||
int server_fd;
|
||||
int client_fd;
|
||||
char buf[1024];
|
||||
char buf[BUFSIZE];
|
||||
struct sockaddr_in addr;
|
||||
socklen_t len;
|
||||
fd_set fds;
|
||||
socklen_t addr_len;
|
||||
fd_set fdset;
|
||||
|
||||
if (ac != 2)
|
||||
error("Wrong number of arguments\n");
|
||||
if ( (port = atoi(av[1])) == -1)
|
||||
error("Fatal error\n");
|
||||
len = sizeof(addr);
|
||||
addr_len = sizeof(addr);
|
||||
|
||||
server_fd = init_socket(&addr, len, port);
|
||||
server_fd = init_socket(&addr, addr_len, port);
|
||||
|
||||
while (1) {
|
||||
if ( (client_fd = accept(server_fd, (struct sockaddr *)&addr, &len)) == -1)
|
||||
if ( (client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len)) == -1)
|
||||
error("Fatal error\n");
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(client_fd, &fds);
|
||||
select(client_fd + 1, &fds, NULL, NULL, NULL);
|
||||
if (FD_ISSET(client_fd, &fds)) {
|
||||
recv(client_fd, buf, 1024, 0);
|
||||
putstr(1, buf);
|
||||
}
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(client_fd, &fdset);
|
||||
select(client_fd + 1, &fdset, NULL, NULL, NULL);
|
||||
if (FD_ISSET(client_fd, &fdset))
|
||||
recv(client_fd, buf, BUFSIZE, 0);
|
||||
// "server: client %d just arrived\n"
|
||||
// "client %d: "
|
||||
// "server: client %d just left\n"
|
||||
//while ()
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user