Title: ? ??????? ?? ???????????? ?????????????? ?????????????? ?????? (peer-to-peer) ???????????????? ??????????? ?????????? ????? ????????? ????? ???????? ? ????????.
1?????????? ??????-??????
- ??????????? ?????????????? ??????????????
???????, ? ??????? ?????????? ??????????
?????????? ????????? ?? ?????????? ? ?????????
????????. - ? ??????? ?? ???????????? ??????????????
?????????????? ?????? (peer-to-peer)
???????????????? ??????????? ?????????? ?????
????????? ????? ???????? ? ????????.
??????
????
????
??????
??????
??????
????
????
??????
??????
????
????
??????
Rev. 1.0 / 1.10.2007
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
2?????????? ??????-??????
- ??????????? ???????
- ????????? ???????
- ?????? ??????????????? ?????????? ? ?????????
??????????? ???????? - ?????? ??????????????? ??????????????? ? ????????
????????????? (??????????? GUI).
- ??????????? ???????
- ???????? ??????? ??????? ???????
- ?????? ??????????????? ?????????? ? ???????
??????????? ???????? - ?????? ??????????????? ?? ??????????????? ?
?????????????.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
3?????????? ??????-??????
- ????????????? ??????????? ?????? ? ??????.
- ????????????? (??????????????) ?????????????????
,?????? ?????????? (???????????? ?????? ???
????????),?????? ??? ?????? (?????? ?????? ???
??????? ??????????).
????? ??????????????, ???????? ?????????
?????????????????? ? ??????????
- ?????????? ???????? ???????, ??????? ??????? ?
????????????
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
4?????????? ??????-??????
- ???????????? (?? ????????? ? peer-to-peer)
- ????????????
- ????????? ???????????? (???????? ??????? ??
??????? ???????) - ???????????????? ?????????? ??????
- ??????? ?????????? ??????? ???????
- ??????????? ???????? ?????? ?? ????????????????
????????
- ?????????? (?? ????????? ? peer-to-peer)
- ????????????? ???????? ?? ???????????????? ??????
- ??????? ? ????? ??????? ? ??????? ???????
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
5Inter-Process Communication
- ???? (file)
- ?????? (signal)
- ????? (socket)
- ???????? (pipe), ??. ????? FIFO
- ??????? (semaphore)
- ??????????? ?????? (shared memory)
- ??????? ????????? (message queue)
- ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
6C?????
- ???????? ????? ???????????????? ??????????????
?????? ???? ????? ????? ?????????? (??????). - ??????????
- ????? ???? ????????????????? (???? ???????????).
- ????? ?? ????????????? ?????????? ???????? ???
???????????????. - ????? ?? ?????? ???????????? ??? ????? ??????
?????? ?????? ?????? ???????? ?????? ??? ??????.
??????? 2
??????? 1
READ WRITE
READ WRITE
FIFO
FIFO
?????
?????
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
7C?????
??????? ?????? (network sockets ??? Internet
sockets)
????????? ?????? (Unix domain sockets ??? POSIX
Local IPC Sockets)
TCP-??????
UDP-??????
raw-IP-??????
??? ?????? IP-????? ? ????.
??? ?????? ??? ????. ?????.
Berkeley Sockets API (1983) ??????????? ??, ?.
?. ? Windows Winsock(??????????? Winsock
??????? ??? ??????????? ?????? ?
????????) ???????????? Transport Layer Interface
(TLI) ??? X/Open Transport Interface (XTI) ?
Solaris, Mac OS
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
8Berkeley Sockets API
- ???????????? ?????
- ltsys/socket.hgt - ???????? ??????? ?????? ?
???????? ? ??????? ????????? ?????? - ltnetinet/in.hgt - ????????? ?????? ?????????????
??? ??????? ??????? - ltsys/un.hgt - ????????? ?????? ????????????? ???
????????? ??????? - ltarpa/inet.hgt - ??????? ??? ???????????
IP-???????? - ltnetdb.hgt - ??????? ??? ???????? ????????
?????????? ? ?????? ? ????????? ???????? (? ?.?.
DNS-???????)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
9Berkeley Sockets API
?????????????? ? (??) ??????? ???????
???? include ltnetinet/in.hgt uint32_t
htonl(uint32_t hostlong) / host-to-network-long
/ uint16_t htons(uint16_t hostshort) /
host-to-network-short / uint32_t ntohl(uint32_t
netlong) / network-to-host-long / uint16_t
ntohs(uint16_t netshort) / network-to-host-shor
t /
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
10Berkeley Sockets API
- ???????? ???????
- ??????? ?????
- ???????????? ?????????? ? ????????
- ????????? ? ??????? ?????? ? ??????? ??
- ??????? ?????
- ???????? ???????
- ??????? ?????
- ?????? ??? ??????
- ????????? ????? ? ????? ?????????????
- ??????? ?????????? (????????? ????? ?????)
- ????????? ???????
- ??????? ??????
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
11Berkeley Sockets API
???????? ?????? include ltsys/types.hgt include
ltsys/socket.hgt int socket(int domain, int type,
int protocol) domain PF_UNIX/PF_LOCAL,
PF_INET, PF_INET6 ? ??. type SOCK_STREAM,
SOCK_DGRAM, SOCK_RAW ? ??. protocol
(ltnetinet/in.hgt) IPPROTO_TCP, IPPROTO_UDP ?
??. ????????? -1 ??? ??????, ? ????????? ??????
????? ??????????????? ????? (??????????
??????). ?????? int sock sock socket(PF_INET,
SOCK_STREAM, IPPROTO_TCP) if (sock lt 0)
perror("socket() failed") exit(1)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
12Berkeley Sockets API
???????? ?????? ?????? EPROTONOSUPPORT ??
?????????????? ????????? ???????? EAFNOSUPPORT
?? ?????????????? ????????? ?????????
??????? EINVAL ??????????? ???????? ???
????????? ??????? EACCES ?????? ?? ????????
?????? ??????? ???? ???????? ENFILE, EMFILE,
ENOBUFS, ENOMEM ???????????? ?????????
???????? ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
13Berkeley Sockets API
??????? ????? ?????? include ltsys/types.hgt inclu
de ltsys/socket.hgt int bind( int
sockfd, /??????????/ struct sockaddr
my_addr, /????? (???)/ socklen_t
addrlen) /?????? my_addr/ ????????? 0 ???
???????? ?????????, -1 ??? ??????. ???????
??????? ??????????? ??? ????????? ????????
?????????? ? ????????? ???????? ???????.
????????? sockaddr ??? '???????'
????????? struct sockaddr unsigned short int
sa_family unsigned char sa_data14
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
14Berkeley Sockets API
??????? ????? ?????? ??? ?????????
PF_INET struct sockaddr_in sa_family_t
sin_family /????????? AF_INET/ in_port_t
sin_port /????/ struct in_addr
sin_addr /IP-?????/ unsined char
sin_zero8 struct in_addr in_addr_t
s_addr ?????? struct sockaddr_in
saddr saddr.sin_family AF_INET saddr.sin_port
htons(80) /HTTP-????/ saddr.sin_addr.s_addr
htonl(INADDR_ANY) /0.0.0.0/ if
(bind(sock,(struct sockaddr)saddr,sizeof(saddr))
) perror("bind() failed") exit(1)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
15Berkeley Sockets API
??????? ????? ?????? ?????? EINVAL ? ??????
??? ???? ??? EACCES ???????
???????????????????? ???????? ??????????? ? ?????
? ??????? ?????? 1024 EADDRINUSE ????? ?
????????? ?????? ??? (???) ?????????? EADDRNOTAVAI
L ????????? ????? ?? ?????? ?? ? ?????
????????? ??????? ??????????? ENOBUFS, ENOMEM
?? ??????? ????????? ???????? EBADF, ENOTSOCK
?????? ????? ??????????
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
16Berkeley Sockets API
??????? ?????? ? ????? ?????????????
(SOCK_STREAM) include ltsys/socket.hgt int
listen(int sockfd, int backlog) sockfd
?????????? ?????? backlog ???????????? ?????
??????? ??????????, ????????? ????????????? (??
?????? ?? ?? ????? ????????? 5, ? Linux
???????????? ???????? SOMAXCONN128) ?????????
0 ??? ???????? ?????????, -1 ???
??????. ?????? if (listen(sock, SOMAXCONN))
perror("listen() failed") exit(1)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
17Berkeley Sockets API
??????? ?????? ? ????? ????????????? ?????? EADD
RINUSE ????? ? ????????? ?????? ??? (???)
?????????? ? ????????? ? ??????
????????????? EBADF, ENOTSOCK ?????? ?????
?????????? EOPNOTSUPP ???????????? ??? ??????
(?????? ???? SOCK_STREAM ??? SOCK_SEQPACKET)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
18Berkeley Sockets API
????? (?????????????) ?????????? include
ltsys/types.hgt include ltsys/socket.hgt int
accept( int sockfd, /??????????/ struct
sockaddr addr, /????? (???)/ socklen_t
addrlen) /?????? my_addr/ ????????? -1
??? ??????, ? ????????? ?????? ?????
??????????????? ????? (?????????? ?????? ??????).
????? ????? ?? ????????? ? ?????????
?????????????, ???????? ?????? sockfd (????????
O_NONBLOCK ??? O_ASYNC) ?? ?? ???????????.
????????? ????????? ?????? sockfd ?? ????????.
????? ????? ???? ???????????. ?????? struct
sockaddr_in remote_addr socklen_t len
sizeof(remote_addr) int csock csockaccept(sock,
(struct sockaddr)remote_addr,len) if
(csocklt0) /??????/
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
19Berkeley Sockets API
????? (?????????????) ?????????? ?????? EBADF,
ENOTSOCK ?????? ????? ?????????? EAGAIN,
EWOULDBLOCK ????????????? ?????, ???
?????????? EOPNOTSUPP ???????????? ??? ??????
(?????? ???? SOCK_STREAM) EINTR ????? ???
??????? ???????? EINVAL ????? ?? ????????? ?
????????? ????????????? ECONNABORTED ??????
?????????? ENFILE, EMFILE, ENOBUFS, ENOMEM
???????????? ????????? ???????? ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
20Berkeley Sockets API
?????? ?????????? include ltsys/types.hgt include
ltsys/socket.hgt int connect( int
sockfd, /??????????/ struct sockaddr
addr, /????? (???)/ socklen_t
addrlen) /?????? my_addr/ ????????? 0 ???
???????? ??????????, -1 ??? ??????. ???? ?
?????? sockfd ??? ?????????? ?????, ?? ???????
??? ??????. ????? ????? ???? ???????????. ??????
struct sockaddr_in serv_addr serv_addr.sin_family
AF_INET serv_addr.sin_port
htons(80) /HTTP-????/ serv_addr.sin_addr.s_addr
htonl(INADDR_LOOPBACK) /127.0.0.1/ if
(connect(sock, (struct sockaddr)
serv_addr, sizeof(serv_addr))) /??????/
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
21Berkeley Sockets API
?????? ?????????? ?????? EBADF, ENOTSOCK
?????? ????? ?????????? EISCONN ????? ??? ???
???????? ECONNREFUSED ?? ?????? ???????
????????? ???? ????? ?? ??????? ETIMEDOUT
??????? ENETUNREACH ???? ?????????? EINPROGRESS
????????????? ?????, ?????????? ???
??????????????? EALREADY ????????????? ?????,
?????????? ??????? ??? ?? ??????????? EACCES,
EPERM ??????? ????????????????? ???????? ???
????????? ??????????????? ????? ?????? ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
22Berkeley Sockets API
???????? ?????? ? ????? include
ltsys/types.hgt include ltsys/socket.hgt size_t
sendto( int sockfd, /??????????
??????/ const void buf, /???
????????/ size_t len, /?????? ????????????
??????/ int flags, /?????/ const struct
sockaddr to, /???? ????????/ socklen_t
tolen) /????? ????????? to/ size_t send( int
sockfd, const void buf, size_t len, int
flags) include ltunistd.hgt size_t write(int fd,
const void buf, size_t len)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
23Berkeley Sockets API
???????? ?????? ? ????? write(sock,buf,len)
ltgt send(sock,buf,len,0) ltgt sendto(sock,buf,len,0
,NULL,0) ????? MSG_OOB ??????? ??????
out-of-band MSG_DONTROUTE ?? ???????? ??????
????? ???? MSG_DONTWAIT ????????????? ????????
(???????????? EAGAIN) MSG_NOSIGNAL ?? ????????
?????? SIGPIPE, ???? ?????????? ????????? (?????
????????????? ?????? EPIPE) ????????? -1 ???
??????, ? ????????? ?????? ????????????
?????????? ?????????? ????. ????? ????? ????
???????????. send() ? write() ????? ????????????
??? ??????? ? ????????? ?????????????? ??????????
(??? SOCK_STREAM), sendto() ??? ???? ?????????
(SOCKET_DGRAM)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
24Berkeley Sockets API
???????? ?????? ? ????? ?????? EBADF, ENOTSOCK
?????? ????? ?????????? EAGAIN, EWOULDBLOCK
????????????? ?????, ???????? ??????
????????????? ?? ??? EISCONN ????? ??? ???
????????, ? ? sendto() ?????? ?????????? ECONNRESE
T ?????? ??????? ???????? ?????????? ENOTCONN,
EDESTADDRREQ ????? ?? ????????, ? ?????
?????????? ? sendto() ?? ?????? EINTR ????? ???
??????? ???????? ENOBUFS, ENOMEM ????????????
????????? ???????? EFAULT ????????????????
????? ?????? EOPNOTSUPP ????????????????
????????? ?????? ??? ??????? ?????? EPIPE ?
????????? ??????? ????? ??? ?????? ??? ????????
(????? ?????????? ?????? SIGPIPE, ???? ??
?????????? ????? MSG_NOSIGNAL) ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
25Berkeley Sockets API
????? ?????? ?? ?????? include
ltsys/types.hgt include ltsys/socket.hgt size_t
recvfrom( int sockfd, /??????????
??????/ const void buf, /????
?????????/ size_t len, /?????? ??????
??????/ int flags, /?????/ const struct
sockaddr from, /?????? ?????????/ socklen_t
fromlen) /????? ????????? from/ size_t
recv( int sockfd, const void buf, size_t
len, int flags) include ltunistd.hgt size_t
read(int fd, const void buf, size_t len)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
26Berkeley Sockets API
????? ?????? ?? ?????? read(sock,buf,len)
ltgt recv(sock,buf,len,0) ltgt recvfrom(sock,buf,len
,0,NULL,0) ????? MSG_OOB ??????? ??????
out-of-band MSG_PEEK ???????? ??????, ?? ??
??????? ?? ?? ???????? ?????? MSG_WAITALL
??????? ?????? ?? ??? ???, ???? ?? ????? ????????
??????????? ?????????? ???? MSG_DONTWAIT
????????????? ???????? (????????????
EAGAIN) ????????? -1 ??? ??????, ? ?????????
?????? ???????????? ?????????? ???????? ????.
???? ???????????? 0 ????? ?????? ? ??????
???????. ??????????? ?????? ???????? ? SIGPIPE.
????? ????? ???? ???????????. recv() ? read()
????? ???????????? ??? ??????? ? ?????????
?????????????? ?????????? (??? SOCK_STREAM),
recvfrom() ??? ???? ????????? (SOCKET_DGRAM)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
27Berkeley Sockets API
????? ?????? ?? ?????? ?????? EBADF, ENOTSOCK
?????? ????? ?????????? EAGAIN, EWOULDBLOCK
????????????? ?????, ????? ?????? ???????????? ??
??? ENOTCONN ????? ?? ???????? EINTR ?????
??? ??????? ???????? ENOBUFS, ENOMEM
???????????? ????????? ???????? EFAULT
???????????????? ????? ?????? ? ??.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
28Berkeley Sockets API
???????? ?????? include ltsys/socket.hgt int
shutdown(int sockfd, int how) how SHUT_RD (0)
?????????? ????? ?????? ?? ?????? SHUT_WR (1)
?????????? ???????? ?????? ? ????? SHUT_RDWR (2)
?????????? ????? ? ???????? ?????? ??? ???????,
??????????????? ?? ?????????? (SOCK_STREAM). ?????
?? ?? ??????????? ??????????. ????? ?????? ????
?????? ??????? include ltunistd.hgt int
close(int fd) ????????? 0 ??? ????????
??????????, -1 ??? ??????.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
29Berkeley Sockets API
??????? ??????????? IP-??????? include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltarpa/inet.hgt int inet_aton(const char cp,
struct in_addr inp) ??????????? IP-?????,
?????????? ?????? ?????????? (ASCIIZ-??????), ??
??????????? ????? ? ???? ?????????? ?????,
??????????? ???????, ? ???????? ????? ? ???????
??????? ??????. ????????? ??????????????
?????????? ? ?????????, ?? ??????? ?????????
?????? ????????. ??????? ?????????? ?????????
???????? ??? ???????? ??????????????, 0 ???
??????. ?????? struct sockaddr_in
addr inet_aton("192.168.0.1", addr.sin_addr)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
30Berkeley Sockets API
??????? ??????????? IP-??????? include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltarpa/inet.hgt in_addr_t inet_addr(const char
cp) ??????????? IP-?????, ?????????? ??????????
(ASCIIZ-??????), ?? ??????????? ????? ? ????
?????????? ?????, ??????????? ???????, ? ????????
????? ? ??????? ??????? ??????. ??? ??????
???????????? ???????? INADDR_NONE (-1), ???????
????? ????? ?????????????????? ??? ?????
255.255.255.255, ??????? ???????????????
????????????? inet_aton(). ?????? struct
sockaddr_in addr addr.sin_addr.s_addr
inet_addr("192.168.0.1")
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
31Berkeley Sockets API
??????? ??????????? IP-??????? include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltarpa/inet.hgt char inet_ntoa(struct in_addr
in) ??????????? IP-?????, ?????????? ??????????,
?? ???????? ????? ? ??????? ??????? ???? ?
??????????? ????? ? ???? ?????????? ?????,
??????????? ???????. ?????????????? ASCIIZ-??????
???????? ? ??????????? ??????, ??????? ???
??????????? ??????? ??????????????. ?????? struct
sockaddr_in raddr socklen_t len
sizeof(raddr) int csock csock accept(sock,
(struct sockaddr) raddr, len) if (csocklt0)
/??????/ printf("Peer address
sd\n", inet_ntoa(raddr.sin_addr), ntohs(raddr
.sin_port))
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
32Berkeley Sockets API
??????? ??????????? IP-??????? ???????? POSIX
1003.1-2001 ??????????? ?????? ???????
inet_aton() ???????????? ????????? ????,
????????? ??? ???????????? ? IPv4, ?
IPv6 include ltsys/types.hgt include
ltsys/socket.hgt include ltarpa/inet.hgt int
inet_pton(int af, const char src, void
dst) af AF_INET (dst struct in_addr),
AF_INET6 (dst struct in6_addr) ???????
inet_pton() ??????????? IP-?????, ??????????
?????????? src, ?? ????????? ????? ? ???????? ?
??????? ??????? ???? ? ???????????? ? ?????????
?????????? ??????? af. ??????? ??????????
????????????? ???????? ??? ???????? ??????????, 0
???? ?????? ?? ????? ???? ???????????????? ???
IP-????? ? ????????????? ????????, ???? af
???????? ???????????????? ???????? ?????????..
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
33Berkeley Sockets API
??????? ??????????? IP-??????? ???????? POSIX
1003.1-2001 ??????????? ?????? ???????
inet_ntoa() ???????????? ????????? ????,
????????? ??? ???????????? ? IPv4, ?
IPv6 include ltsys/types.hgt include
ltsys/socket.hgt include ltarpa/inet.hgt const char
inet_ntop( int af, const void src, char dst,
size_t cnt) af AF_INET (src struct in_addr),
AF_INET6 (src struct in6_addr) ???????
inet_ntop() ??????????? IP-?????, ??????????
?????????? src, ?? ???????? ????? ? ???????
??????? ???? ? ????????? ?????. ??????????????
ASCIIZ-?????? ?????????? ? ?????????
????????????? ????? dst. ???????????? ??????
??????????????? ?? ??????? ???? INET_ADDRSTRLEN
(??? INET6_ADDRSTRLEN) ???? ??? ????? dst.
??????? ?????????? ???????? ????????? dst ???
NULL ??? ??????.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
34Berkeley Sockets API
??????? ?????? ? ?? ????? ???? (DNS) include
ltnetdb.hgt include ltsys/socket.hgt struct hostent
gethostbyname(const char cp) struct hostent
gethostbyaddr( const char addr, int len, int
type) ??????? ????????? ????? ? ?? ????? (DNS)
?????????? ?? ????????? ?????. ???
gethostbyname() ???? ???????? ???????? ?????? ???
IP-??????? ? ?????????-???????? ???????. ???
gethostbyaddr() ???? ???????? IP-??????? ?
???????? ???? ? ??????? ??????? ???? (addr),
?????? ???????? ?????????? ????? ??????, ? ??????
?????? ???? AF_INET ??? AF_INET6). ??? ??????
???????????? NULL. ??????? ????? ??????
?????????? ? /etc/hosts, ? DNS, ? LDAP, ? NIS ?
??. ?????? ?????? ?????????? ? ??????? ??????
????? ???????????? Name Service Switch
Configuration (/etc/nsswitch.conf)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
35Berkeley Sockets API
??????? ?????? ? ?? ????? ???? (DNS) struct
hostent char h_name /??????????? ???
?????/ char h_aliases /??????
???????????/ int h_addrtype /??? ??????
AF_INET/ int h_length /????? ??????/ char
h_addr_list /?????? ???????/ ???????
h_aliases ? h_addr_list ?????????? ????????? ??
????????? 0. ?????? ???????? ? ??????? ???????
????. ??????? gethostbyname() ? gethostbyaddr()
?????????? ????????? ?? ?????????, ??????????? ?
??????? ?????? ???? ?? (?? ???? ???????? ?????
??? ????????? ?????? ??? ?????????). ???? ???
gethostbyname() ? ???????? ????????? ???????
IP-????? ?????, ?? ?? ????? ?????? ?????????? ?
???? h_name (DNS-?????? ?? ????? ???????????).
???? ????? ???????? ???????? ??? ?? IP-??????,
??????????? gethostbyaddr().
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
36Berkeley Sockets API
??????? ?????? ? ?? ????? ???? (DNS) ?????? stru
ct hostent he int i char bufINET_ADDRSTRLEN"
" he gethostbyname("iq.karelia.ru") if (!he)
/??????/ printf("\nOfficial name s",
he-gth_name) i0 while (he-gth_aliasesi) printf
("\nAlias s", he-gth_aliasesi) if
(he-gth_addrtype AF_INET) if
(inet_ntop(AF_INET, he-gth_addr_list0, buf,
INET_ADDRSTRLEN)) printf("\nIP s\n", buf)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
37Berkeley Sockets API
??????? ?????? ? ?? ????? ???? (DNS) ????
????????? ????????? ??? ???????? ? DNS, ?????
????? ?????????? ?? ? ????? ?????? ? DNS ??
?????? ?????? ??????????. void sethostent(int
stayopen) ???? stayopen1, TCP-?????????? ? DNS
?? ????? ??????????? ????? ?????????????????
?????????. ???? stayopen0, ?????? ?????? ? DNS
????? ???????? ? ???? ?????????
UDP-???????????. ?????????? ?????? ?????? ? DNS
?? ?????? TCP-?????????? void endhostent(void)
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
38Berkeley Sockets API
??????? ?????? ? ?? ????? ???? (DNS) ??? ??????
??????? ?????? ? ?? ????? ???? ???????? ???
?????? ? ?????????? h_error. ???????? HOST_NOT_FO
UND ?????????? ? ????? ?? ??????? NO_ADDRESS,
NO_DATA ? ??????????? ?????? ?? ??????
IP-????? NO_RECOVERY ?????? ??? ?????? ? ??
????? ???? TRY_AGAIN ????????? ?????? ?????
???????? ????????? ????????? ?? ?????? ??? ??????
??????? const char hstrerror(int err) ?
??????? ? ???????? ????????? ?????????? ????????
h_error. ??? ??????? ????????? ?? ?????? ??
??????????? ??????????????? ??????????
stderr void herror(const char s) s
?????????????? ???????????, ????????? ?????
?????????? ?? ??????.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
39Berkeley Sockets API
??????? ?????? ? ?? ?????????? ? ???????
??????? include ltnetdb.hgt struct servent
getservbyname( const char name, const char
proto) struct servent getservbyport( int
port, const char proto) ???????
getservbyname() ????????? ????? ? ??
(/etc/services) ?????????? (?????? ?????) ??
????????? ??????? ??????. ??????? getservbyport()
?????????? ?????????? ? ??????? ?????? ?? ??????
?? ????? (port ? ??????? ??????? ????).
???????? proto ????? ???? NULL, ????? ?????
?????????????? ??? ?????? ?????????. ??? ??????
???????????? NULL.
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
40Berkeley Sockets API
??????? ?????? ? ?? ?????????? ? ???????
??????? struct servent char
s_name /??????????? ??? ??????/ char
s_aliases /?????? ???????????/ int
s_port /????? ?????/ char s_proto /???????
?/ ?????? s_aliases ????????? ????????? ??
????????? 0. ????? ????? s_port ???????? ?
??????? ??????? ????. ??????? getservbyname() ?
getservbyport() ?????????? ????????? ??
?????????, ??????????? ? ??????? ?????? ???? ??
(?? ???? ???????? ????? ??? ????????? ??????
??? ?????????).
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
41Berkeley Sockets API
??????? ?????? ? ?? ?????????? ? ???????
??????? ?????? int i struct servent se se
getservbyname("www", NULL) / ??? ??? se
getservbyport(htons(80), "tcp") / if (!se)
fprintf(stderr, "\nservice not found!\n")
exit(1) printf("\nOfficial name s",
se-gts_name) i0 while (se-gts_aliasesi) printf
("\nAlias s", se-gts_aliasesi) printf("\ns
d\n", se-gts_proto, ntohs(se-gts_port))
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
42Berkeley Sockets API
??????? ?????? ? ?? ?????????? ?
?????????? (/etc/protocols) include
ltnetdb.hgt struct protoent getprotobyname(const
char name) struct protoent getprotobynumber(in
t proto) struct protoent char
p_name /??????????? ???/ char
p_aliases /??????????/ int
p_proto /????? ?????????/
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????
43Berkeley Sockets API
????? ?????? include ltsys/types.hgt include
ltsys/socket.hgt int getsockopt(int s, int level,
int optname, void optval, socklen_t
optlen) int setsockopt(int s, int level, int
optname, const void optval, socklen_t
optlen) ??????? ?????????? 0 ??? ????????
???????? ??? -1 ??? ??????. level
SOL_SOCKET optname SO_KEEPALIVE (???????? int)
???????? ?????? keep-alive ???
TCP SO_REUSEADDR (???????? int) ????? ???????
??????? ?????????? ?????? ??? ?????????? bind
(????? ????????? ????? ? ?????, ???? ??? ????
??? ???????? ???????? ??????????) man 7 socket
?????????????? ??????????????, ?. ?. ????????,
2007
??????? ??????????