Title: Unix Network Programming, 2nd Ed., W. Richard Stevens, Prentice Hall PTR, 1999.
1???? ?????
- Unix Network Programming, 2nd Ed., W. Richard
Stevens, Prentice Hall PTR, 1999. - ??? ? Unix Network Programming, Stevens ?, ???,
??? ?, ???, 1991. - ??????? ?????, ???, ???, ???????, 2000.
210.7 ??
- ????? ?? ??? ?? ??
- ????? ?? ?? ??
- ??? ?? ???? ??? ?????-?? ??(client-server model)?
?? -
- ??
- ? ??? ???? ??? ?? ???? ???
- ? ???? ?? ??? ??? ??
3?????? ??? ??
Transport
?? ?? ?? ??
4??? ??? ??
- ??? ??(connection oriented protocol
TCP(Transmission Control Protocol)
5???? ?? ??? ??
- Server(connectionless protocol UDP(User
Datagram Protocol)) -
-
-
-
Client -
- blocks until data
- received from a client
-
-
data(request) -
- process request
-
-
data(reply)
socket()
bind()
recvfrom()
socket()
bind()
sendto()
write()
read()
610.7 ??
710.7 ??
- ?? ??? ??
- ??? ??? ?? ??
- ???(domain)
- ??? ????? ??? ???? ??? ??
- ??(type)
- ?????? ?? ??? ??? ? ?? ??? ??? ??
- ????(protocol)
- ?? ??? ???? ??? ??? ??
- ??? ??? ????? ??? ???? ??? ??
- AF Address Family
- AF_UNIX
- ?????? ??? ??? ??? ???? ?
- AF_INET
- ?????? ??? ???? ?? ????? ??? ? ??
- AF_NS
- ?????? ??? XEROX ???? ???? ??? ? ??
810.7 ??
- ?? ?????? ???? ??? ?? ??
- SOCK_STREAM
- ?? ??? ??, ???, ??? ??? ??? ???? ?? ??? ???
- SOCK_DGRAM
- ??? ???, ???, ???? ?? ??? ???
- SOCK_SEQPACKET
- ????? ??, ???, ??? ??? ??? ???? ?? ?? ??
- SOCK_RAW
- ?? ???? ????? ?????? ?? ?? ??
910.7 ??
- ???? ?? ???? ?? ??? ????? ??
- Yes? ??? ??? ?? ??? ???,
- ? ??? ???? ?? ????.
??
1010.7 ??
- ?? ???? ??
- ?? ??? ??? ?? ??
- /usr/include/sys/types.h
- /usr/include/sys/socket.h ??
- ?? ???? ?? ???? ??
- ?? ???? P525
1110.7 ??
- ??
- ??? ??? ????, ??? ?????
- ??? ???? ?? ???? ????
- ??? ???? ?????? ??
1210.7 ??
- ??? ?? socket( )
- socket( )? ???? ?? ??
- ??
- ??? domain, type, protocol? ???? ?? ??? ??
- ?? ?? ??? ?? ??? ??? ??? ?? ??? (serverFd)? ??
- ?? ?? ??? ?? ??? (serverFd)? -1 ??
1310.7 ??
- ??? ?? ?? bind( )
- ??? ???? ?? ?? ???, bind( )? ???? ?? ??
- ??? 0? ??, ??? -1? ??
- ??
- address? ??? ?? ??? ??? ?? ??? fd? ???? ?? ???
???? - addressLen? ?? ???? ??? ??
- ??? ??? ??? ?? ?? ???? ??
1410.7 ??
- ???? ?? ??? ??
- AF_UNIX ???
- sockaddr_un ???? ??
- AF_INET ???
- sockaddr_in ???? ??
1510.7 ??
- ?? ? ?? listen( )
- ?? ?? ?? ??? ?? ?? ??
- ??, ?????? ?? ? ?? ?? ??? ????? ???, ? ??? ???
- ??
- ?) listen (serverFd, 5) /?? ?? ??? ?? ?? /
1610.7 ??
- ????? ?? accept( )
- ?????? ?? ?? ??
- ???? ????, ????? -1 ??
- accept( )? ?? ? ??? ??? ? ?? ??? ??
- ??
- address ??? ?????? ??, ?? ??? ??? ?????? ??
- addressLen address? ??? ???? ???? ??? ???? ??
1710.7 ??
- ?????
- ???? ?? ??? ????, ??? ??? ??? ??????? ??? ????
???? - ??? ???? ?????? ??
1810.7 ??
- ???? connect( )
- ??? ??? ??
- connect( ) ??? 0????, ?? ??? ?? ?? ???, ?? ??
??? ??? -1?? - ??
- address ????? ??? ???? ?? ????? ??? ??
- ?? ???, fd? ??? ??? ???? ?? ??
- addressLen? ?? ???? ??? ??
1910.7 ??
- ???? ?? IP ??? PORT ??? ?? ??
- 32bit IP ??? 16bit port ?? ??
- ???? ?? ??
- s? ?? ???? ??
- ??? ?? A.B.C.D ??? IP ??? ??
- ? ?????? ??? 32bit? IP ??? ??
- ??? ?? ??? ??? ???? ??
- ? ?????? ?? 32bit IP ??? ??
- Inettime ?? ???? p536 p538 (debugging)
20Chef.c
- include ltstdio.hgt
- include ltsignal.hgt
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltsys/un.hgt / AF_UNIX ??? /
- define DEFAULT_PROTOCOL 0
- main()
-
- int serverFd, clientFd, serverLen, clientLen
- struct sockaddr_un serverUNIXAddress / ??
?? / - struct sockaddr_un clientUNIXAddress /
????? ?? / - struct sockaddr serverSockAddrPtr / ??
??? ?? ??? / - struct sockaddr clientSockAddrPtr /
????? ??? ?? ??? / - / ??? ???? ?? ??-?? ???? ?? /
- signal(SIGCHLD, SIG_IGN)
21Chef.c (cont)
- / ???, ??? ????? ??? ?? ?? /
- serverFd socket(AF_UNIX, SOCK_STREAM,
DEFAULT_PROTOCOL) - serverUNIXAddress.sun_family AF_UNIX /
??? ?? ?? / - strcpy (serverUNIXAddress.sun_path, "recipe")
/ ?? ??/ - unlink( "recipe") / ?? ?? ???, ?? ?? /
- bind(serverFd, serverSockAddrPtr, serverLen)
/ ?? ?? / - listen(serverFd, 5) / ???? ??? ?? ?? /
- while(1) / ???? /
-
- / ????? ?? ?? /
- clientFd accept(serverFd,
clientSockAddrPtr, clientLen) - printf(" Server called! ")
-
- if (fork() 0) / ???? ??? ?? ?? /
-
- writeRecipe(clientFd) / ??? ?? /
- close(clientFd) / ?? ?? /
22Cook.c
- include ltstdio.hgt
- include ltsignal.hgt
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltsys/un.hgt
- define DEFAULT_PROTOCOL 0
- main()
-
- int clientFd, serverLen, result
- struct sockaddr_un serverUNIXAddress
- struct sockaddr serverSockAddrPtr
-
- serverSockAddrPtr (struct sockaddr)
serverUNIXAddress - serverLen sizeof(serverUNIXAddress)
- / ???, ??? ???????? ???? /
23Cook.c (cont)
- readRecipe(clientFd) / ??? ?? /
- close(clientFd) / ?? ?? /
- exit(/ EXIT_SUCCESS / 0) / ?? ?/
-
- readRecipe(int fd)
-
- char str200
-
- while(readLine(fd, str)) / ?? ??? ? ?? /
- printf("s\n", str) / ?????? ? ?? /
-
- //
- readLine(int fd, char str)
-
- int n
- do
- n read(fd,str,1)
24inettime.c ???? ????
- include ltstdio.hgt
- include ltsignal.hgt
- include ltctype.hgt
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltnetinet/in.hgt / For
AFINET sockets / - include ltarpa/inet.hgt
- include ltnetdb.hgt
- define DAYTIME_PORT 13 /
Standard port o / - define DEFAULT_PROTOCOL 0
- unsigned long promptForINETAddress ()
- unsigned long nameToAddr ()
- main ()
-
- int clientFd / Client socket file descriptor
/ - int serverLen / Length of server address
structure / - int result / From connect () call /
- struct sockaddr_in serverINETAddress / Server
address / - struct sockaddr serverSockAddrPtr / Pointer
to address /
25inettime.c ???? ????(cont.)
- serverLen sizeof (serverINETAddress) / Length
of address / - while (1) / Loop until break /
-
- inetAddress promptForINETAddress () /
Get 32-bit IP / - if (inetAddress 0) break / Done /
- / Start by zeroing out the entire address
structure / - bzero ((char)serverINETAddress,sizeof(serv
erINETAddress)) - serverINETAddress.sin_family AF_INET /
Use Internet / - serverINETAddress.sin_addr.s_addr
inetAddress / IP / - serverINETAddress.sin_port htons
(DAYTIME_PORT) - / Now create the client socket /
- clientFd socket (AF_INET, SOCK_STREAM,
DEFAULT_PROTOCOL) - do / Loop until a connection is made with
the server / -
- result connect (clientFd,serverSockAdd
rPtr,serverLen) - if (result -1) sleep (1) / Try
again in 1 second / -
- while (result -1)
- readTime (clientFd) / Read the time from
the server /
26inettime.c ???? ????(cont.)
- unsigned long promptForINETAddress ()
-
- char hostName 100 / Name from user numeric
or symbolic / - unsigned long inetAddress / 32-bit IP format
/ - / Loop until quit or a legal name is entered
/ - / If quit, return 0 else return host's IP
address / - do
-
- printf ("Host name (q quit, s self)
") - scanf ("s", hostName) / Get name from
keyboard / - if (strcmp (hostName, "q") 0) return
(0) / Quit / - inetAddress nameToAddr (hostName) /
Convert to IP / - if (inetAddress 0) printf ("Host name
not found\n") -
- while (inetAddress 0)
-
- unsigned long nameToAddr (name)
- char name
-
27inettime.c ???? ????(cont.)
- if (strcmp (name, "s") 0) / Get host name
from database / -
- gethostname (hostName,100)
- printf ("Self host name is s\n",
hostName) -
- else / Assume name is a valid symbolic host
name / - strcpy (hostName, name)
- / Now obtain address information from
database / - hostStruct gethostbyname (hostName)
- if (hostStruct NULL) return (0) / Not
Found / - / Extract the IP Address from the hostent
structure / - hostNode (struct in_addr)
hostStruct-gth_addr - / Display a readable version for fun /
- printf ("Internet Address s\n", inet_ntoa
(hostNode)) - return (hostNode-gts_addr) / Return IP
address / -
- readTime (fd)
- int fd
-
28inettime.c ???? ????(cont.)
- readLine (fd, str)
- int fd
- char str
- / Read a single NEWLINE-terminated line /
-
- int n
- do / Read characters until NULL or
end-of-input / -
- n read (fd, str, 1) / Read one
character / -
- while (n gt 0 str ! '\n')
- return (n gt 0) / Return false if
end-of-input / -
2910.7 ??
- ???? ?????
- ?? ??? AF_INET
- ?? ?? ???(sockaddr_in)? 4??
- sin_family ??? ?????, AF_INET? ??
- sin_port ?? ?? ??
- sin_addr ?????/??? 32bit IP ??
- sin_zero ????, ????? ???
- ???? ??
- ?? ?? ???? ??? ????? ??? ??
- sin_port ??? INADDR_ANY? ??, ??? ??? ?? ?????
???? ?????? ?? ?? - struct sockaddr_in serverINETAddress / ??
???? ?? / - serverINETAddress.sin_family AF_INET / ????
???/ - serverINETAddress.sin_addr.s_addr
htonl(INADDR_ANY) - / ?? ????/
- serverINETAddress.sin_port htons(port) /??
?? ??/
3010.7 ??
3110.7 ??
3210.7 ??
- ???? ??
- ?? ?? ???? ??? ????? ??? ??
- sin_port ??? INADDR_ANY? ??, ??? ??? ?? ?????
???? ?????? ?? ?? - struct sockaddr_in serverINETAddress / ??
???? ?? / - serverINETAddress.sin_family AF_INET / ????
???/ - serverINETAddress.sin_addr.s_addr
htonl(INADDR_ANY) - / ?? ????/
- serverINETAddress.sin_port htons(port) /??
?? ??/
33IP????? ??? ?? ? ?? ??
- Media.inchon.ac.kr
192.203.144.1 - ??? ?? IP ??(binary)
dotted decimal - gethostbyname( )
inet_addr( ) -
- gethostbyaddr( )
inet_ntoa( )
11000011 11001011 10010000 00001011
34?????? ?????(????)
-
- ??? ???
?? ?? ?? ??? -
-
(??) - ?? ??? ?? 3
- ????? 4
(??) - 5
(??)
35??????? ??,TCP/IP? ??
- ??? ??? ????
??? -
-
?? ???? -
(????) -
?? -
-
(????) -
????? ?? -
??? ?? -
(IP??) -
???? - fd File Descriptor IPInternet Protocol
sdSocket Descriptor - TCPTransmission Control Protocol UDPUser
Datagram Protocol
??1
??2
??3
??4
fd3
sd4
sd3
sd3
sd3
3000 3001
3002
3003 TCP/IP
211.119.245.149
UDP
TCP
I P
36?? ??
- / Definitions for UNIX IPC domain
/usr/include/sys/un.h / - struct sockaddr_un
- sa_family_t sun_family
/ AF_UNIX / - char sun_path108
/ path name (gag) / -
- / Socket address, internet style
/usr/include/netinet/in.h / - struct sockaddr_in
- sa_family_t sin_family
- in_port_t sin_port
- struct in_addr sin_addr
37?? ??
- / Internet address /usr/include/netinet/in.h
- This definition contains obsolete fields
for compatibility - with SunOS 3.x and 4.2bsd. The presence
of subnets renders - divisions into fixed fields misleading at
best. New code - should use only the s_addr field. /
- if !defined(_XPG4_2) defined(__EXTENSIONS__)
- define _S_un_b S_un_b
- define _S_un_w S_un_w
- define _S_addr S_addr
- define _S_un S_un
- endif / !defined(_XPG4_2) defined(__EXTENSION
S__) / - struct in_addr
- union
- struct uchar_t s_b1, s_b2,
s_b3, s_b4 _S_un_b - struct ushort_t s_w1, s_w2
_S_un_w - if !defined(_XPG4_2) defined(__EXTENSIONS__)
- uint32_t _S_addr
- else
38?? ??(Port Number)
- 16-?? ?? ??
- ?? ??? ????? ???? TCP? UDP? ??? ? ?? ?? ? ???
????? ??? ??? ?? ?? - 32-?? Internet ???? ???? ?? ??, ?? ???? ???? ??
?? - 1255 ?? ??? ?? ??
- /usr/include/netinet/in.h ?? ?? ??(well-known
ports) ?? - (?) FTP ?? TCP/IP ?? ?? 21
- 4.3BSD 11023 ???? ???? ?? ??
- 1023 lt TCP? ?? ?? lt 5000
- ??? ??(TCP/IP ?? UDP/IP)?? ??? ???? 5?
- ??(TCP ?? UDP)
- ?? ???? Internet ??(32-??)
- ?? ?? ??(16-??)
- ?? ???? Internet ??(32-??)
- ?? ?? ??(16-??)
- (?) tcp, 128.10.0.3, 1500, 128.10.0.7, 21
39inetserver.c ???? ??
- / compile gcc -o client client.c -lsocket
-lnsl / - / execute server /
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltnetinet/in.hgt
- include ltarpa/inet.hgt
- include ltnetdb.hgt
- define PORTNUM 3000
- main(int argc, char argv)
-
- int serverFd, clientFd, clilen, childpid
- struct sockaddr_in cli_addr, serv_addr
- if((serverFd socket(AF_INET, SOCK_STREAM,
0)) lt 0) - printf("server can't open stream socket")
- return -1
-
- bzero((char ) serv_addr, sizeof(serv_addr))
- serv_addr.sin_family AF_INET
40inetserver.c ???? ??(cont.)
- for( )
- / Wait for a connection from a client
process. - This is an example of a concurrent
server. / - clilen sizeof(cli_addr)
- clientFd accept(serverFd, (struct
sockaddr ) cli_addr, clilen) - printf("Server called\n")
- if((childpid fork()) lt 0)
- printf("server fork error")
- exit(0)
-
- else if(childpid 0) / child
process / - printf("serverFd d, clientFd d\n",
serverFd, clientFd) - /
process the request / - write(clientFd,"Hello!",7)
- close(clientFd) / close
original socket / - return -1
-
- close(clientFd) / parent
process / -
41inetclient.c ???? ?????
- / compile gcc -o client client.c -lsocket
-lnsl / - / execute inetclient hostname(or server IP)
/ - include ltstdio.hgt
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltnetinet/in.hgt
- include ltarpa/inet.hgt
- include ltnetdb.hgt
- define PORTNUM 3000
- main(int argc, char argv)
-
- int clientFd
- char str10
- char hostaddress
- struct sockaddr_in serv_addr
- struct hostent hostStruct
- struct in_addr hostNode
- if(argv1 NULL)
- printf("Usage inetclient hostname(or
server IP)\n")
42inetclient.c ???? ?????(cont.)
- hostStruct gethostbyname(argv1)
- if(hostStruct NULL) return(0)
- hostNode (struct in_addr)
hostStruct-gth_addr - hostaddress inet_ntoa(hostNode)
- / Fill in the structure "serv_addr" with the
address of the - server that we want to connect with. /
- bzero((char ) serv_addr, sizeof(serv_addr))
- serv_addr.sin_family AF_INET
- serv_addr.sin_addr.s_addr inet_addr(hostaddre
ss) - serv_addr.sin_port htons(PORTNUM)
- /Open a TCP socket (an Internet stream
soskcet). / - if((clientFd socket(AF_INET, SOCK_STREAM,
0)) lt 0) - printf("client can't open stream socket")
- / Connect to the server /
- if(connect(clientFd, (struct sockaddr )
serv_addr, sizeof(serv_addr)) lt 0) - printf("client can't connect to server")
- printf("clientFd d\n", clientFd)
- read(clientFd, str, 10)
- printf("s\n", str)
4310.8 ???? ?
- ??
- ???? ?? ?? ?? ??
- ?? ??? ?? ?????, ????-??? ?? ??
- ?? ??? ??? ??? ???? ?? ??
- ?? ??(token)? ??(tab ?? space)? ??? ??
- ?? ?? ??? ???? ??
4410.8 ???? ?
- ?? ?? ?? ??? ?? ??? ??? ??
-
ltinternetshellcommandgt ltsequencegt
ltsequencegt ltpipelinegt ltpipelinegt
ltpipelinegt ltsimplegt ltsimplegt ltsimplegt
lttokengt ltredirectiongt ltredirectiongt
ltfileRedirectiongt ltsocketRedirectiongt
ltfileRedirectiongt \gt ltfilegt gtgt ltfilegt \lt
ltfilegt ltsocketRedirectiongt ltclientRedirectiongt
ltserverRedirectiongt ltclientRedirectiongt
_at_\gtc ltsocketgt _at_\ltc ltsocketgt ltserverRedirectiongt
_at_\gts ltsocketgt _at_\lts ltsocketgt lttokengt a
string of chracters ltfilegt a valid UNIX
pathname ltsocketgt either a UNIX pathname (
UNIX domain socket ) or an Internet socket
name of the form hostname.port
4510.8 ???? ?
- ???? ? ????
- ???? ? ?? ?? ?? ish
- ???? ? ???? ????(?)
- ish ??? ??? ??? ??? PATH ????? ????
- PATH ?? ????? setenv? ??? ?? ??
- ???? ? ??? control-D
- ?? ???
- ?? ???? P553
46?? ?? ????
- ??? ?? ????
- netprog? talk_server.c talk_client.c ???? ??
- ??
- talk_server ????
- talk_client ?????IP?? ????
- ??? ?? ????
- ??? ????? ?? ?? ?? ??? ??????? ?? ??? ? ? ? ???
accept() ?? read()?? ?? ???? ??(synchronous,
blocking) ?? ?? select() ??? ??? ???? ???
???(asynchronous, non-blocking) ??? ?? - netprog? chat_server.c chat_client.c ?? ?? ??
- ??
- chat_server ????
- chat_client ?????IP?? ????
47select()
- select()
- ???? ???? I/O ??? ????? ??? /O ??? ???? ?? ?
- ?? ?? s? ??? ??????? ??? I/O ?? ??
- select() ??? ?? ??
- int select (
- int maxfdp1, / chleo ??(? ??) ?? ?? 1 /
- fd_set readfds, / ?? ?? ??? ??? ?? ?? /
- fd_set writefds, / ?? ?? ??? ??? ?? ?? /
- fd_set exceptfds, / ?? ?? ??? ??? ?? ?? /
- struct timeval tvptr) /select() ??? ??? ????
?? / - FD_ZERO(fd_set fdset) ???? ?? ?? ??
- FD_SET(int fd, fd_set fdset) ???? ?? fd? fdset
??? ???? ?? fdset ? fd? ???? ??? 1? ?? - FD_ISSET(int fd, fd_set fdset) ???? fdset? ?? fd
??? 1? ???? ??? ??? ????? fd?? fdset ??? ????? ??
48X ??? ????? ??? ??
- X ??? ?????
- /usr/X/demo ??? ??
- puzzle
- spider
- xeyes
- xsol
- ?
- X manager http//www.netsarang.co.kr?? 30? ??
???? - Http//marvel.inchon.ac.kr/mysung ? Lecture
Notes ? X Window Program ?? - Anonymous ftp multi cd pub/xprog ??
- ??? ??
- Http//marvel.inchon.ac.kr/mysung ? Lecture
Notes ? System Administration ??
49?? 12/23(?)??
- ?? 1 ??, ??, ? ??? ??? ??
- ? ?? ????? ?? ?? ???? ???? ??? ? ? ?? ??, ?? ?
??? ????. - ? ??? ??? xx?? ?? ???? 5000 ??
- refree 5000
- ? ??? ??? yy?? ??
- player xx 5000
- ? ??? ??? zz?? ??
- player xx 5000
- ?? 2 ??? ?? ????
- ?? ??? ??? ?????? ??? ???? ??? ?? ?? ?? ??? ????
???? ??? ???? ?? ??????? ?? ?? ???? ?? ?? ?????
?? ???? ??? ??? ?? chat_server? chat_client ???
?? ????? ???? ??? ??, ? ?? ????? ?? ??? ??? ????
?who ??? ?? ??. - ?? ?? ??
- ???? ???? ?? ?? 1023 lt ???? lt 5000
- ?? ?? ? ??? ?? ???? ?? kill -9 ??????(ps ????
???)
50?! ???
- ???? ????.
- ???? ????? ??? ???? ???.
- ???? ?? ?? ??? ?? ?????. ??? ?? ???? ? ??? ????
??? ??? ?? ?? ??. - ???? ???? ??? ???? ?? ??? ????.
?? ?????!