8-kirill
This commit is contained in:
50
lab_8/kirill/Makefile
Normal file
50
lab_8/kirill/Makefile
Normal file
@@ -0,0 +1,50 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -std=c11 -g
|
||||
|
||||
all: server_udp_third client_udp_third
|
||||
|
||||
server_udp_third: server.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
client_udp_third: client.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
test_server: server_udp_third
|
||||
@echo "=== Запуск UDP-сервера (каждый третий символ -> пробел) ==="
|
||||
@echo "Выходной файл: out.txt"
|
||||
./server_udp_third 5000 out.txt
|
||||
|
||||
test_client: client_udp_third
|
||||
@echo "=== Запуск UDP-клиента ==="
|
||||
@echo "Создаём input.txt"
|
||||
@printf "abcdefghi\n1234567890\n" > input.txt
|
||||
./client_udp_third 127.0.0.1 5000 input.txt
|
||||
|
||||
test_all: all
|
||||
@echo "=== Автотест UDP (каждый третий символ -> пробел) ==="
|
||||
@printf "abcdefghi\n1234567890\n" > input.txt
|
||||
./server_udp_third 5000 out.txt & \
|
||||
SRV=$$!; \
|
||||
sleep 1; \
|
||||
./client_udp_third 127.0.0.1 5000 input.txt; \
|
||||
wait $$SRV; \
|
||||
echo "--- input.txt ---"; \
|
||||
cat input.txt; \
|
||||
echo "--- out.txt ---"; \
|
||||
cat out.txt
|
||||
|
||||
clean:
|
||||
@echo "Очистка..."
|
||||
rm -f server_udp_third client_udp_third
|
||||
rm -f input.txt out.txt
|
||||
|
||||
help:
|
||||
@echo "Targets:"
|
||||
@echo " all - build server_udp_third and client_udp_third"
|
||||
@echo " test_server - run server (port 5000, out.txt)"
|
||||
@echo " test_client - run client to send input.txt"
|
||||
@echo " test_all - end-to-end UDP test"
|
||||
@echo " clean - remove binaries and test files"
|
||||
@echo " help - show this help"
|
||||
|
||||
.PHONY: all test_server test_client test_all clean help
|
||||
117
lab_8/kirill/client.c
Normal file
117
lab_8/kirill/client.c
Normal file
@@ -0,0 +1,117 @@
|
||||
// client_udp_third.c
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define BUF_SIZE 4096
|
||||
#define END_MARKER "END_OF_TRANSMISSION"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 4) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s <server_ip> <server_port> <input_file>\n",
|
||||
argv[0]);
|
||||
fprintf(stderr,
|
||||
"Example: %s 127.0.0.1 5000 input.txt\n",
|
||||
argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *server_ip = argv[1];
|
||||
const char *server_port = argv[2];
|
||||
const char *input_path = argv[3];
|
||||
|
||||
char *endptr = NULL;
|
||||
errno = 0;
|
||||
long port_long = strtol(server_port, &endptr, 10);
|
||||
if (errno != 0 || endptr == server_port || *endptr != '\0' ||
|
||||
port_long <= 0 || port_long > 65535) {
|
||||
fprintf(stderr, "ERROR: invalid port '%s'\n", server_port);
|
||||
return -1;
|
||||
}
|
||||
int port = (int) port_long;
|
||||
|
||||
FILE *fin = fopen(input_path, "r");
|
||||
if (!fin) {
|
||||
perror("fopen(input_file)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket");
|
||||
fclose(fin);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
if (inet_pton(AF_INET, server_ip, &servaddr.sin_addr) != 1) {
|
||||
fprintf(stderr, "ERROR: invalid IP address '%s'\n", server_ip);
|
||||
close(sockfd);
|
||||
fclose(fin);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
for (;;) {
|
||||
size_t n = fread(buf, 1, sizeof(buf), fin);
|
||||
if (n > 0) {
|
||||
ssize_t sent = sendto(sockfd,
|
||||
buf,
|
||||
n,
|
||||
0,
|
||||
(struct sockaddr *) &servaddr,
|
||||
sizeof(servaddr));
|
||||
if (sent < 0 || (size_t) sent != n) {
|
||||
perror("sendto");
|
||||
close(sockfd);
|
||||
fclose(fin);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (n < sizeof(buf)) {
|
||||
if (ferror(fin)) {
|
||||
perror("fread");
|
||||
close(sockfd);
|
||||
fclose(fin);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fclose(fin) == EOF) {
|
||||
perror("fclose(input_file)");
|
||||
}
|
||||
|
||||
ssize_t sent = sendto(sockfd,
|
||||
END_MARKER,
|
||||
strlen(END_MARKER),
|
||||
0,
|
||||
(struct sockaddr *) &servaddr,
|
||||
sizeof(servaddr));
|
||||
if (sent < 0 || (size_t) sent != strlen(END_MARKER)) {
|
||||
perror("sendto(END_MARKER)");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("File '%s' sent to %s:%d, END marker transmitted.\n",
|
||||
input_path, server_ip, port);
|
||||
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
142
lab_8/kirill/server.c
Normal file
142
lab_8/kirill/server.c
Normal file
@@ -0,0 +1,142 @@
|
||||
// server_udp_third.c
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define BUF_SIZE 4096
|
||||
#define END_MARKER "END_OF_TRANSMISSION"
|
||||
|
||||
static void process_block(char *buf,
|
||||
ssize_t len,
|
||||
long long *pos_counter,
|
||||
long long *repl_counter) {
|
||||
if (!buf || !pos_counter || !repl_counter) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < len; i++) {
|
||||
char c = buf[i];
|
||||
long long pos = *pos_counter;
|
||||
|
||||
if (((pos + 1) % 3 == 0) && c != '\0') {
|
||||
buf[i] = ' ';
|
||||
(*repl_counter)++;
|
||||
}
|
||||
|
||||
(*pos_counter)++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s <port> <output_file>\n",
|
||||
argv[0]);
|
||||
fprintf(stderr,
|
||||
"Example: %s 5000 out.txt\n",
|
||||
argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *endptr = NULL;
|
||||
errno = 0;
|
||||
long port_long = strtol(argv[1], &endptr, 10);
|
||||
if (errno != 0 || endptr == argv[1] || *endptr != '\0' ||
|
||||
port_long <= 0 || port_long > 65535) {
|
||||
fprintf(stderr, "ERROR: invalid port '%s'\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
int port = (int) port_long;
|
||||
|
||||
const char *out_path = argv[2];
|
||||
|
||||
FILE *fout = fopen(out_path, "w");
|
||||
if (!fout) {
|
||||
perror("fopen(output_file)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket");
|
||||
fclose(fout);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
|
||||
perror("bind");
|
||||
close(sockfd);
|
||||
fclose(fout);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("UDP server (third-char) listening on port %d, output file: %s\n",
|
||||
port, out_path);
|
||||
|
||||
char buf[BUF_SIZE];
|
||||
long long pos_counter = 0;
|
||||
long long repl_counter = 0;
|
||||
int done = 0;
|
||||
|
||||
while (!done) {
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t cli_len = sizeof(cliaddr);
|
||||
|
||||
ssize_t n = recvfrom(sockfd,
|
||||
buf,
|
||||
sizeof(buf) - 1,
|
||||
0,
|
||||
(struct sockaddr *) &cliaddr,
|
||||
&cli_len);
|
||||
if (n < 0) {
|
||||
perror("recvfrom");
|
||||
repl_counter = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
buf[n] = '\0';
|
||||
|
||||
if (strcmp(buf, END_MARKER) == 0) {
|
||||
printf("Received END marker from %s:%d, finishing.\n",
|
||||
inet_ntoa(cliaddr.sin_addr),
|
||||
ntohs(cliaddr.sin_port));
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
process_block(buf, n, &pos_counter, &repl_counter);
|
||||
|
||||
if (fwrite(buf, 1, (size_t) n, fout) != (size_t) n) {
|
||||
perror("fwrite");
|
||||
repl_counter = -1;
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fclose(fout) == EOF) {
|
||||
perror("fclose(output_file)");
|
||||
repl_counter = -1;
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
|
||||
printf("Total replacements (every 3rd char): %lld\n", repl_counter);
|
||||
if (repl_counter < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user