태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

초보 리눅서의 이야기

제가 이번 학기에 대학교에서 네트워크 프로그래밍 과목을 수강합니다.

UNIX NETWORK PROGRAMMING VOLUME 1(제3판) 상세보기
STEVENS 외 지음 | 교보문고 펴냄
UNIX NETWORK PROGRAMMING 제3판. 컴퓨터 통신을 전공하거나 현업에서 종사하고 있는 이들에게 도움이 된다. 특히 응용에서 직접 이용할 수 있는 많은 프로그램 예제들을 보여주고 있어 초보자에게는 문제와 해법을 이해하는 데, 전문가에게는 더욱 효율적인 해법을 모색하는 데 참고가 될 것이다.
   
Unix Network Programming이라는 교재로 공부를 하는데, 거기에 간단한 DAYTIME 서비스 프로그램이 예제로 나오더군요.

포트 번호 13번 DAYTIME 서비스는 현재 날짜와 시간을 알려주는 인터넷 프로토콜입니다.

다음은 교재에 나오는 예제를 바탕으로 만든 간단한 DAYTIME 서비스 클라이언트 프로그램입니다.

01: /* daytimetcpcli.c */
02:
03: #include <stdio.h>
04: #include <stdlib.h>
05: #include <string.h>
06: #include <unistd.h>
07: #include <errno.h>
08: #include <netinet/in.h>
09: #include <arpa/inet.h>
10:
11: #define MAXLINE 4096
12:
13: int
14: main(int argc, char** argv)
15: {
16:   int sockfd, n;
17:   char recvline[MAXLINE + 1];
18:   struct sockaddr_in servaddr;
19:
20:   if (argc != 2) {
21:     printf("usage : daytimetcpcli <IPaddress>\n");
22:     exit(EXIT_FAILURE);
23:   }
24:
25:   // socket()
26:   sockfd = socket(AF_INET, SOCK_STREAM, 0);
27:   if (sockfd < 0) {
28:     perror("socket error");
29:     exit(EXIT_FAILURE);
30:   }
31:
32:   bzero(&servaddr, sizeof(servaddr));
33:   servaddr.sin_family = AF_INET;
34:   servaddr.sin_port = htons(13); /* daytime server */
35:   if ( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0 ) {
36:     printf("inet_pton error for %s : %s\n", argv[1], strerror(errno));
37:     exit(EXIT_FAILURE);
38:   }
39:
40:   // connect()
41:   if ( connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
42:     perror("connect error");
43:     exit(EXIT_FAILURE);
44:   }
45:
46:   // read()
47:   while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
48:     recvline[n] = 0;            /* null teminate */
49:     if (fputs(recvline, stdout) == EOF) {
50:       perror("fputs error");
51:       exit(EXIT_FAILURE);
52:     }
53:   }
54:   if (n < 0) {
55:     perror("read error");
56:     exit(EXIT_FAILURE);
57:   }
58:
59:   // close()
60:   if (close(sockfd) < 0) {
61:       perror("close error");
62:       exit(EXIT_FAILURE);
63:   }
64:
65:   exit(EXIT_SUCCESS);
66: }

실행 결과
jeongsw@jeongsw-desktop:~/programming$ ./daytimetcpcli 64.90.182.55

54568 08-04-12 13:08:41 50 0 0 623.2 UTC(NIST) *
jeongsw@jeongsw-desktop:~/programming$

그리고 다음은 위의 클라이언트 프로그램과 같이 사용할 수 있는 서버 프로그램입니다.

01: /* daytimetcpsrv.c */
02:
03: #include <stdio.h>
04: #include <unistd.h>
05: #include <stdlib.h>
06: #include <string.h>
07: #include <time.h>
08: #include <netinet/in.h>
09: #include <arpa/inet.h>
10:
11: #define MAXLINE 4096
12: #define LISTENQ 1024
13:
14: int
15: main()
16: {
17:   int listenfd, connfd;
18:   socklen_t len;
19:   struct sockaddr_in servaddr, cliaddr;
20:   char buff[MAXLINE];
21:   time_t ticks;
22:
23:   // socket()
24:   listenfd = socket(AF_INET, SOCK_STREAM, 0);
25:   if (listenfd < 0) {
26:     perror("socket error");
27:     exit(EXIT_FAILURE);
28:   }
29:
30:   bzero(&servaddr, sizeof(servaddr));
31:   servaddr.sin_family = AF_INET;
32:   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
33:   servaddr.sin_port = htons(13); /* daytime server */
34:
35:   // bind()
36:   if ( bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
37:     perror("bind error");
38:     exit(EXIT_FAILURE);
39:   }
40:
41:   // listen()
42:   if ( listen(listenfd, LISTENQ) < 0 ) {
43:     perror("listen error");
44:     exit(EXIT_FAILURE);
45:   }
46:
47:   for ( ; ; ) {
48:     // accept()
49:     connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
50:     if (connfd < 0) {
51:       perror("accept error");
52:       exit(EXIT_FAILURE);
53:     }
54:
55:     if ( inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)) == NULL ) {
56:       perror("inet_ntop error");
57:       exit(EXIT_FAILURE);
58:     }
59:
60:     printf("connection from %s, port %d\n", buff, ntohs(cliaddr.sin_port));
61:
62:     ticks = time(NULL);
63:     snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
64:
65:     // write()
66:     if ( write(connfd, buff, strlen(buff)) < 0 ) {
67:       perror("write error");
68:       exit(EXIT_FAILURE);
69:     }
70:
71:     // close()
72:     if ( close(connfd) < 0 ) {
73:       perror("close error");
74:       exit(EXIT_FAILURE);
75:     }
76:   }
77: }

서버 프로그램의 경우, 13번 포트를 사용하기 때문에, 슈퍼 유저 권한이 필요할 겁니다.

크리에이티브 커먼즈 라이선스
Creative Commons License

'프로그래밍' 카테고리의 다른 글

me2DAY Shortcut  (6) 2008/05/10
JZip 0.9  (4) 2008/04/14
간단한 DAYTIME 서비스 프로그램  (2) 2008/04/12
프로그래밍의 도(道)  (8) 2008/04/05
source-highlight  (2) 2008/04/02
리틀 엔디언과 빅 엔디언  (2) 2008/03/31

TRACKBACK :: http://jeongsw.tistory.com/trackback/407

  1. BlogIcon 환상경  댓글주소  수정/삭제  댓글쓰기

    와 재미난거 하시네요 -0-
    저도 저 교제로 수업하기를 바랬것만 ㅠ.ㅠ
    저희 학교는 Winsock만 가르치고 있어서 너무 서글퍼요 T^T

    2008/04/12 23:47
    • BlogIcon 초보 리눅서  댓글주소  수정/삭제

      다행히 저는 유닉스 기반에서 네트워크 프로그래밍을 배우고 있습니다. :)
      저야 원래 리눅스 사용자이기 때문에 아무런 불편이 없지만, 다른 학생들은 유닉스 계정을 따로 얻어야하기 때문에 많이 불편할 것 같더군요.

      2008/04/13 01:26

1  ... 50 51 52 53 54 55 56 57 58  ... 440 
BLOG main image
초보 리눅서의 이야기
초보 리눅서의 프로그래밍 이야기
by 초보 리눅서

카테고리

분류 전체보기 (440)
프로그래밍 (29)
Emacs (16)
Linux (109)
프로그램 (209)
기타 (77)
Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이선스에 따라 이용하실 수 있습니다.
tistory!get rss Tistory Tistory 가입하기!