                   BeeJ's Guide to Network Programming.

                  ͳ  Ȱ(v.1.5.4, 17-May-1998)

            http://www.ecst.csuchico.edu/~beej/guide/net  :
                 ڼȣ(tempter@fourthline.com),1998/8/20
  ------------------------------------------------------------------------



 α׷ Ƴ? ׳   ˾ƳⰡ  ?
 ־̴ ͳ α׷   bind() ȣϰ
connect() ȣϰ ̷  ü   ð ?

۽,   ܿ  سҰ, а   ϰ ͱ.
ٷ ãƿ̽ϴ.   ٷ  C α׷ӿ Ʈũ
α׷ õ  帱̴ϴ.

  ------------------------------------------------------------------------



  ȳ ۷ ƴմϴ. Ƹ  α׷ ó
ϸ鼭 𼭺 ؾ  𸣴 鿡  ɰ̴ϴ. 
 ǹ̿    α׷ֿ  Ϻ ȳ ƴ ̴ϴ.
  ǹ̸     ݾ ϰ Ǳ⸸ ٶ
Դϴ.
  ------------------------------------------------------------------------

뵵

κ ڵ  PC GNU gcc ̿Ͽ  Ǿϴ. 
HPUX gcc ̿ؼ  ȴٴ ͵ Ȯ߽ϴ. ׷  
ڵ ׽Ʈ Ȱ ƴ϶  Ͻñ ٶϴ.

( Ī )

  ------------------------------------------------------------------------



   * ̶ ΰ.
   * ΰ  
   * Ʈũ ̷а  ˼ ͵
   * struct s--̰ 𸣸 ܰ  ıҰ~~
   *  ٲٱ
   * IPּ  Ȱ
   * socket()-- ڸ ƶ
   * bind()--  Ʈ Ǿ?
   * connect()-- ű!
   * listen()-- ȭ ɾ
   * accept()--Ʈ3490 ȭɾּż մϴ.
   * send() and recv()-- غ!
   * sendto() and recvfrom()--غ! ͱ׷
   * close() and shutdown()--!
   * getpeername()--ʴϱ?
   * gethostname()-- ΰ?
   * DNS--"whitehouse.gov", - "198.137.240.100"
   * Ŭ̾Ʈ  
   *  Ʈ
   *  ƮŬ̾Ʈ
   * ͱ׷ 
   * ŷ
   * select()--ȭ ߺ, ϱ!
   * 
   * ǻ  ó

  ------------------------------------------------------------------------

̶ ΰ.

̶ ܾ   ̴. ׸ Ƹ   Ȯ
ΰ Ͽ ñ ϱ⵵  ̴.   н 
ڸ ̿Ͽ ٸ α׷  ȯϴ  ǹѴ.

?

. Ƹ н ϴ  ̷ ϴ    
̴. "н  Ϸ Ǿֱ!"  ׵ ϴ 
 н α׷    ϴ  ڸ ؼ
ϰ ȴٴ ̴.  ڴ    ǹϴ 
̴. ׷   Ʈũ ɼ ְ FIFO, , ͹̳, 
ũ  ɼ      ȴٴ ̴. н
 ̴!   ͳ Ͽ ָ  ٸ
α׷  ȯϱ ؼ  ڸ ̿ϸ ȴٴ ̴.
~

"ȶ ,   ڴ ü  °ſ?" ° 
ӿ     ̴. ⿡  ִ. socket()
ȣϸ  ڸ  ǰ send(), recv() Ͽ õ Լ
ȣϿ  ȯ  ִ. (man send, man recv غ )

"!" ̷ Ǹ ϰ. "  ڰ  ڶ ü
 read(),write()  ȵǴ°ſ?" ª ϸ ´. ׷
send(),recv()    Ʈũ   ϱ⿡
 ȴٴ ̴.

 ?  ? DARPA ͳ ּ(ͳ ), θ
(н ), CCITT X.25 ּ(X.25 , ׳ ص )
ְ Ƹ   н     ϵ 
̴.   ù° (ͳ ) ϳ  ̴.

  ------------------------------------------------------------------------

ΰ  

ͳ Ͽ ΰ  ֳ? ׷. .. ̴.  ֱ
  ְ  ʱ  ̰ ΰ ̾߱ ϴ ̴. RAW
̶ ſ  ͵  ѹ δ ͵ .

ΰ  ΰ? ϳ Ʈ ̰ ٸ ϳ ͱ׷
̴. Ŀ SOCK_STREAM, SOCK_DGRAM Ī ̴. Ŀ׷
 񿬰 ̶ Ѵ. (  Ͽ Ѵٸ connect()
  ִ. connect() Ұ)

Ʈ   ŷڼְ  ִ ̴.  ΰ 
  Ͽ ٸ   Ȯ  ̴. 
ȴ.   ٸ  Ǽ̰ ŽǼ   ⼭
  ̴.

Ʈ   ̴°? Ƹ ڳ̶  ̴.
ô? װ   . Է  ڴ    Ǿ
ϴ ̴.  WWWƮ Ʈ 80 ڳ Ͽ "GET pagename"
 Էϸ HTML ȭ  츣  ̴.

 Ʈ   Ȯ  ǰ ߰ Ǵ°?  
TCP ̿ϱ ̴. (Transmission Control Protocol, RFC-793 ô
ڼϰ ִ.) Ƹ TCP ٴ TCP/IP    ̴.
պκ ٷ  TCP̰ ޺κ IP ͳ  ϴ
̴.

~ ͱ׷  ?  񿬰̶ ϴ? 뿡 
 ִ?  ŷڵ ?     
ͱ׷ ٸ Ȯ   ִ. Ǵ Ŷ 
ٲ   ִ. ׷  Ѵٸ    Ȯ
̴.

ͱ׷   ÿ IP ̿ TCP ̿ ʴ´.
 UDP(RFC-768) ̿Ѵ.

 ϴ°? Ʈ Ͽó ִ   ʿ䰡 
̴. ׳  Ŷ   õ IP ٿ
߼ϱ⸸ ϸ Ǵ ̴.  ʿ.  tftp bootp  Ǵ
̴.

! ׷  Ŷ   𸣴 ̷   
α׷ ?  α׷ UDP   å
߰ ִ ̴.   tftp 쿡 ϳ Ŷ  Ŀ
  ޾Ҵٴ  Ŷ ö ٸ ̴. 
ð(  5)     ϰ ٽ
, ٽ     Ŷ  ϰ Ǵ°̴. 
߹޾Ҵٴ (ACK reply)   SOCK_DGRAM   ſ
߿ϴ.

  ------------------------------------------------------------------------

Ʈũ ̷а Ʒ ˼ ͵

  ̾ ؼ  (UDP  å
¼)   Ʈũ  ۵ϴ  ˾ƺ  Ǿ
 SOCK_DGRAM  Ǵ  ˾ƺ ʿ䰡  Ͱ.  
 ׳ Ѿ ȴ.

                                 ~  Ÿ ĸȭ Ͽ
                                 ھ~  ̰ ſ ߿ϴ.
󸶳 ߿ϳĸ 츮 б Ʈũ ڽ Ϸ ݵ ˾ƾ
ϴ ̱ ̴. (..)  ̷.  Ŷ  
ù° (tftp ) ʿ Ӹ  ٴ´. ̷
ѹ ĸȭ  ٽ ι° (UDP) õ Ӹ 
ٽ ٰ ȴ.   IP,    ϵ 
μ ̴ ݷ ĸȭ Ǵ ̴.

ٸ ǻͿ  Ŷ ް Ǹ ϵ ̴  Ǯ Ŀο
IP UDP  Ǯ tftp α׷ tftp Ǯ Ͽ  
͸  Ǵ ̴.

  Ǹ  Ʈũ (Layered Network Model) 
 Ȱ .   ٸ 𵨵鿡 ؼ Ʈũ ý ϴ
鿡   ִ.    α׷ ϴ   
   (ø thin ethernet Ǵ
AUI) ޵Ǵ  Ͽ  Ű  ʰ ۾ 
  ִٴ ̴.  Ʈũ    α׷ӿԴ
  о̴.

̻  ʰ   Ϸ ִµ  Ʈũ ڽ 
   ܿ   ̴.

   * Application
   * Presentation
   * Session
   * Transport
   * Network
   * Data Link
   * Physical

 (Physical layer) ϵ(ø, ̴ݵ) ̴. ø̼
   ֵ   ݴ ̴.   Ͽ
ڴ Ʈũ ϰ Ǵ ̴.

   ڵ   ó     ֱ⿡
ʹ Ϲ ̴. н 츦    ⸦ 
ٸ,

   * Application Layer (telnet, ftp, etc.)
   * Host-to-Host Transport Layer (TCP, UDP)
   * Internet Layer (IP and routing)
   * Network Access Layer (was Network, Data Link, and Physical)

̷   ٸ Ʊ  ĸȭ    ϴ
    ̴.

̷  ۾ ϳ  Ŷ µ Ǵ ̴.  
  Ŷ Ӹκп  Ÿ  ־ Ѵٴ . (
̴.) Ʈ   ͸   ؾ   
send() ȣϴ  ̴.  ׷ 쿡 ϴ 
͸ ѹ ĸȭϰ (tftpĵ) sendto()  Ǵ
̴.Ŀ ۰ ͳ  õ ĸȭ ϰ 
ϵ Ѵ. ~ ÷ !!

̰  Ʈũ ̷ ̴. , ÿ ؼ ϰ ʹ
 ϳ ߴ. , ϳ .  ÿ ؼ ϳ
  ̴. Ͱ IP ܳ  ̺ Ͽ
¼ ¼...  ⿡  ִٸ IP RFC  ̸
 ű⿡ ؼ ϳ  Ѵٸ!   .

  ------------------------------------------------------------------------

struct S

ᱹ  Ա.  α׷ֿ  ⸦  ̴. 
  ϳ ϱ   ̽ ̴ 
 ŸԿ  ⸦  ̴.

 .  ̴.   

        int

̴. ׳  int̴. ()

  ̻ϴ ׳  б ٶ. ̰ ˾ƾ Ѵ.  
Ʈ ִµ  Ʈ տ ְų Ǵ  Ʈ տ ְ
ȴ.  찡 Ʈũ Ʈ ̴.  ȣƮ 
Ʈũ Ʈ   ϴ 쵵  ȱ׷ 찡 .
 NBO ޵  ִٸ Լ ̿Ͽ (htons()Լ) ȣƮ
Ʈ  ٲپ Ѵ.  ׷  ٸ ׳  ֵ ȴ.

ù° ü, struct sockaddr.  ü    ּҸ
Եȴ.

    struct sockaddr {
        unsigned short    sa_family;    /* address family, AF_xxx       */
        char              sa_data[14];  /* 14 bytes of protocol address */
    };

sa_family     ִµ,   ߿ "AF_INET"
츸 ٷ ȴ. sa_data   ּҿ Ʈȣ  ȴ. ణ
ǿ̱.

sockaddr ü ٷ ؼ   parallel structure 
Ѵ. ("in" ͳ ǹѴ.)

    struct sockaddr_in {
        short int          sin_family;  /* Address family               */
        unsigned short int sin_port;    /* Port number                  */
        struct in_addr     sin_addr;    /* Internet address             */
        unsigned char      sin_zero[8]; /* Same size as struct sockaddr */
    };

 ü   ϱⰡ    .  
sin_zero迭 sockaddr  ü ũ⸦ ߱ ؼ ־ ̹Ƿ
bzero() memset()Լ ̿Ͽ  0 ä Ѵ.   ߿
ε,  ü sockaddr  ͸ ̿Ͽ   ְ  ݴ뵵
ϴٴ ̴.  socket()Լ struct sockaddr *  ϴ
struct sockaddr_in   ְ ٷ   ִ ̴. 
sin_family sa_family Ǵ ̸  "AF_INET" Ǿ ϸ
sin_port, sin_addr Ʈũ Ʈ  Ǿ ϴ  ߿ ̴.

׷!  struct in_addr sin_addr ü NBO   ִ°?  
Ƴ   Ͽ struct in_addr     䰡
ʿ Ͱ.

    /* Internet address (a structure for historical reasons) */
    struct in_addr {
        unsigned long s_addr;
    };

.. ̰ Ͽ "̾". ׷ ׷  . ÿϰ
!   "ina" struct sockaddr_in  Ҵٸ
ina.sin_addr.s_addr  NBO  4Ʈ ͳ 巹 Ȯϰ 
  ̴.  ϴ ý struct in_addr   Ͽ
 ϰ ִ #defines S п  ѰͰ  Ȯϰ
   ̴.

  ------------------------------------------------------------------------

 ٲٱ

   Դ. Ʈũ ȣƮ Ʈ  ؼ  ʹ
Ұ     .

. ΰ  ȯ ִµ ϳ short(2 Ʈ) long(4Ʈ)
̴.  Լ unsigned  ۵ȴ.  short
ȣƮ Ʈ  Ʈũ Ʈ  ȯϴ 츦 .
ȣƮ h  ؼ to  ְ Ʈũ n    short s 
ִ´. ׷ htons()̴. (б ȣƮ  Ʈũ Ʈ̴.)

ʹ ?

 h,n,s,l   յ 밡ϴ. ( ʹ ٺ 
 ʰ..  stolh, Ʈ   ȣƮ?? ̷ .  
׿¾.) ִ ͵  .

   * htons()--"Host to Network Short"
   * htonl()--"Host to Network Long"
   * ntohs()--"Network to Host Short"
   * ntohl()--"Network to Host Long"

Ƹ    ˰Ե Ͱ   ̴. "char Ʈ
  ٲ?(:   Ƹ ǹ̾    
䵵  ̻       κ̴.)" Ǵ
",   68000  ̹ Ʈũ Ʈ  
ϴϱ ȯ ʿ  "     ̴. ׷ 
׷ ʴ. ׷ ۼ α׷ ٸ 迡 ۵Ųٸ 翬
 ߻ ̴.  н  ̱ ȣȯ ſ ߿
̴. ݵ Ʈũ ͸   Ʈũ Ʈ 
ٲ㼭 ٴ   .

 sin_addr, sin_port Ʈũ Ʈ  ϴµ 
sin_family  ׷°?  ϴ. sin_addr sin_port ĸȭǾ
Ʈũ ۵Ǿ ϴ  ̴.  翬 NBO Ѵ.
׷ sin_family ý ο Ŀο ؼ Ǵ ̸
Ʈũ ۵ ʴ ̹Ƿ ȣƮ Ʈ  ϵǾ ϴ
̴.

  ------------------------------------------------------------------------

IPּҴ ̸  ٷ°?

ེԵ IPּҸ  ִ  Լ   4Ʈ
long  ؼ << ڸ ̿ؼ ־ ϴ  
ʿ䰡 .

 struct sockaddr_IN ina ǵǾ ְ 132.241.5.10  IP ̸ּ 
  ־ Ѵٰ  . inet_addr()Լ ٷ ̷
ϴ ̴.  Լ ڿ   IPּҸ unsigned long
  ־ ش.   ϸ ȴ.

ina.sin_addr.s_addr = inet_addr("132.241.5.10")

inet_addr()  ̹ NBO  ָ  htonl()  
ʿ ٴ  ؾ Ѵ. !

׷  © ڵ ׷ ߽  ʴ´. ֳϸ
inet_addr()   -1 ְ Ǹ unsigned long -1
255.255.255.255 ǹѴ. ̴ ͳ εĳƮ 巹 ȴ. 
༮. ׻  ó Ȯ ϴ° .

.  IPּҸ long ִ° ˾Ҵµ  ݴ   ΰ?
࿡  ִ struct in_addr  ִµ ̸ ڿ 
ǥϷ?   inet_ntoa()  ȴ.(ntoa  Ʈũ 
ƽŰ̴.)

    printf("%s",inet_ntoa(ina.sin_addr));

 ڵ IPּҸ Ʈ  ̴ٰ.  Լ long  ƴ϶
struct in_addr   ޾ δٴ  ؾ Ѵ.   Լ
char   ͸   ִµ ̴ Լ static  
Ǹ  Ź Լ ȣ   Ͱ Ű  
ȭѴٴ ̴.   ,

    char *a1, *a2;
    .
    .
    a1 = inet_ntoa(ina1.sin_addr);  /* this is 198.92.129.1 */
    a2 = inet_ntoa(ina2.sin_addr);  /* this is 132.241.5.10 */
    printf("address 1: %s\n",a1);
    printf("address 2: %s\n",a2);

  ̷  ̴.

    address 1: 132.241.5.10
    address 2: 132.241.5.10

࿡   ؾ  ʿ䰡 ִٸ strcpy() ̿Ͽ  char
迭 ؾ  ̴.

    ߴ. ߿ "whitehouse.gov" ڿ شϴ
IPּҷ ٲٴ  ˷ ̴ٰ. (DNS )

  ------------------------------------------------------------------------

socket() ;  ڸ ƶ

ϸ  Ƽ socket() ý ȣ⿡ ؼ ؾ߸ ҰͰ.
̰  .

    #include <sys/types.h>
    #include <sys/socket.h>

    int socket(int domain, int type, int protocol);

׷    ? ù° domain  struct sockaddr_in ó
AF_INET  ϸ ȴ.  type  SOCK_STREAM̳ SOCK_DGRAM
ϸ ȴ.  protocol 0 ϸ ȴ. ( ʾ 
 domain   type  ִٴ  ϶. socket() 
ϰ  protocol  ؼ  ˷ getprotobyname() ϸ
ȴ.)

socket() ٷ ߿     ָ ÿ
-1 ְ ȴ.  errno  ϵȴ. (perror()
 Ұ.)

  ------------------------------------------------------------------------

bind() ;   Ʈ Ǿ?

ϴ   Ǹ    ý Ʈ  ־ Ѵ.
( ۾  listen()Լ ̿ؼ ܺ    Ǹ
Ϲ ӵ Ʈ telnet *.*.*.* 6969  ϶ Ҷ 
۾ ߴٴ ǹ̴. ) ࿡  ٸ ȣƮ ϱ⸸ 
̶ ׳ connect() Ͽ Ḹ ϸ ǰ  ۾ ʿ䰡
.

Ʒ bind() ý ȣ ̴.

    #include <sys/types.h>
    #include <sys/socket.h>

    int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

sockfd socket()Լ   ̸ my_addr IP ּҿ 
(, IP ּҿ Ʈȣ)  ִ struct sockaddr   
̰ addrlen  ü (sizeof(struct sockaddr))̴.

~~ ѹ濡 ޾Ƶ̱⿡  ׷.  .

    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>

    #define MYPORT 3490

    main()
    {
        int sockfd;
        struct sockaddr_in my_addr;

        sockfd = socket(AF_INET, SOCK_STREAM, 0); /* do some error checking! */

        my_addr.sin_family = AF_INET;     /* host byte order */
        my_addr.sin_port = htons(MYPORT); /* short, network byte order */
        my_addr.sin_addr.s_addr = inet_addr("132.241.5.10");
        bzero(&(my_addr.sin_zero), 8);    /* zero the rest of the struct */

        /* don't forget your error checking for bind(): */
        bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
        .
        .
        .

   my_addr.sin_port  my_addr.sin_addr.s_addr 
NBO̴.  ȭ  ý۸ ٸ  Ƿ  ý
  ؾ  ̴.

 bind() ؼ ּҳ Ʈ    ڵȭ 
 ִٴ  ؾ  Ͱ.

        my_addr.sin_port = 0; /* choose an unused port at random */
        my_addr.sin_addr.s_addr = INADDR_ANY;  /* use my IP address */

my_addr.sin_port 0 ϸ ڵ  ʰ ִ Ʈ ȣ
 ̸ٰ my_addr.sin_addr.s_addr INADDR_ANY   
۵ǰ ִ ڽ IPּҸ ڵ  ְ ȴ.

 ⼭ ణ Ǹ ￴ٸ INADDR_ANY   NBO ٲٴ
   ġç ̴. ƻ~~. ׷   ˰ .
 INADDR_ANY 0̴. 0  ٲپ 0ΰ̴. ׷
̷ 鿡 INADDR_ANY ׷ϱ 12 谡 Ѵٸ 
ڵ ۵ Ұ̴. ׷?  .  ׷ٸ,

        my_addr.sin_port = htons(0); /* choose an unused port at random */
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);  /* use my IP address */

 ϱ   ̽İ ڵ尡 Ǿ. ٸ ϰ  
۵ϴ  ƹ  ٴ ̴.

bind()   -1 ָ errno  ڵ尡  ȴ.

bind() ȣ   :  Ѽ Ʒ Ʈȣ  
̴. 1024 Ʒ ȣ  Ǿ ִ.  δ 65535 ϴ
   ִ. (ٸ α׷    쿡 ؼ..)

 ϳ   : bind()  ȣ ʾƵ Ǵ 찡 ִ. 
ٸ ȣƮ  (connect())ϰ ϴ 쿡 ڽ Ʈ (ڳ
ó) Ű  ʿ䰡 .  connect() ȣϱ⸸ ϸ ˾Ƽ
bind Ǿ ִ üũؼ ִ Ʈ bind ش.

  ------------------------------------------------------------------------

connect() ; ~ ű~

  ġ ڽ ڳ α׷ ó   . 
ڴ ϱ⸦ (TRONȭó.. (:   ȭ  ôµ..))
 ڸ  ߰   socket() ȣߴ. 
ڴ 132.241.5.10  Ʈ 23( ڳ Ʈȣ) ϶ Ѵ. ,
  ?

ེԵ (α׷) connect()( ϴ°) ɰϰ
а    ǸŰ  ģ о
̷δ~~

connet()   Ѵ.

    #include <sys/types.h>
    #include <sys/socket.h>

    int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);

sockfd  ģ  ̸ serv_addr ϰ ϴ
  ּҿ Ʈ    ִ struct sockaddr ̸
addrlen տ ̾߱ ѰͰ   ü ũ̴.

  ذ   ?   .

    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>

    #define DEST_IP   "132.241.5.10"
    #define DEST_PORT 23

    main()
    {
        int sockfd;
        struct sockaddr_in dest_addr;   /* will hold the destination addr */

        sockfd = socket(AF_INET, SOCK_STREAM, 0); /* do some error checking! */

        dest_addr.sin_family = AF_INET;        /* host byte order */
        dest_addr.sin_port = htons(DEST_PORT); /* short, network byte order */
        dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
        bzero(&(dest_addr.sin_zero), 8);       /* zero the rest of the struct */

        /* don't forget to error check the connect()! */
        connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
        .
        .
        .

ٽ ϰǵ connect()  ѹ üũ  Ѵ.   -1
ְ errno ϱ ̴.

 bind() ȣ  Ϳ ؾ Ѵ. ⺻ ⼭ ڽ
Ʈ ȣ    ̴.   ° ߿ϴ.
Ŀ ˾Ƽ  Ʈ   ̸ 츮 ϰ ϴ 
ڵ   ˰  ̴.

  ------------------------------------------------------------------------

listen() ;  ȭ ɾ~

  ٲپ, ࿡ а ϰ ϴ  ƴ϶
ܺηκ  ؼ     ε  ó
 ־ ϴ    ΰ.  ۾  ܰ ̷.
 listen() ؾ ǰ   accept() ؾ ȴٴ ̴.

listen()   ణ  ʿϴ.

    int listen(int sockfd, int backlog);

sockfd   ̸ backlog Ӵ ť ִ  
̴. װ   ΰ? ܺηκ    ť
accept() ȣ  ٷ Ѵٴ ̸ ڴ ٷ 󸶳 
  ť ׿  ִ° ϴ ̴. κ ý  ڸ
ϰ 20 ϰ   5 10 ̷ ϰ ȴ.

 ٽ listen()   -1 ָ errno Ѵ.

Ƹ   ֵ listen() ռ bind() ȣؾ ϸ ࿡
bind()   츮 ϰ  Ʈ ܺ  ٷ
Ѵ. (Ʈ 𸣰    ְڴ°? 쿢~~)  ܺ
 ٸ    ۾ Ǿ ϴ ̴.

    socket();
    bind();
    listen();
    /* accept() goes here */

 ͸ε ذ ϴٰ   ϰڴ. (accept()
  ڵ尡 غǾ ִ.)   sha-bang(:  ?)߿
 򰥸 κ accept() θ κ̴.

  ------------------------------------------------------------------------

accept() ; Ʈ 3490 ȭɾּż մϴ.

غ! accept() ȣϴ    ϱ ϴ.   °?
 ָ    connect() ȣϿ  listen()
ȣϰ ٸ Ʈ  õѴ. ׵  ٷ accept()
ȣǱ  ť ٷ  accept() ȣϿ   ϶
  ϰ ȴ. ׷  Լ     
ǰ   ڸ ְ ȴ. ڱ  ϳ ΰ
 ڸ  Ǵ ̴.     Ʈ 
listen()ϰ ִ.  ϳ Ӱ âǾ  send() recv() 
غ ǵ ϴ ̴.

  Դ! ~~

 Ʒ .

     #include <sys/socket.h>

     int accept(int sockfd, void *addr, int *addrlen);

sockfd listen()ϰ ִ  ̴.  .. addr 
struct sockaddr_in ̴. ⿡  ӿ   
ǰ ̸ ̿ؼ  ȣƮ  Ʈ ̿ؼ  Դ
  ְ ȴ. addrlen   ̸   struct
sockaddr_in ũⰡ ̸ Ǿ ־ Ѵ.  ں  
Ʈ   accept() ޾   ̸  ´ٸ
addrlen  ٿ  ̴.

accept()    쿡  Ѵٰ? -1 ְ errno 
Ѵ.

Ʊ ġ ѹ濡 ޾Ƶ̱⿡  ׷ϱ   о .

    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>

    #define MYPORT 3490    /* the port users will be connecting to */

    #define BACKLOG 10     /* how many pending connections queue will hold */

    main()
    {
        int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int sin_size;

        sockfd = socket(AF_INET, SOCK_STREAM, 0); /* do some error checking! */

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        /* don't forget your error checking for these calls: */
        bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));

        listen(sockfd, BACKLOG);

        sin_size = sizeof(struct sockaddr_in);
        new_fd = accept(sockfd, &their_addr, &sin_size);
        .
        .
        .

 new_fd ̿ؼ send() recv() ̿  ִٴ ̴. 
Ѵٸ ̻  ޾Ƶ ʰ ϳ Ḹ ̿ϱ ؼ
close() ̿Ͽ  sockfd    ִ.

  ------------------------------------------------------------------------

send(), recv() ; غ~

  Լ Ʈ ̳  ͱ׷   ְ
 ϴ ͵̴.   񿬰 ͱ׷  Ѵٸ
sendto() recvfrom() ϵ Ѵ.

send() ȣ  Ʒ .

    int send(int sockfd, const void *msg, int len, int flags);

sockfd socket() ؼ ų accept() ؼ  , ͸
  ̸, msg  ͸ Ű , len 
 Ʈ  ̸ flags  ׳ 0 ؾ Ѵ. (÷׿  
ڼ  send()   Ұ.)

ణ   .

    char *msg = "Beej was here!";
    int len, bytes_sent;
    .
    .
    len = strlen(msg);
    bytes_sent = send(sockfd, msg, len, 0);
    .
    .
    .

send()    Ʈ  ִµ ̰  
ں   ִ.   ϴ  ũⰡ ó 
   쵵    send() ڱⰡ   ִ
ڸŭ   ߶     ٽ  
 ϴ ̴. ࿡    ũ⺸  ڰ
  Դٸ   ͸    
å ̴. ׳ ҽ   ۴ٸ (1k ̳)
Ƹ ѹ     ̴.    -1 ָ
errno Ѵ.

recv() 쵵  ϴ.

    int recv(int sockfd, void *buf, int len, unsigned int flags);

sockfd о  ̸ buf   ̴. len 
ִ ṵ̃ flags 0 ؾ Ѵ. (ڼ flags  recv() 
 Ұ.)

recv()  о Ʈ ڸ ָ   -1, errno
Ѵ.

? ..   Ʈ  ̿ؼ ͸  
 ְ Ǿ. ~ н Ʈũ α׷ӳ~~

  ------------------------------------------------------------------------

sendto(), recvfrom() ; غ~  ׷ 

, ̶ ϰ ִٷ ϰڴ. ׷ ͱ׷ 
 ? ~ ƹ̰~(: ͹̳2 ±~~)
  ̴.

ͱ׷    ʿ䰡 ٸ ͸   ־ 
   ־ ϴ°? ´.  ּҸ ˷־ Ѵ.
⿡  ִ.

    int sendto(int sockfd, const void *msg, int len, unsigned int flags,
               const struct sockaddr *to, int tolen);

ٽ  Լ ΰ ΰ    ̿ܿ ⺻
send() ϴ. to  struct sockaddr ̸(Ƹ struct
sockaddr_in) ⿡  ּҿ Ʈȣ  ־  ̴.
tolen  ü ũ ̴.

send()  sendto()   Ʈ 
ָ(    ũ⺸  𸣴),  
-1 ش.

ϰ recvfrom() Ʒ .

    int recvfrom(int sockfd, void *buf, int len, unsigned int flags
                 struct sockaddr *from, int *fromlen);

 ̰͵ ΰ   ־ ȴ. from ͸  
ּҿ Ʈ  ִ struct sockaddr ̸ fromlen  μ
ü ũⰡ õǾ ־ Ѵ. Լ ȣ ڿ fromlen 
from ũⰡ ϵǰ ȴ.

recvfrom()    Ʈ ָ   -1,
errno ϰ ȴ.

 connect() ̿Ͽ ͱ׷    Ȳ̶ 
send(), recv()  ص    ̽ ڵ 
ҽ   Լ ߰ؼ ۵ǰ  ̴.

  ------------------------------------------------------------------------

close(), shutdown() ; .

~~ Ϸ ͸  ޾Ҵ..     ̴.
̰ .   ڿ  close() ϸ Ǵ ̴.

    close(sockfd);

̰ ̻  Ұ     Ͽ а
 ϴ ڴ  ް  ̴.

ణ    ؼ shutdown() ϸ ȴ. ̰ ̿ϸ
Ư Ÿ   ְ ȴ.

    int shutdown(int sockfd, int how);

sockfd  ̸ how  .

   * 0 - ̻  
   * 1 - ̻ ۽ 
   * 2 - ̻ ۼ (close()  )

shutdown()    -1 ָ errno Ѵ.

ȲϿɰԵ ᵵ  ͱ׷ Ͽ shutdown() Ѵٸ 
send(), recv()  ϰԸ  ̴. connect()  쿡
̷   ִٴ  ؾ Ѵ. (: ׷ٸ sendto,
recvfrom  ȴٴ ΰ??׽Ʈ ʿҵ.)

ϰ͵ ƴϱ.

  ------------------------------------------------------------------------

getpeername() ; ʴϱ?

 Լ ǰ .

ʹ     ʿ䰡 ʳ   ִ° ϱ..

getpeername()   Ʈ Ͽ  Ǿ ִ° ˷ش.

    #include <sys/socket.h>

    int getpeername(int sockfd, struct sockaddr *addr, int *addrlen);

sockfd  Ʈ  ̸ addr    
struct sockaddr(Ǵ struct sockaddr_in)  ̸ addrlen 
Ű ͷμ ü ũⰡ Ǿ ־ Ѵ.

  -1 ְ errno Ѵ. (ܿڱ.)

ϴ ּҸ ˰ԵǸ inet_ntoa() gethostbyaddr() ̿Ͽ  
 ˾Ƴ  ְ   login name ˰ԵǴ  ƴϴ.
(  ident   ִٸ ˾Ƴ    ƴ 
     ̹Ƿ RFC-1413 ϶ ϰ
ʹ.)

  ------------------------------------------------------------------------

gethostname() ;  ΰ?

getpeername()     Լ̴.  α׷  ִ
ǻ ̸ ˷ش. ̸ gethostbyname() ̿Ͽ  
IPּҸ ˾Ƴµ   ִ.

  ִ°?     ִµ    
̴(:  ..ǰ ñϳ..). ·ų,

    #include <unistd.h>

    int gethostname(char *hostname, size_t size);

hostname ڿ ̸ Լ ִ    ̴.
size  ڿ ũ̴.

̸ 0,   -1 ϰ errno Ѵ.

  ------------------------------------------------------------------------

DNS ; whitehouse.gov - 198.137.240.100

𸣴  Ͽ DNS Domain Name Service     ϰڴ.
ϰ Ѵٸ DNSٰ   ִ ּҸ ָ DNS
bind,connect,sendto,·ų IPּҰ ʿ ͵鿡   ִ
IPּҸ ش.   ̷ Էߴٸ

    $ telnet whitehouse.gov

telnet  connect() ϱ ؼ 198.137.240.100̶ IPּҸ
ãƳ ȴ. ׷  ׷ ϴ ΰ? gethostbyname() ϸ
ȴ.

    #include <netdb.h>

    struct hostent *gethostbyname(const char *name);

ٽ  struct hostent Ͱ ƿ´.   Ʒ .

    struct hostent {
        char    *h_name;
        char    **h_aliases;
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list;
    };
    #define h_addr h_addr_list[0]

 ʵ忡    .

   * h_name - ȣƮ  ̸
   * h_aliases - ȣƮ μ NULL  ȴ.
   * h_addrtype - ּ ,  AF_INET
   * h_length - ּ Ʈ 
   * h_addr_list - 0  Ʈũ ּҵ, NBO Ǿ ִ.
   * h_addr - h_addr_list ù° ּ

gethostbyname()  ü ͸ ְ Ǹ   NULL
ش. errno õ ʰ h_errno  ȴ. (Ʒ
herror())

׷ ̰  ϴ°?  ǻ Ŵ ó ھտ 
 ׾Ƴ ͸δ  ̴.  Լ  ⺸ٴ Ⱑ
 ̴.

 .

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>

    int main(int argc, char *argv[])
    {
        struct hostent *h;

        if (argc != 2) {  /* error check the command line */
            fprintf(stderr,"usage: getip address\n");
            exit(1);
        }

        if ((h=gethostbyname(argv[1])) == NULL) {  /* get the host info */
            herror("gethostbyname");
            exit(1);
        }

        printf("Host name  : %s\n", h->h_name);
        printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));

        return 0;
    }

gethostbyname() errno õ ʴ  perror()  
 herror() ؾ Ѵ.

 ȣƮ ̸  ִ Ʈ gethostbyname()Լ ־
ν ٷ struct hostent   Ǵ ̴.

ִ Ѱ      ּҸ ڿ 
 ΰ ϴ ̴. h->h_addr   ( char *) ε
inet_ntoa() μ struct in_addr  ϱ ̴.  h->h_addr
 struct in_addr *  ȯ ϰ    ٽ  ϸ
ȴٴ ̴.

  ------------------------------------------------------------------------

Ŭ̾Ʈ- 

 Ŭ̾Ʈ- ġ ~~ Ʈũ    
μ ûϴ Ŭ̾Ʈ μμ ٷ. ڳ ̿Ͽ
23 Ʈ ϴ (Ŭ̾Ʈ) α׷(telnetd) ۵Ű
Ǵ ̸   α׷   ȣ ޾Ƶ鿩  ڳ
 Ͽ α Ʈ ְ Ǵ ̴. ..

       [Client-Server Relationship]  ׸2. Ŭ̾Ʈ- 

Ŭ̾Ʈ   ȯ  ׸  ִ.

ָ Ŭ̾Ʈ  SOCK_STREAM̵, SOCK_DGRAM̵
 θ ȴٸ ǻ ȴٴ ̴.   telnet-telnetd,
ftp-ftpd, Ǵ bootp-bootpd ̴. ftp ٸ ݵ  ftpd
 ִٴ ̴.

 ȣƮ ϳ  α׷  ְ ȴ. ׸  
fork() ̿Ͽ  Ŭ̾Ʈ ް Ǵ ̴. ⺻ ƾ
  .   ϴٰ accept() ȣϰ Ǹ  
fork() ̿Ͽ ڽ μ    óϰ ȴ.
̰ ٷ  Ұ   α׷ ̴.

  ------------------------------------------------------------------------

 Ʈ 

  ϴ   Ʈ  ϰ Ǵ  Ŭ̾Ʈ
"Hello, World!\n"  ִ ̴.   ׽Ʈϱ ؼ
ϳ 쿡     ٸ 쿡 ڳ 
õ  ̴.

    $ telnet remotehostname 3490

hostname   α׷ ۵ ȣƮ ̸̴.

 α׷ ڵ

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/wait.h>

    #define MYPORT 3490    /* the port users will be connecting to */

    #define BACKLOG 10     /* how many pending connections queue will hold */

    main()
    {
        int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int sin_size;

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
                                                                      == -1) {
            perror("bind");
            exit(1);
        }

        if (listen(sockfd, BACKLOG) == -1) {
            perror("listen");
            exit(1);
        }

        while(1) {  /* main accept() loop */
            sin_size = sizeof(struct sockaddr_in);
            if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
                                                          &sin_size)) == -1) {
                perror("accept");
                continue;
            }
            printf("server: got connection from %s\n", \
                                               inet_ntoa(their_addr.sin_addr));
            if (!fork()) { /* this is the child process */
                if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                    perror("send");
                close(new_fd);
                exit(0);
            }
            close(new_fd);  /* parent doesn't need this */

            while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
        }
    }

 ڵ  ܼ Ͽ ϳ Ŀٶ( ) main() 
  ִ. ࿡ ̰ ߰ ߶   Լ 
ϴ° ٰ ȴٸ ׷ ȴ.

 Ŭ̾Ʈ ڵ带 ̿Ѵٸ  κ ڿ ޾ 
ִ.

  ------------------------------------------------------------------------

 Ʈ Ŭ̾Ʈ

̳༮    ڵ̴.  α׷ ϴ  ࿡
 ּҿ 3490 Ʈ Ͽ   ڿ ޴  ̴.

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>

    #define PORT 3490    /* the port client will be connecting to */

    #define MAXDATASIZE 100 /* max number of bytes we can get at once */

    int main(int argc, char *argv[])
    {
        int sockfd, numbytes;
        char buf[MAXDATASIZE];
        struct hostent *he;
        struct sockaddr_in their_addr; /* connector's address information */

        if (argc != 2) {
            fprintf(stderr,"usage: client hostname\n");
            exit(1);
        }

        if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
            herror("gethostbyname");
            exit(1);
        }

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        their_addr.sin_family = AF_INET;      /* host byte order */
        their_addr.sin_port = htons(PORT);    /* short, network byte order */
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        bzero(&(their_addr.sin_zero), 8);     /* zero the rest of the struct */

        if (connect(sockfd, (struct sockaddr *)&their_addr, \
                                              sizeof(struct sockaddr)) == -1) {
            perror("connect");
            exit(1);
        }

        if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
            perror("recv");
            exit(1);
        }

        buf[numbytes] = '\0';

        printf("Received: %s",buf);

        close(sockfd);

        return 0;
    }

 Ŭ̾Ʈ ۵ϱ⿡ ռ  ۵ѳ ʾҴٸ
connect()Լ "Connection refused" ְ ɰ̴. ϱ!

  ------------------------------------------------------------------------

ͱ׷ 

̿ ؼ ״    ʴ.  ׳ ΰ α׷
 ְڴ.

listener ȣƮ ɾƼ 4950Ʈ   Ŷ ٸ.
talker  ȣƮ  Ʈ   ڰ Է ͸
.

listener.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/wait.h>

    #define MYPORT 4950    /* the port users will be connecting to */

    #define MAXBUFLEN 100

    main()
    {
        int sockfd;
        struct sockaddr_in my_addr;    /* my address information */
        struct sockaddr_in their_addr; /* connector's address information */
        int addr_len, numbytes;
        char buf[MAXBUFLEN];

        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        my_addr.sin_family = AF_INET;         /* host byte order */
        my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
        my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
                                                                       == -1) {
            perror("bind");
            exit(1);
        }

        addr_len = sizeof(struct sockaddr);
        if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, \
                           (struct sockaddr *)&their_addr, &addr_len)) == -1) {
            perror("recvfrom");
            exit(1);
        }

        printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
        printf("packet is %d bytes long\n",numbytes);
        buf[numbytes] = '\0';
        printf("packet contains \"%s\"\n",buf);

        close(sockfd);
    }

ᱹ socket() ȣ  SOCK_DGRAM ϰ Ȱ ϰ,
listen()̳ accept()  ͵   Ѵ.  ڵ尡 ٷ
񿬰 ͱ׷  ڶ 뿹 ̴.

talker.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <sys/wait.h>

    #define MYPORT 4950    /* the port users will be connecting to */

    int main(int argc, char *argv[])
    {
        int sockfd;
        struct sockaddr_in their_addr; /* connector's address information */
        struct hostent *he;
        int numbytes;

        if (argc != 3) {
            fprintf(stderr,"usage: talker hostname message\n");
            exit(1);
        }

        if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
            herror("gethostbyname");
            exit(1);
        }

        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        their_addr.sin_family = AF_INET;      /* host byte order */
        their_addr.sin_port = htons(MYPORT);  /* short, network byte order */
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        bzero(&(their_addr.sin_zero), 8);     /* zero the rest of the struct */

        if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \
             (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
            perror("sendto");
            exit(1);
        }

        printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));

        close(sockfd);

        return 0;
    }

̰ ٴ. listener  ȣƮ  Ű ٸ  talker
Ų. ٰô뿡 ︮   ɼ...

տ ߾ Ѱ    ؾ Ұ . 
talker connect() ȣؼ  ߴٸ  ʹ sendto(),
recvfrom() ƴ϶ ׳ send().recv() ص ȴٴ ̴. ޵Ǿ
ϴ ȣƮ ּҴ connect()  ּҰ ǰ ȴ.

  ------------------------------------------------------------------------

ŷ

ŷ. Ƹ ð. ׷ ü װ ?  ""
 Ұ ̴. Ƹ listener Ű鼭 ġ ë 
α׷  ɾƼ  Ŷ ö ٸ ̴. ڸ鼭..
recvfrom() ȣߴµ Ͱ   ٸ? ٷ  Ͱ
  ŷ Ǵ ̴(׳ ű⼭ ڰ ִ ̴.).

 Լ ŷ ȴ. accept()  ȴ. recv*() 
 ȴ. ׵ ̷   ִ  ׷   ֵ 
޾ұ ̴. ó socket()   Ŀ 
ϵ  ߱ ̴.  Ҽ  Ϸ fcntl()
Ѵ.

    #include <unistd.h>
    #include <fcntl.h>
    .
    .
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    fcntl(sockfd, F_SETFL, O_NONBLOCK);
    .
    .

 Ҽ  ν  ϴ  ȿ
socket ̿  ִ.  Ͱ   Ͽ ͸
 õѴٸ -1  ְ errno EWOULDBLOCK  ϰ
ȴ.

Ϲδ ̷   ̾      ƴϴ.
  ͸ ϱ Ͽ ̷  ٻڰ ͸ ã
ƾ ٸ ̴ CPU ð Ҹϰ Ǵ ̴. ̴.  
   select() Ͽ ͸ ٸ ̴.

  ------------------------------------------------------------------------

select() ; ȭ ߺ . ϱ!

̰   ̻ Լ̴. ׷  ϹǷ  о ٶ.
 Ȳ  .     ̹  Ͽ
Ͱ   ٸ ִٰ .

, ׳ accept()ϰ recv() ٵ..  , ģ.
 accept() ŷ ȴٸ? ÿ  recv()  ΰ? ŷ
ϰ Ѵٰ? CPUð  ϱ. ׷ ?

̻    ְڴ.

       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int select(int numfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);

 Լ ȭ  "", Ư readfds,writefds,exceptfds
Ѵ.  Ϲ Է̳  ڷκ о ϼ ִ°
ȮϷ  ȭ  0 sockfd readfds ֱ⸸ ϸ ȴ.
numfds    ڿٰ 1 ؼ ؾ ϸ ̹
  Է 0 Ȯ ũ ϱ ؼ sockfd+1  ؾ
Ѵ.

select()  ö readfs    ߿   б
Ѱ ݿ  ֵ Ǹ FD_ISSET() ũθ ̿Ͽ üũ 
ִ.

ʹ ָ    ""  ϴ  ؼ ⸦ ؾ
Ұ .  "" fd_set̸  ũε ̸  
ִ.

   * FD_ZERO(fd_set *set) - ϱ  ҰѴ.
   * FD_SET(int fd, fd_set *set) - fd  set ش.
   * FD_CLR(int fd, fd_set *set) - fd  set ش.
   * FD_ISSET(int fd, fd_set *set) - fd setȿ ִ ȮѴ.

   struct timeval  ΰ? Ƹ  
͸    ٸ⸦ ġ  ̴. Ưð
ƹϵ   " ..."̶ ޽ ͹̳ο
½Ű  ̴.  ü  ð ϱ ؼ
Ǵ ̴.  ð ʰǰ   select() ƹ ȭ
    ְ  ۾  Ҽ ֵ ش.

struct timeval   .

    struct timeval {
        int tv_sec;     /* seconds */
        int tv_usec;    /* microseconds */
    };

ٸ ð ʸ Ϸ ׳ tv_sec ϸ ȴ. tv_usec
ũ ʸ Ѵ. иʰ ƴϰ ũ̴. ũʴ
鸸 ̴. ׷  usecΰ? u ׸  Mu Ұ ̴
ũθ ǹϴµ ȴ. Լ  timeout  ð
ϵɼ    н ٸ ϴ.

Ϳ~ ũ   Ÿ̸Ӹ  Ǿ!  timeval ʵ
0 ä select()   ָ  set  
˷ְ ȴ. timeout NULL ϸ   ʰ  
ڰ غǴ  ٸ Ǹ  Ư set ȭ 
ٸ  ׸ NULL ϸ ȴ.

  Է¿  Ÿ 2.5ʸ ٸ ڵ̴.

       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       #define STDIN 0  /* file descriptor for standard input */

       main()
       {
           struct timeval tv;
           fd_set readfds;

           tv.tv_sec = 2;
           tv.tv_usec = 500000;

           FD_ZERO(&readfds);
           FD_SET(STDIN, &readfds);

           /* don't care about writefds and exceptfds: */
           select(STDIN+1, &readfds, NULL, NULL, &tv);

           if (FD_ISSET(STDIN, &readfds))
               printf("A key was pressed!\n");
           else
               printf("Timed out.\n");
       }

 پ ۸ϴ ͹̶̳ Ű ġ ʴ ̻ ׳
ŸӾƿ ɸ̴.

 Ƹ  Ǹ  ͱ׷ Ͽ ͸ ٸ 
Ҽ   ̴. ´. ׷  ִ.  н
   ȵǴ ͵ ִ. ϰ ϴ 뿡 ؼ Ƹ
 ؾ  ̴.

select()    listen()   ִٸ  
̿Ͽ  ڸ readfds ÷ϴ  ο  ־°
Ȯ  ִٴ ̴.

̰ select()  ª 俴.



 ͼ Ƹ  ο ٸ  ° Ұ̴.   ٸ
𰡸  ã ִ° ˰ Ұ̴.

ʺڶ   ϴ ͵ .

   * socket()
   * bind()
   * connect()
   * listen()
   * accept()
   * send()
   * recv()
   * sendto()
   * recvfrom()
   * close()
   * shutdown()
   * getpeername()
   * getsockname()
   * gethostbyname()
   * gethostbyaddr()
   * getprotobyname()
   * fcntl()
   * select()
   * perror()

 å鵵  ɰ̴. books:
     Internetworking with TCP/IP, volumes I-III
     by Douglas E. Comer and David L. Stevens.
     Published by Prentice Hall.
     Second edition ISBNs: 0-13-468505-9, 0-13-472242-6, 0-13-474222-2.
     There is a third edition of this set which covers IPv6 and IP over
     ATM.

     Using C on the UNIX System
     by David A. Curry.
     Published by O'Reilly & Associates, Inc.
     ISBN 0-937175-23-4.

     TCP/IP Network Administration
     by Craig Hunt.
     Published by O'Reilly & Associates, Inc.
     ISBN 0-937175-82-X.

     TCP/IP Illustrated, volumes 1-3
     by W. Richard Stevens and Gary R. Wright.
     Published by Addison Wesley.
     ISBNs: 0-201-63346-9, 0-201-63354-X, 0-201-63495-3.

     Unix Network Programming
     by W. Richard Stevens.
     Published by Prentice Hall.
     ISBN 0-13-949876-1.

󿡴   ͵ ̴.
     BSD Sockets: A Quick And Dirty Primer
     (http://www.cs.umn.edu/~bentlema/unix/--has other great Unix system
     programming info, too!)

     Client-Server Computing
     (http://pandonia.canberra.edu.au/ClientServer/socket.html)

     Intro to TCP/IP (gopher)
     (gopher://gopher-chem.ucdavis.edu/11/Index/Internet_aw/Intro_the_Internet/intro.to.ip/)

     Internet Protocol Frequently Asked Questions (France)
     (http://web.cnam.fr/Network/TCP-IP/)

     The Unix Socket FAQ (http://www.ibrado.com/sock-faq/)

..RFC  ϰڴ.
     RFC-768 -- The User Datagram Protocol (UDP)
     (ftp://nic.ddn.mil/rfc/rfc768.txt)

     RFC-791 -- The Internet Protocol (IP)
     (ftp://nic.ddn.mil/rfc/rfc791.txt)

     RFC-793 -- The Transmission Control Protocol (TCP)
     (ftp://nic.ddn.mil/rfc/rfc793.txt)

     RFC-854 -- The Telnet Protocol (ftp://nic.ddn.mil/rfc/rfc854.txt)

     RFC-951 -- The Bootstrap Protocol (BOOTP)
     (ftp://nic.ddn.mil/rfc/rfc951.txt)

     RFC-1350 -- The Trivial File Transfer Protocol (TFTP)
     (ftp://nic.ddn.mil/rfc/rfc1350.txt)

  ------------------------------------------------------------------------

ǻ  ó

̻ ̸ 󿡼 ũ Ʋ  ⸸ ٶ ̴. 
Ǽ ׻ ִ ̴.

 Ǽ ִٸ Ȯ  ־ 򰥸 Ϳ Ͽ 
ǻ  å  .    ̸ ǻ 
 ۵   ϼ ִ ̴.

׷  ׷  ̴.    ͵   
ð Ҹ߰  TCP/IPƮũ ƿƼ(  ڳݵ)
м ߾.    ƴ϶ ׳   ̴.

װ ׷  (Ȥ ı̶)  ִ 
beej@ecst.csuchico.edu   ֱ ٶ. Ͽ ĳ
  ڴ.

   ߴ° ñϴٸ,  ߴ. ~  ƴϰ 
 Ͽ õ    ٶ ׵鿡   
ø ̶ ߴ "ٷ װž~" ؼ . ƹ
ؼ    ٸ   ʴ´ٸ  ̶
Ѵ. WWW ٷ     ̴. ٸ  ̷ 
 ϴٸ ̷ ֱ ٶ.

. α׷̳ ¥. ;-)

  : 쿬   ߰ϰ Ǿ  ϰ  
ġ ־  Ȩ ÷ ҽϴ.  Ǽ ־ٸ
帮  ֽŴٸ  ڽϴ.  α׷ µ 
   Ǳ ٶϴ.

  ------------------------------------------------------------------------
Copyright (C) 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted
in any medium provided that its content is not altered, it is presented in
its entirety, and this copyright notice remains intact. Contact
beej@ecst.csuchico.edu for more information.   ۼϰ
ѱ   ڿ ϸ ڷμ Ǹ оֽ
е鲲 帮ڽϴ.  Ǽ   tempter@fourthline.com
 ֽø ǰڽϴ. մϴ.
