Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Baekjoon
- For Beginners
- The Balance of the World
- system programming
- 백준
- IT
- c
- 시프
- 시스템 프로그래밍
- 균형잡힌 세상
- 해바
- 2941
- BAKA
- process control
- file IO
- 전자책
- 10773
- Zero That Out
- File 조작
- 4949
- QA
- 1874
- 바샤
- 브런치
- c++
- Parenthesis
- 입력 버퍼
- Process Communication
- 5622
- LJESNJAK
Archives
- Today
- Total
해바
Lab #15 본문
Socket() 시스템 호출을 이용하여 간단한 메시지를 교환하는 client/server 프로그램을 작성하라 :
- Domain은 PF_UNIX, socket type은 SOCK_DGRAM, 프로토콜은 0을 이용
- 교환할 간단한 메시지는 "This is a message from the client"
- 서버 프로그램은 하단의 serv.c에 나와 있음
- Client 프로그램을 작성할 것
// serv.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#define NAME "socket"
#define SIZE 1024
main() {
int sock, length, fromlen;
struct sockaddr_un name, from;
char buf[SIZE] = "";
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
name.sun_family = PF_UNIX;
strcpy(name.sun_path, NAME);
bind(sock, (struct sockaddr*)&name, sizeof(struct sockaddr_un));
printf("socket -> %s\n", NAME);
recvfrom(sock, buf, SIZE, 0, (struct sockaddr *)&from, &fromlen);
printf("->%s\n", buf);
unlink(NAME);
close(sock);
}
// clnt.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#define NAME "socket"
#define SIZE 1024
main() {
int sock;
struct sockaddr_un name;
char buf[SIZE] = "";
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
name.sun_family = PF_UNIX;
strcpy(name.sun_path, NAME);
printf("socket -> %s\n", NAME);
fgets(buf, SIZE, stdin);
sendto(sock, (const char*)buf, SIZE, 0, (const struct sockaddr *)&name, sizeof(struct sockaddr_un));
unlink(NAME);
close(sock);
}
컴파일 :
%gcc serv.c -o serv
%gcc clnt.c -o clnt
실행 :
Terminal 1 : %./serv
Terminal 2 : %./clnt
client에서 입력 테스트 실행
정상 출력 및 종료 확인
Comments