컴공 일기278
게시글 주소: https://orbi.kr/00073281944
가산점을 주는 네트워크 과제입니다.
4장 이내 11pt 설계보고서, 소스코드 원본을 제출해야 하며
연구실에서 1:1 인터뷰를 통해 데모 실행 및 설명을 해야 합니다.
TCP/IP 통신기반의 공유 문서 작성 및 읽기 프로그램입니다.
소켓 프로그래밍과 시스템 프로그래밍에서 자주 사용되는 기법을 적절히 조화시켜야 하는데 가장 핵심적인 기능 중 하나는, write 명령이 클라이언트로부터 왔을 때, 서버는 한줄씩 데이터를 받아들이는 겁니다. 이걸 구현하는 것이 이 과제의 핵심 중 하나가 아닌가 생각하네요.
소켓의 본질을 알고 있어야, 이 기능을 구현할 수 있거든요.
소켓의 본질은 파일입니다. 파일은 데이터 단위가 Stream인데,
이 스트림은 시작은 확실히 정해져 있지만, 끝이 어딘지 확실하지 않다는 특징을 갖고 있습니다. 그렇기 때문에 ‘줄 입력‘이 여기서 종료되었다는 판단을 아무런 정보가 없다면 서버는 할 수 없죠.
따라서 적절한 시그널을 주고받는 프로토콜 절차가 있어야 합니다.
인터뷰에서 시그널을 주고받음으로써 줄의 끝이 어디까지인지 서버가 알도록 한다는 말씀을 드렸을 때, 인터뷰 평가 사항에 무엇인가를 막 적고 계시더라구요. 그때 조금 확실히 알게된 것 같습니다.
과제의 의도가 결국 ‘소켓’이 무엇인지 정확히 알고 있느냐라는 걸요.. 사실 이 얘기는 네트워크 이론과 운영체제론을 알고 있어야 이해할 수 있을 겁니다. 조금 더 다듬어서 비동기 입출력까지 지원하는 서버를 한번 만들어 보려구요. 설계 구조를 완전히 바꿔야 겠지만, 오랜만에 아주 재미있는 프로젝트를 하게 되어서 이 내용물은 깃허브에 올려 볼 것 같습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_DOCS 100
#define MAX_SECTIONS 10
#define MAX_TITLE 64
#define MAX_LINE 256
#define MAX_LINES 10
#define BUF_SIZE 1024
typedef struct {
char title[MAX_TITLE];
char section_titles[MAX_SECTIONS][MAX_TITLE];
char section_contents[MAX_SECTIONS][MAX_LINES][MAX_LINE];
int section_line_count[MAX_SECTIONS];
int section_count;
} Document;
typedef struct WriteRequest {
int client_sock;
int estimated_lines;
struct WriteRequest *next;
} WriteRequest;
Document docs[MAX_DOCS];
int doc_count = 0;
pthread_mutex_t docs_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t section_mutex[MAX_DOCS][MAX_SECTIONS];
pthread_cond_t section_cond[MAX_DOCS][MAX_SECTIONS];
int section_writing[MAX_DOCS][MAX_SECTIONS] = {{0}};
WriteRequest *section_queue[MAX_DOCS][MAX_SECTIONS] = {{{0}}};
pthread_mutex_t section_queue_mutex[MAX_DOCS][MAX_SECTIONS];
pthread_cond_t section_queue_cond[MAX_DOCS][MAX_SECTIONS];
void send_all(int sock, const char *msg) {
send(sock, msg, strlen(msg), 0);
}
Document* find_doc(const char* title) {
for (int i = 0; i < doc_count; ++i) {
if (strcmp(docs[i].title, title) == 0)
return &docs[i];
}
return NULL;
}
ssize_t read_line(int sock, char *buf, size_t max_len) {
size_t i = 0;
char ch;
while (i < max_len - 1) {
ssize_t n = recv(sock, &ch, 1, 0);
if (n <= 0) break;
if (ch == '\n') break;
buf[i++] = ch;
}
buf[i] = '\0';
return i;
}
void parse_command(const char* input, char* args[], int* argc) {
*argc = 0;
const char* p = input;
while (*p) {
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
p++;
const char* start = p;
while (*p && *p != '"') p++;
int len = p - start;
args[*argc] = malloc(len + 1);
strncpy(args[*argc], start, len);
args[*argc][len] = '\0';
(*argc)++;
if (*p == '"') p++;
} else {
const char* start = p;
while (*p && *p != ' ' && *p != '\t' && *p != '\n') p++;
int len = p - start;
args[*argc] = malloc(len + 1);
strncpy(args[*argc], start, len);
args[*argc][len] = '\0';
(*argc)++;
}
}
}
void* client_handler(void* arg);
int main(int argc, char* argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <IP> <Port>\n", argv[0]);
exit(1);
}
for (int i = 0; i < MAX_DOCS; ++i)
for (int j = 0; j < MAX_SECTIONS; ++j) {
pthread_mutex_init(§ion_mutex[i][j], NULL);
pthread_cond_init(§ion_cond[i][j], NULL);
pthread_mutex_init(§ion_queue_mutex[i][j], NULL);
pthread_cond_init(§ion_queue_cond[i][j], NULL);
}
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr, client_addr;
socklen_t addrlen = sizeof(client_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
listen(server_sock, 10);
printf("[Server] Listening on %s:%s\n", argv[1], argv[2]);
while (1) {
int *client_sock = malloc(sizeof(int));
*client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
pthread_t tid;
pthread_create(&tid, NULL, client_handler, client_sock);
pthread_detach(tid);
}
close(server_sock);
return 0;
}
void* client_handler(void* arg) {
int client_sock = *(int*)arg;
free(arg);
char buf[BUF_SIZE];
char* args[64];
int argc;
while (1) {
memset(buf, 0, sizeof(buf));
if (read_line(client_sock, buf, sizeof(buf)) <= 0) break;
parse_command(buf, args, &argc);
if (argc == 0) continue;
if (strcmp(args[0], "create") == 0) {
pthread_mutex_lock(&docs_mutex);
if (argc < 3 || doc_count >= MAX_DOCS) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Invalid create command.\n");
continue;
}
if (find_doc(args[1])) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Document already exists.\n");
continue;
}
int section_count = atoi(args[2]);
if (section_count <= 0 || section_count > MAX_SECTIONS || argc != 3 + section_count) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Invalid section count or titles.\n");
continue;
}
strcpy(docs[doc_count].title, args[1]);
docs[doc_count].section_count = section_count;
for (int i = 0; i < section_count; ++i) {
strcpy(docs[doc_count].section_titles[i], args[3 + i]);
docs[doc_count].section_line_count[i] = 0;
}
++doc_count;
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[OK] Document created.\n");
}
else if (strcmp(args[0], "write") == 0) {
if (argc < 3) {
send_all(client_sock, "[Error] Invalid write command.\n");
continue;
}
pthread_mutex_lock(&docs_mutex);
Document* doc = find_doc(args[1]);
if (!doc) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Document not found.\n");
continue;
}
int section_idx = -1;
for (int i = 0; i < doc->section_count; ++i)
if (strcmp(doc->section_titles[i], args[2]) == 0) {
section_idx = i;
break;
}
if (section_idx == -1) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Section not found.\n");
continue;
}
int doc_idx = doc - docs;
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[OK] You can start writing. Send <END> to finish.\n>> ");
int line_count = 0;
char line[MAX_LINE];
char temp_lines[MAX_LINES][MAX_LINE];
while (1) {
if (read_line(client_sock, line, sizeof(line)) <= 0) break;
if (strcmp(line, "<END>") == 0) break;
if (line_count < MAX_LINES)
strncpy(temp_lines[line_count++], line, MAX_LINE - 1);
send_all(client_sock, ">> ");
}
WriteRequest *req = malloc(sizeof(WriteRequest));
req->client_sock = client_sock;
req->estimated_lines = line_count;
req->next = NULL;
pthread_mutex_lock(§ion_queue_mutex[doc_idx][section_idx]);
if (!section_queue[doc_idx][section_idx] || line_count < section_queue[doc_idx][section_idx]->estimated_lines) {
req->next = section_queue[doc_idx][section_idx];
section_queue[doc_idx][section_idx] = req;
} else {
WriteRequest *cur = section_queue[doc_idx][section_idx];
while (cur->next && cur->next->estimated_lines <= line_count)
cur = cur->next;
req->next = cur->next;
cur->next = req;
}
pthread_cond_signal(§ion_queue_cond[doc_idx][section_idx]);
pthread_mutex_unlock(§ion_queue_mutex[doc_idx][section_idx]);
pthread_mutex_lock(§ion_mutex[doc_idx][section_idx]);
while (section_queue[doc_idx][section_idx]->client_sock != client_sock)
pthread_cond_wait(§ion_queue_cond[doc_idx][section_idx], §ion_mutex[doc_idx][section_idx]);
pthread_mutex_lock(&docs_mutex);
doc->section_line_count[section_idx] = 0;
for (int i = 0; i < line_count && i < MAX_LINES; ++i)
strncpy(doc->section_contents[section_idx][i], temp_lines[i], MAX_LINE - 1);
doc->section_line_count[section_idx] = line_count;
pthread_mutex_unlock(&docs_mutex);
section_queue[doc_idx][section_idx] = section_queue[doc_idx][section_idx]->next;
pthread_cond_broadcast(§ion_queue_cond[doc_idx][section_idx]);
pthread_mutex_unlock(§ion_mutex[doc_idx][section_idx]);
send_all(client_sock, "[Write_Completed]\n");
}
else if (strcmp(args[0], "read") == 0) {
pthread_mutex_lock(&docs_mutex);
if (argc == 1) {
for (int i = 0; i < doc_count; ++i) {
char line[BUF_SIZE];
snprintf(line, sizeof(line), "%s\n", docs[i].title);
send_all(client_sock, line);
for (int j = 0; j < docs[i].section_count; ++j) {
snprintf(line, sizeof(line), " %d. %s\n", j + 1, docs[i].section_titles[j]);
send_all(client_sock, line);
}
}
} else if (argc >= 3) {
Document* doc = find_doc(args[1]);
if (!doc) {
pthread_mutex_unlock(&docs_mutex);
send_all(client_sock, "[Error] Document not found.\n");
continue;
}
int found = 0;
for (int i = 0; i < doc->section_count; ++i)
if (strcmp(doc->section_titles[i], args[2]) == 0) {
found = 1;
char header[BUF_SIZE];
snprintf(header, sizeof(header), "%s\n %d. %s\n", doc->title, i + 1, doc->section_titles[i]);
send_all(client_sock, header);
for (int j = 0; j < doc->section_line_count[i]; ++j) {
char line[BUF_SIZE];
snprintf(line, sizeof(line), " %s\n", doc->section_contents[i][j]);
send_all(client_sock, line);
}
break;
}
if (!found)
send_all(client_sock, "[Error] Section not found.\n");
}
send_all(client_sock, "__END__\n");
pthread_mutex_unlock(&docs_mutex);
}
else if (strcmp(args[0], "bye") == 0) {
send_all(client_sock, "[Disconnected]\n");
break;
} else {
send_all(client_sock, "[Error] Unknown command.\n");
}
for (int i = 0; i < argc; ++i) free(args[i]);
}
close(client_sock);
return NULL;
}
0 XDK (+0)
유익한 글을 읽었다면 작성자에게 XDK를 선물하세요.
-
수학 인현강 차이 큰가요? 0 0
인강으로만 공부하고 있는데 유튜브에서 현강하시는 강사님들 강의 클립 보니까 뭔가...
-
상상 시즌3 2회 3 2
병신
-
쵸비 미드럼블 뭐냐 ㅅㅂ 1 0
살살 해줘
-
6평에 수완도 연계 돼요? 2 0
수능완성 나왔더라구요 연계 되나요?
-
덕코가..80만? MS가 2026인데 어쩌다가 글과 댓글로... 흐음
-
고기 정병이 너무 심하게 온다 1 2
고기를 저녁식사로 먹지 않으면 그냥 죽겠긔
-
근데 운동회무승부하는거 3 1
이걸 왜 사회문제로 끌고가려하는지 모르겠네 무승부하는거 짜치고 개노잼이라서...
-
뉴런등등 문제가 많다 3 1
문제는 내가 많고 인강 들을때마다 딴 생각하고 집중도 안되고 졸린데 인강이랑 안...
-
그런거 없음?
-
지금바로 2 2
운지
-
아무래도 초딩 때부터 나데나데받지 못한 것이 정신병의 근원일지도 3 4
인정욕규도비대해지고 자존감도낮고 호의를의심해버리고 나데나데를받을수없지만원하게된 병신이 됨
-
작수 국수영 만점 받고 탐구 88 받으면 어디감 7 1
11188
-
수능 64367이면 0 0
보통 대학 어디감
-
싱숑님을 차냥하라 5 1
반박시 밤길을 조심해야할거시다...
-
아마도내가태어난그때부터인거
-
나는잘하는거없다 나는이싱한거같다 나는왜친구가없지 ㅇㅈㄹ햇음
-
진지하게 여초딩이 되고 싶음 2 1
공부로부터 벗어나고 싶고 무엇이 될지 진지하게 고민할 필요도 없을 테고 조금만...
-
작년(26) 9모 언매 고수분들 38번 질문.. 3 0
이 문제에서 보조사는 어미나 부사 뒤에도 결합할 수 있다. 라고 돼있고 보기...
-
ㄹㅇ궁금함
-
어렵지는 않은데 항상 틀리는거 0 0
예약채권지문... 풀때마다 틀림
-
정석민 비문학 << 신인듯 0 0
독서는 자습하면 오른다는 천재들의 가스라이팅에 당해버렸었는데 강의 듣는게 훨훨나은듯
-
수능실전문제가(수특자작) 기출플러스에(69수능) 있는 문제보다 어려운데....
-
지금인생이꿈이엇으면좋겟음 1 1
-
맞팔 구해요 0 2
잡당 답니다
-
반수생 수학 커리 질문 0 0
미적분이고 작년 한해동안 수학 모의고사 3 유지하다가 수능에서 5로 떨어져 충격받아...
-
ㄹㅇ정신병없는사람은 죽고싶다는 생각을 잘 안함? 6 3
본인은 정신병 있어서 모름 일단 잘 때마다 잠들고 영원히일어나지않으면좋겟다는 생각은 디폴트값인데
-
문학에서 말리면 전 할 수 있는게 아무것도 없어요
-
파란버튼을안누를이유가없음 2 1
안 죽으면 아쉬운 거고 눌러서죽으면좋지뭐...
-
군수생 질문 2 0
지금 할꺼하고 교재 살라니까 좀 부족한데 군대에서 야뎁쓰면 눈치보임?
-
쌍윤 선택인데 작년에 윤사 내신으로 했고 아직 윤사 개념 시작도 안했는데 그냥...
-
그냥남들한테민폐만끼치는거같음 1 2
조금이라도마음을열게되먄 정병을무한발사해서 주변사람들을 힘들게함... 그래서 손절당한...
-
도?덕 테스트 5 0
오른쪽엔 파란버튼, 왼쪽엔 빨간버튼이 있습니다 전지구인이 모두 버튼을 한 번씩...
-
생2런 1일차 0 1
나의 이상형을 찾은 것만 같아. 암기조차도 재밌어 ㅎㅎ
-
수학 72점 "6등급"..? 6 0
1709 가형 1에서 5까지 1문제 틀릴때마다 등급 1개씩 내려감...
-
반수생 고민 0 0
현재 인하대 공대 다니고 있고(높공은 아님) 고3 5월에 시작해서 화 확 영 생윤...
-
그냥다음주에부엉이바위로가서운지가뛰고싶을뿐
-
필통 이쁘지? 1 1
-
07이상은 오르비 꺼라 2 3
쫌
-
수기사 오르비꺼라 0 0
ㅋㅋ
-
2호선 오르비꺼라 2 2
네
-
바둑판 제조 장인...jpg 2 0
-
여대가 남녀공학 된다고 시위까진 그럴 수 있는데 깨부수다가 돈? 난 못 내지~...
-
다들 안녕 4 1
-
6모 1 0
반수생 6모 탐구 신청한대로 그대로 쳐야하나요
-
나 25년 살면서 내가 수영 못한다는 걸 처음 알았음 3 1
사실 수영장을 한 번도 안 가봄.. 작년에 태국에서 처음 가봄 당연히 난 바다에서...
-
정시 5 1
화작84 확통94 영어1 생윤98 윤사98 이정도면 정시에서 최대 어디까지 갈 수 잇나요
-
본인 수1 합답형 3 0
길이 대소 구하는 문제였는데 샤프심으로 길이 직접 재서 맞춤 ㅁㅌㅊ?
-
풀어보세요 17 2
내신틱하지만 재밌어요
-
일단 작수 경제사문 본 사람은 2 0
뇌가 터져 죽었을 듯 경제 타임어택 지나고 사문 보니 높은 벽이 ㅋㅋ
-
확통 틀리는 거 어캄 3 1
슬픔


반가워요