working
This commit is contained in:
45
mini_serv.c
45
mini_serv.c
@@ -31,7 +31,7 @@
|
|||||||
// strstr
|
// strstr
|
||||||
// write
|
// write
|
||||||
|
|
||||||
#define BUFSIZE 1024
|
#define BUFSIZE 42000
|
||||||
|
|
||||||
void error(char *str) {
|
void error(char *str) {
|
||||||
write(2, str, strlen(str));
|
write(2, str, strlen(str));
|
||||||
@@ -57,24 +57,13 @@ int init_socket(struct sockaddr_in *addr, int len, int port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
|
void broadcast(char *buf, fd_set *set, int max_fd, int server_fd, int sender_fd) {
|
||||||
write(1, "---\n", 4);
|
|
||||||
for(int i = 0; i <= max_fd; ++i) {
|
for(int i = 0; i <= max_fd; ++i) {
|
||||||
printf("[%d]\n", i);
|
|
||||||
if (i == server_fd)
|
if (i == server_fd)
|
||||||
{
|
|
||||||
write(1, "is server\n", 10);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
if (i == sender_fd)
|
if (i == sender_fd)
|
||||||
{
|
|
||||||
write(1, "is sender\n", 10);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
if (FD_ISSET(i, set))
|
if (FD_ISSET(i, set))
|
||||||
{
|
|
||||||
send(i, buf, strlen(buf), 0);
|
send(i, buf, strlen(buf), 0);
|
||||||
write(1, buf, strlen(buf));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +74,7 @@ int main(int ac, char **av) {
|
|||||||
int max_fd;
|
int max_fd;
|
||||||
int client_id;
|
int client_id;
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
|
char tmpbuf[BUFSIZE];
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
socklen_t addr_len;
|
socklen_t addr_len;
|
||||||
fd_set fdset;
|
fd_set fdset;
|
||||||
@@ -99,6 +89,7 @@ int main(int ac, char **av) {
|
|||||||
addr_len = sizeof(addr);
|
addr_len = sizeof(addr);
|
||||||
|
|
||||||
server_fd = init_socket(&addr, addr_len, port);
|
server_fd = init_socket(&addr, addr_len, port);
|
||||||
|
|
||||||
FD_ZERO(&fdset);
|
FD_ZERO(&fdset);
|
||||||
FD_SET(server_fd, &fdset);
|
FD_SET(server_fd, &fdset);
|
||||||
max_fd = server_fd;
|
max_fd = server_fd;
|
||||||
@@ -107,14 +98,12 @@ int main(int ac, char **av) {
|
|||||||
/*
|
/*
|
||||||
*/
|
*/
|
||||||
while (1) {
|
while (1) {
|
||||||
// FD_ZERO(&rdset);
|
|
||||||
rdset = fdset;
|
rdset = fdset;
|
||||||
|
|
||||||
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))
|
if (FD_ISSET(server_fd, &rdset)) {
|
||||||
{
|
|
||||||
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
|
client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
|
||||||
FD_SET(client_fd, &fdset);
|
FD_SET(client_fd, &fdset);
|
||||||
clients[client_fd] = client_id;
|
clients[client_fd] = client_id;
|
||||||
@@ -129,25 +118,21 @@ int main(int ac, char **av) {
|
|||||||
client_fd = 0;
|
client_fd = 0;
|
||||||
while(client_fd <= max_fd) {
|
while(client_fd <= max_fd) {
|
||||||
ret = 1;
|
ret = 1;
|
||||||
if (FD_ISSET(client_fd, &rdset))
|
if (FD_ISSET(client_fd, &rdset)) {
|
||||||
|
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(buf, "server: client %d just left\n", clients[client_fd]);
|
||||||
write(1, buf, strlen(buf));
|
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
||||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
FD_CLR(client_fd, &fdset);
|
||||||
FD_CLR(client_fd, &fdset);
|
}
|
||||||
}
|
else if (ret > 0) {
|
||||||
else {
|
sprintf(tmpbuf, "client %d: %s", clients[client_fd], buf);
|
||||||
sprintf(buf, "client %d: %s\n", clients[client_fd], buf);
|
broadcast(tmpbuf, &fdset, max_fd, server_fd, client_fd);
|
||||||
write(1, buf, strlen(buf));
|
}
|
||||||
broadcast(buf, &fdset, max_fd, server_fd, client_fd);
|
|
||||||
}
|
}
|
||||||
client_fd++;
|
client_fd++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// "client %d: "
|
|
||||||
// "server: client %d just left\n"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
196
mini_serv_ex.c
196
mini_serv_ex.c
@@ -1,79 +1,147 @@
|
|||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/select.h>
|
|
||||||
|
|
||||||
typedef int bool;
|
// write
|
||||||
|
// close
|
||||||
|
// select
|
||||||
|
// socket
|
||||||
|
// accept
|
||||||
|
// listen
|
||||||
|
// send
|
||||||
|
// recv
|
||||||
|
// bind
|
||||||
|
// strstr
|
||||||
|
// malloc
|
||||||
|
// realloc
|
||||||
|
// free
|
||||||
|
// calloc
|
||||||
|
// bzero
|
||||||
|
// atoi
|
||||||
|
// sprintf
|
||||||
|
// strlen
|
||||||
|
// exit
|
||||||
|
// strcpy
|
||||||
|
// strcat
|
||||||
|
// memset
|
||||||
|
|
||||||
bool check_arguments(int argc)
|
#define BUFSIZE 42000
|
||||||
{
|
|
||||||
return (argc > 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void putstr_fd(char *str, int fd)
|
void error(char *msg) {
|
||||||
{
|
write(2, msg, strlen(msg));
|
||||||
write(fd, str, strlen(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
void arguments_error(void)
|
|
||||||
{
|
|
||||||
putstr_fd("Wrong number of arguments\n", 2);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fatal_error(void)
|
int main(int ac, char **av) {
|
||||||
{
|
int server_fd;
|
||||||
putstr_fd("Fatal error\n", 2);
|
// int client_fd;
|
||||||
exit(1);
|
int client_id;
|
||||||
}
|
int port;
|
||||||
|
int max_fd;
|
||||||
|
// 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;
|
||||||
|
|
||||||
int get_port(char *arg)
|
if (ac != 2)
|
||||||
{
|
error("Wrong number of arguments\n");
|
||||||
int port = atoi(arg);
|
port = atoi(av[1]);
|
||||||
return (port);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int port;
|
|
||||||
int server_fd;
|
|
||||||
int client_fd;
|
|
||||||
char buf[1024];
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
fd_set fds;
|
|
||||||
|
|
||||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = htons(port);
|
|
||||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
addr_len = sizeof(addr);
|
addr_len = sizeof(addr);
|
||||||
|
|
||||||
if (!check_arguments(argc))
|
// socket create and verification
|
||||||
arguments_error();
|
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if ((port = get_port(argv[1])) == -1)
|
if (server_fd == -1)
|
||||||
fatal_error();
|
error("Fatal error\n");
|
||||||
printf("port = %d\n", port);
|
bzero(&addr, addr_len);
|
||||||
if (bind(server_fd, (struct sockaddr *)&addr, addr_len)== -1)
|
// assign IP, PORT
|
||||||
fatal_error();
|
addr.sin_family = AF_INET;
|
||||||
if (listen(server_fd, 0) == -1)
|
addr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
|
||||||
fatal_error();
|
addr.sin_port = htons(port);
|
||||||
while (1)
|
// Binding newly created socket to given IP and verification
|
||||||
{
|
if ((bind(server_fd, (const struct sockaddr *)&addr, addr_len)) == -1)
|
||||||
if ((client_fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len)) == -1)
|
error("Fatal error\n");
|
||||||
fatal_error();
|
if (listen(server_fd, 10) == -1)
|
||||||
FD_ZERO(&fds);
|
error("Fatal error\n");
|
||||||
FD_SET(client_fd, &fds);
|
|
||||||
select(client_fd + 1, &fds, NULL, NULL, NULL);
|
FD_ZERO(&fdset);
|
||||||
if (FD_ISSET(client_fd, &fds))
|
FD_SET(server_fd, &fdset);
|
||||||
{
|
max_fd = server_fd;
|
||||||
recv(client_fd, buf, 1024, 0);
|
client_id = 0;
|
||||||
printf("Recv: %s\n", buf);
|
|
||||||
}
|
while(1) {
|
||||||
|
rdset = fdset;
|
||||||
|
|
||||||
|
select(max_fd + 1, &rdset, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
// new connection
|
||||||
|
|
||||||
|
|
||||||
|
// new messages and close
|
||||||
}
|
}
|
||||||
|
|
||||||
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