twice
This commit is contained in:
110
mini_serv_ex.c
110
mini_serv_ex.c
@@ -1,5 +1,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
@@ -34,15 +35,27 @@ void error(char *msg) {
|
|||||||
exit(1);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
int main(int ac, char **av) {
|
||||||
int server_fd;
|
int server_fd;
|
||||||
// int client_fd;
|
int client_fd;
|
||||||
int client_id;
|
int client_id;
|
||||||
int port;
|
int port;
|
||||||
int max_fd;
|
int max_fd;
|
||||||
// char buf[BUFSIZE];
|
int ret;
|
||||||
// char tmp_buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
// int clients[FD_SETSIZE];
|
char tmp_buf[BUFSIZE];
|
||||||
|
int clients[FD_SETSIZE];
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
socklen_t addr_len;
|
socklen_t addr_len;
|
||||||
fd_set fdset;
|
fd_set fdset;
|
||||||
@@ -53,16 +66,13 @@ int main(int ac, char **av) {
|
|||||||
port = atoi(av[1]);
|
port = atoi(av[1]);
|
||||||
addr_len = sizeof(addr);
|
addr_len = sizeof(addr);
|
||||||
|
|
||||||
// socket create and verification
|
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (server_fd == -1)
|
if (server_fd == -1)
|
||||||
error("Fatal error\n");
|
error("Fatal error\n");
|
||||||
bzero(&addr, addr_len);
|
bzero(&addr, addr_len);
|
||||||
// assign IP, PORT
|
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||||
addr.sin_port = htons(port);
|
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)
|
if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1)
|
||||||
error("Fatal error\n");
|
error("Fatal error\n");
|
||||||
if (listen(server_fd, 10) == -1)
|
if (listen(server_fd, 10) == -1)
|
||||||
@@ -79,69 +89,37 @@ int main(int ac, char **av) {
|
|||||||
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
||||||
|
|
||||||
// new connection
|
// new connection
|
||||||
|
if (FD_ISSET(server_fd, &rdset)) {
|
||||||
|
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
|
||||||
|
FD_SET(client_fd, &fdset);
|
||||||
|
clients[client_fd] = client_id;
|
||||||
|
if (client_fd > max_fd)
|
||||||
|
max_fd = client_fd;
|
||||||
|
sprintf(buf, "server: client %d just arrived\n", client_id);
|
||||||
|
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
client_id++;
|
||||||
|
}
|
||||||
|
|
||||||
// new messages and close
|
// new messages and close
|
||||||
|
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(buf, "server: client %d just left\n", clients[client_fd]);
|
||||||
|
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
FD_CLR(client_fd, &fdset);
|
||||||
|
}
|
||||||
|
else if (ret > 0) {
|
||||||
|
sprintf(tmp_buf, "client %d: %s", clients[client_fd], buf);
|
||||||
|
broadcast(tmp_buf, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client_fd++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
// len = sizeof(cli);
|
|
||||||
// connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
|
|
||||||
// if (connfd < 0) {
|
|
||||||
// printf("server acccept failed...\n");
|
|
||||||
// exit(0);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// printf("server acccept the client...\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//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);
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user