wip solution for split messages
This commit is contained in:
34
mini_serv.c
34
mini_serv.c
@@ -68,19 +68,23 @@ void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
int main(int ac, char **av) {
|
||||||
int port;
|
|
||||||
int server_fd;
|
int server_fd;
|
||||||
int client_fd;
|
int client_fd;
|
||||||
int max_fd;
|
|
||||||
int client_id;
|
int client_id;
|
||||||
|
|
||||||
|
int port;
|
||||||
|
int max_fd;
|
||||||
|
int ret;
|
||||||
|
int clients[FD_SETSIZE];
|
||||||
|
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
char tmpbuf[BUFSIZE];
|
char tmpbuf[BUFSIZE];
|
||||||
|
char msg[BUFSIZE];
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
socklen_t addr_len;
|
socklen_t addr_len;
|
||||||
fd_set fdset;
|
fd_set fdset;
|
||||||
fd_set rdset;
|
fd_set rdset;
|
||||||
int clients[FD_SETSIZE];
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (ac != 2)
|
if (ac != 2)
|
||||||
error("Wrong number of arguments\n");
|
error("Wrong number of arguments\n");
|
||||||
@@ -109,8 +113,8 @@ int main(int ac, char **av) {
|
|||||||
clients[client_fd] = client_id;
|
clients[client_fd] = client_id;
|
||||||
if (client_fd > max_fd)
|
if (client_fd > max_fd)
|
||||||
max_fd = client_fd;
|
max_fd = client_fd;
|
||||||
sprintf(buf, "server: client %d just arrived\n", client_id);
|
sprintf(msg, "server: client %d just arrived\n", client_id);
|
||||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
client_id++;
|
client_id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,13 +126,23 @@ int main(int ac, char **av) {
|
|||||||
bzero(buf, BUFSIZE);
|
bzero(buf, BUFSIZE);
|
||||||
ret = recv(client_fd, buf, BUFSIZE, 0);
|
ret = recv(client_fd, buf, BUFSIZE, 0);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
sprintf(buf, "server: client %d just left\n", clients[client_fd]);
|
sprintf(msg, "server: client %d just left\n", clients[client_fd]);
|
||||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
FD_CLR(client_fd, &fdset);
|
FD_CLR(client_fd, &fdset);
|
||||||
}
|
}
|
||||||
else if (ret > 0) {
|
else if (ret > 0) {
|
||||||
sprintf(tmpbuf, "client %d: %s", clients[client_fd], buf);
|
|
||||||
broadcast(tmpbuf, &fdset, max_fd, server_fd, client_fd);
|
for(int i = 0, j = 0; buf[i] != '\n'; ++i, ++j) {
|
||||||
|
tmpbuf[j] = buf[i];
|
||||||
|
if (buf[i] == '\n') {
|
||||||
|
tmpbuf[j] = '\0';
|
||||||
|
sprintf(msg, "client %d: %s\n", clients[client_fd], tmpbuf);
|
||||||
|
broadcast(msg, &fdset, max_fd, server_fd, client_fd);
|
||||||
|
bzero(tmpbuf, strlen(tmpbuf));
|
||||||
|
j = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client_fd++;
|
client_fd++;
|
||||||
|
|||||||
125
mini_serv_2.c
125
mini_serv_2.c
@@ -1,125 +0,0 @@
|
|||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
// write
|
|
||||||
// close
|
|
||||||
// select
|
|
||||||
// socket
|
|
||||||
// accept
|
|
||||||
// listen
|
|
||||||
// send
|
|
||||||
// recv
|
|
||||||
// bind
|
|
||||||
// strstr
|
|
||||||
// malloc
|
|
||||||
// realloc
|
|
||||||
// free
|
|
||||||
// calloc
|
|
||||||
// bzero
|
|
||||||
// atoi
|
|
||||||
// sprintf
|
|
||||||
// strlen
|
|
||||||
// exit
|
|
||||||
// strcpy
|
|
||||||
// strcat
|
|
||||||
// memset
|
|
||||||
|
|
||||||
#define BUFSIZE 42000
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
|
||||||
int server_fd;
|
|
||||||
int client_fd;
|
|
||||||
int client_id;
|
|
||||||
int port;
|
|
||||||
int max_fd;
|
|
||||||
int ret;
|
|
||||||
char buf[BUFSIZE];
|
|
||||||
char tmp_buf[BUFSIZE];
|
|
||||||
int clients[FD_SETSIZE];
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
fd_set fdset;
|
|
||||||
fd_set rdset;
|
|
||||||
|
|
||||||
if (ac != 2)
|
|
||||||
error("Wrong number of arguments\n");
|
|
||||||
port = atoi(av[1]);
|
|
||||||
addr_len = sizeof(addr);
|
|
||||||
|
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server_fd == -1)
|
|
||||||
error("Fatal error\n");
|
|
||||||
bzero(&addr, addr_len);
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
|
||||||
addr.sin_port = htons(port);
|
|
||||||
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;
|
|
||||||
client_id = 0;
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
rdset = fdset;
|
|
||||||
|
|
||||||
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
102
mini_serv_3.c
102
mini_serv_3.c
@@ -1,102 +0,0 @@
|
|||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define BUFSIZE 42000
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
|
||||||
int server_fd;
|
|
||||||
int client_fd;
|
|
||||||
int client_id;
|
|
||||||
int port;
|
|
||||||
int max_fd;
|
|
||||||
int ret;
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
fd_set fdset;
|
|
||||||
fd_set rdset;
|
|
||||||
char buf[BUFSIZE];
|
|
||||||
char tmp_buf[BUFSIZE];
|
|
||||||
int clients[FD_SETSIZE];
|
|
||||||
|
|
||||||
if (ac != 2)
|
|
||||||
error("Fatal error\n");
|
|
||||||
port = atoi(av[1]);
|
|
||||||
addr_len = sizeof(addr);
|
|
||||||
|
|
||||||
// socket create and verification
|
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server_fd == -1)
|
|
||||||
error("Fatal error\n");
|
|
||||||
bzero(&addr, addr_len);
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
|
||||||
addr.sin_port = htons(port);
|
|
||||||
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;
|
|
||||||
client_id = 0;
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
rdset = fdset;
|
|
||||||
|
|
||||||
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
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++;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user