컴공 일기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년생#독학생 오르비의 주인이 될 기회 27 31
-
(서울대 합격 / 합격자인증)(스누라이프) 서울대 26학번을 찾습니다. 0 0
안녕하세요! 서울대 커뮤니티 스누라이프 새내기맞이단입니다!서울대 26학번 합격자...
-
동생 가르쳐주는데 나는 딱 보고 이렇게이렇게 해서 이거네 하고 바로 풀고 동생은 계속 딴 얘기하더라
-
현장에서 확축각 못 봐서 B 미지수 잡고 2의 거듭제곱+유리수 꼴로 풀었는데 맞음.
-
하이닉스는 자사주를 소각하지만 2 0
테슬라는 자사 주주를 소각한다
-
서강대 시반공 0 0
화작 확통 생윤사문 1컷 통백 영어4 생윤 사문 만점이면 고속기준 가능함
-
좋아하는 동네 얘기해주세요 5 0
한번 놀러가볼게요~~
-
다들 뭔가요
-
만약에 4등급이라고 하면 4 3 2 1 이런식으로 오르나요 아니면 4 2 1 또는...
-
여기만 봐도 맨날 ○○라인이네, 컷이 어쩌네 이런 얘기만 나오는데 걍 무지성으로...
-
2506 존나 어렵네 1 0
독서 다섯 개는 틀린듯 뭔 말이야 시발
-
빨리 발표하라고 개시발새끼들아 0 1
ㅗ
-
09 국어인강 도와주세요 0 0
현재 고1이고 한달동안 국어학원 다녔는데 너무 양치기만하고 제 스타일이랑 안맞는 것...
-
엄엄 3 0
준준
-
주식추천 3 0
이차전지 석유화학이 정배죠?
-
사1 과1 0 0
화학 사문 할건데 이과 대부분 다 뚫리죠??
-
님들 노크 개인정보 털렸다는데 3 0
노크 쓰면 안되는 건가요?ㅍ?
-
설사범 예비 1번인데 4 0
이거 진짜 연고경이나 상지한으로 안빠질까요...? 원서영역 개망했네
-
제가 오르비를 안 해서 매력이 떨어진걸까요.??
-
수특 문학 0 0
수능특강 문학 교과서 개념학습 부분도 연계 내용인가요?
-
수학을 잘 못합니다 ㅜ 현재 김성은 선생님 무불개와 시기백을 같아 풀고 있는데 따로...
-
짜피 독서 피지컬을 늘려야해서
-
지금보면 누가 승자지? ㅋㅋ
-
새내기대학 2 0
새터랑 4차랑 겹쳐서 3차로 무조건 가야하는데 이거 티켓팅처럼 경쟁 심한 편인가요?
-
즐피티 떳네 0 0
Jlpt 확인 ㄱㄱ헛
-
노트북 1대 300만원…새학기 앞둔 대학생·학부모 ‘진땀’ 2 0
“노트북이 이렇게나 비싸다니…” 29일 찾은 춘천의 한 전자기기 판매 매장....
-
한양대 인칼 0 0
예상 최종컷이 몇점정도 인가요 829.45인데 가능할까요
-
강남 지하철역에 버려진 뱀…알고보니 멸종위기종 ‘볼파이톤’ 4 0
이달 초 서울 강남구 지하철역 화장실에서 구조된 뱀이 멸종위기종으로 확인됐다. 이...
-
하이뉙스 하이눅스 2 1
하이닉스ㅇ
-
오늘은 1911 가능세계 1711 콰인 포퍼 2609 차폐 품
-
420은 넘었을까요...
-
맞팔구 4 1
잡담태그 다는사람만…
-
M7 기업들이랑 시총 위상 다 나란히 하게 될듯 몇년 안에 물론 꼬레아 디스카운트...
-
오늘 조발 안 해요 0 1
그런 마인드로 버텨야 진짜 조발 안 해도 타격 없음
-
민트테 달고싶다 10 0
예쁠거같아..
-
뱃지 3관왕 달성 1 0
ㅋㅋ
-
아 개졸리네 4 1
빨리 집들어가고싶다
-
다들 검고 보고 하신건가..?
-
정말 여러모로 독한 한 해네요 2 5
시간이 길게 느껴지네
-
가능성 있음? 일단 목표는 뱃지 얻는거임
-
그리고 보통 강의같은거 다 3월 내로 완강되나요??(문학 수특)
-
새벽에 마소 줍줍했는데 0 0
아직도 430불이네 뭐 난 422불어샀으니... 좀 더 기다려봐야지
-
다들 수능 몇번 보심 25 0
첫 수능 시작한 나이는?
-
연대야발표좀해라 1 0
쫌
-
연대 왜이래진짜 2 2
연대 너무 발표 안하니까 막 불안해져요 정병옴 ㄹㅇ로 현역 일반전형이면 그냥 수시...
-
X의제곱이 0이 아닌거랑 10 1
X가 0이 아닌게 서로 동치인가요?
-
학교가기싫어 0 1
싫다고오
-
ㄷㄷ
-
2월 12일 이후에 추합결과 알 수 잇는 대학 잇으면 어떡하지 걍 알아서 붙을지...

반가워요