컴공 일기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를 선물하세요.
-
#07년생#08년생#독학생 오르비의 주인이 될 기회 37 36
-
부산대 입결 근황 0 0
왜이렇게 된거야
-
이거 절도죄인가요? 0 0
지나가다가 움직이는 꽃이 이뻐서 대화좀 나누다가 혼자사는 제 집에 데려와서 같이...
-
이렇게 넌 날 애태우고 있잖아
-
추가모집 학교 0 0
지금 대학 어디가 리스트에 있는 학교들이 끝인건가요? 아니면 20일 업데이트 때 새...
-
오늘부터 공부... 2 1
그만 놀자 이제~
-
서로 거래해서 합의한건데 왜 한쪽만 피해자임? 버스정류장에 성매매는 성착취입니다 라고 써져있던데
-
기사 따도 취업이 어려워요 취업한다고 해도 이탈률 엄청 높아요
-
알고리즘 해야되나 0 0
영어 안정~ 사알짝 불안 2등급인데 SYNTAX 1.0 이랑 메가 N제, 1주일에...
-
흐앵ㅇ.. 6 0
ㅠㅠㅠㅠㅠ
-
고려대 26학번 합격자를 위한 고려대 클루x노크 오픈채팅방을 소개합니다....
-
과외알바를 생각하시는 분들을 위한 매뉴얼&팁입니다. 5천원 커피값에 미리 하나...
-
학벌통 극복한듯 6 4
원래 연고대에 ㅈㄴ미련있었는데 할 거 열심히하면서 지내다가 어제 재수해서 고대 간...
-
사람이 없네 3 0
다들 뭐하나
-
시립대
-
역시 출근 길에는 신나는 노래야 ♪₍₍◝(* ॑꒳ ॑* )◟⁾⁾♪
-
소설 내용일치 문제 0 0
실시간으로 푸시나요?
-
안녕하세요 현직 약사입니다 1 0
안녕하세요 오랜만에 방문합니다. 약대에도 관심이 많으실텐데, 아무래도 약대가...
-
뱃지 주세요 5일차 3 0
ㅠㅠ
-
늦은 경찰대학 합격 인증 14 0
운이 너무 좋았던 26학년도 입시였네요 열심히 하겠습니다!
-
특히 항공대 탐1이던데 반등했으려나?
-
웨 출근 5 1
에 ₍ᐢꙬᐢ₎
-
오르비 6 0
사랑해
-
피터린치가 만약 지금 국장했으면 포폴에 이마트 넣었음 9 0
"꼭 매출액 변화에만 관심을 기울여야 하는 것은 아니다. 당신이 아는 회사에...
-
여름 해외여행지 추천좀 13 1
ㄱㄱ 예산 고려 ㄴㄴ
-
어쩌면 3 1
나만 딱 봤을때 내세울게 없어 보여서 너무 집칙하는거 같아 학벌 같은거에,, 지금은...
-
님들아 피규어 2 0
케이스좀 사려고 고민중인데 막상 하나사려니까 바닥애놓으면 뽄새가안나고 4개사려니까...
-
어제 좀 유의미하게 우울햇는데 12 2
자고 일어나니까 싹 없어지네 일찍 자는 게 이렇게나 중요하구나
-
사람은 배워야한다 2 0
못배운 사람과 배운사람의 차이는 극대화 된다 점점 난 멍청이가 되지 않을거야...
-
아침 ㅇㅈ 5 5
불고기 파리타
-
(노베도 1~2등급 만들어내는) 검증된 대치동 수학강사의 밀착관리형 수학과외 2 0
1. 지역 : 비대면 및 강남/교대/대치 2. 과목 : 수학 및 전과목상담(무료로...
-
안되겠다 2 0
동갑인데도 말의 깊이감과 성숙하고 고급언어를 내뱉을 수 있도록 공부 좀 해야겠다...
-
물이 없는 곳에서 2 0
이정도의 수둔을
-
지금 생각해보니깐 그러네
-
솔직히 깊이감 있고 성숙한 말에 살짝 감동받았어..
-
제왜국민 에들 생각보다 지림 18 3
오르비에서 적폐라 욕먹지만 한국 출생=>주재원으로 외국 국제고=>한국 명문대 입학...
-
공항으로 가자 2 0
-
불장난 4 0
빰빰 빠바밤빰빰
-
숨이 막힐 것 같이 1 0
차가웠던 공기 속에~
-
호앵ㅇ.... 4 0
ㅠㅠㅠㅠㅠ
-
러셀 기숙 남학생관 0 0
수업 추천 점 부타드립니다 국어는 강민철로 갈껍니다
-
ㅜㅜ... 실제로 입던 옷과 신발을 제자리에 내버려둘 예정인 나:
-
연대에서 반수vs전과 4 1
정시러는 욕심이 끝이없네요. 현역때 중경외시 공대성적에서 재수 연고공까지 올렸는데...
-
내가 사라진다면~ 1 0
먼지처럼~ 너무 슬퍼하지는 마요...
-
영어 문장 구조, 구문 0 0
평소에 문장 구조는 그래도 잘 따는데 어려운 구문 같은 거 나오면 거기서부터 의미...
-
영어 모고뭐풀지 1 0
모고 뭐풀어야함?? 내가 등신같이 2026 6 9 11 각각 20번대랑 빈칸...
-
토플 리스닝에 이상한거 생김 15 6
올해 초에 개정되면서 토익 리스닝 파트5에 출제되는 존나 꼬인 화법 유형이 도입됩 아오
-
ㅈㄱㄴ
-
도태됐노 3 1
자연스레소멸해야겠다
-
하 윗집에서 개짖는 소리나네 2 1
개짖는 소리 좀 안 나게 해라!!!
-
교사경 <— 이거 어떰 7 0
평가원 기출 끝내고 풀꺼 찾고 있는데 바로 이해원 n제를 할지 한완기 교사경을...

반가워요