This commit is contained in:
Your Name 2024-07-14 18:02:31 +03:00
parent 9a7fa213b0
commit 42c2279ec5

View File

@ -20,11 +20,9 @@
#define MSG4 "No namespace available.\n"
#ifndef VERSION
#define VERSION "1.1"
#define VERSION "1.2"
#endif
int iport = 8080;
int oport = 8081;
size_t max_clients = 15;
size_t max_size = 1;
size_t buf_size = 32000;
@ -50,7 +48,7 @@ enum {
SEND
};
int new_server(const int port) {
int new_server(const int port, const int backlog) {
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0)
return -1;
@ -75,7 +73,7 @@ int new_server(const int port) {
if (bind(listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
goto NS_ERROR;
if (listen(listen_fd, 0) < 0)
if (listen(listen_fd, backlog) < 0)
goto NS_ERROR;
return listen_fd;
@ -355,8 +353,12 @@ int main(int argc, char **argv) {
signal(SIGTERM, sig_handler);
signal(SIGCHLD, SIG_IGN);
int backlog = 1;
int iport = 8080;
int oport = 8081;
int opt;
while ((opt = getopt(argc, argv, "i:o:m:d:s:n:b:t:V")) != -1) {
while ((opt = getopt(argc, argv, "i:o:m:d:s:n:b:t:l:V")) != -1) {
switch (opt) {
case 'i':
iport = atoi(optarg);
@ -395,12 +397,16 @@ int main(int argc, char **argv) {
motd = optarg;
break;
case 'l':
backlog = atoi(optarg);
break;
case 'V':
puts("Version: " VERSION "\nWritten under WTFPL License.");
return 0;
default:
printf("%s [iomdsnbtV] - Simple file sharing server\n\t-i NUM Input port\tdefault: %d\n\t-o NUM Output port\tdefault: %d\n\t-m NUM Max clients\tdefault: %zu\n\t-d STR Working dir\tdefault: %s\n\t-s NUM Max file size\tdefault: %zuMB\n\t-n NUM Filename length\tdefault: %zu\n\t-b NUM Buffer size\tdefault: %zuB\n\t-t STR Set MOTD\n\t-V Print version and license\n", prog_name, iport, oport, max_clients, dir, max_size, name_len, buf_size);
printf("%s [iomdsnbtlV] - Simple file sharing server\n\t-i NUM Input port\tdefault: %d\n\t-o NUM Output port\tdefault: %d\n\t-m NUM Max clients\tdefault: %zu\n\t-d STR Working dir\tdefault: %s\n\t-s NUM Max file size\tdefault: %zuMB\n\t-n NUM Filename length\tdefault: %zu\n\t-b NUM Buffer size\tdefault: %zuB\n\t-t STR Set MOTD\n\t-l listen() backlog\tdefault: %d\n\t-V Print version and license\n", prog_name, iport, oport, max_clients, dir, max_size, name_len, buf_size, backlog);
return 0;
}
}
@ -410,13 +416,13 @@ int main(int argc, char **argv) {
return 1;
}
ifd = new_server(iport);
ifd = new_server(iport, backlog);
if (ifd == -1) {
fprintf(stderr, "%s: new_server: input socket: %s\n", prog_name, strerror(errno));
return 1;
}
ofd = new_server(oport);
ofd = new_server(oport, backlog);
if (ofd == -1) {
fprintf(stderr, "%s: new_server: output socket: %s\n", prog_name, strerror(errno));
return 1;
@ -427,4 +433,6 @@ int main(int argc, char **argv) {
else
Proc(ifd, INPUT);
return 0;
}