Ch5 Linux Network Programming - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Ch5 Linux Network Programming

Description:

Title: Linux System Programming: Process Management Author: Jianjian Song Last modified by: song Created Date: 7/16/2003 1:27:40 PM Document presentation format – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 57
Provided by: Jianji8
Category:

less

Transcript and Presenter's Notes

Title: Ch5 Linux Network Programming


1
Ch5 Linux Network Programming
  • Jianjian SONG
  • Software Institute, Nanjing University
  • Nov. 2004

2
Content
  1. TCP/IP Review
  2. Berkeley Socket Basics
  3. System Calls and Routines
  4. Network information
  5. Multi-client
  6. Using inetd/xinetd
  7. Remote Procedure Call (RPC)

3
1. TCP/IP Review
  • ??????
  • 1969?,ARPA????????
  • 1972?,ARPAnet,????????,??NCP??(Network Control
    Program)
  • 1973?,TCP?????
  • 1980?,TCP/IP??UNIX??(BSD4.1)
  • 1982?,TCP/IP??NCP?ARPAnet??
  • 1983?,?????????(DNS)
  • 1991?,WWW???

4
TCP/IP OSI Model
5
2. Berkeley Socket Basics
  • TCP/IP in UNIX
  • BSD4.1
  • socket
  • connection
  • lt??????????????????????gt
  • socket
  • lt????????????gt
  • socket????

6
??????
  • ???????
  • ??????????????
  • ?????(????)
  • ?????????????

7
Three Types of Sockets
  • ????(SOCK_STREAM)
  • ????????????
  • ??TCP??
  • ??????(SOCK_DGRAM)
  • ?????
  • ??UDP??
  • ?????(SOCK_RAW)
  • ????????IP?ICMP????

8
???????
9
Server?????
  • ?????
  • ???????????
  • ??Client????????socket??
  • ?Client?????????
  • ??Client??socket????????,?????????????,?BBS
    Server
  • ??socket???Client????,?Time Server
  • ????????Client???

10
Client?????
  • ?????
  • ?????Server?,??socket??
  • ?Server?????????
  • ??Server??socket????????,??????
  • ??socket???Server??????
  • ????????Client???

11
??????
12
UDP???????
  • ???????????,???UDP
  • ?????
  • ??????????
  • ????Client
  • ???????????
  • ???????,?????????
  • ??ICQ?????

13
????????
  • socket()?????
  • UDP Server?????listen?accept
  • UDP?????sendto/recvfrom??
  • TCP?????connect/accept???UDP?sendto/recvfrom????
    ?????????
  • UDPshutdown????

14
Byte order
  • ??????(NBO,Network Byte Order)
  • ?????????,???????
  • ??????(HBO,Host Byte Order)
  • ?????HBO???,?CPU????
  • Motorola 68k??,HBO?NBO??
  • Intel x86??,HBO?NBO??

15
Byte Ordering Routines
  • Convert values between host and network byte
    order
  • include ltnetinet/in.hgt
  • uint32_t htonl(uint32_t hostlong)
  • uint16_t htons(uint16_t hostshort)
  • uint32_t ntohl(uint32_t hostlong)
  • uint16_t ntohs(uint16_t hostshort)

16
3. System Calls Routines
17
socket system call
  • header file
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • socket creates an endpoint for communication and
    returns a descriptor.
  • int socket(int domain, int type, int protocol)

18
socket system call (contd)
  • domain parameter
  • Specifies a communication domain, that is,
    selects a protocol family, such as
    PF_UNIX(PF_LOCAL), PF_INET, PF_IPX ...
  • type parameter
  • Specifies the communication semantics. Three main
    types are SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
  • protocol parameter
  • usually 0 (default).

19
bind system call
  • bind binds a name to a socket
  • int bind(int sockfd, struct sockaddr my_addr,
    socklen_t addrlen)
  • struct sockaddr ?
  • struct sockaddr
  • sa_family_t sa_family
  • char sa_data14
  • -gtstruct sockaddr_un
  • -gtstruct sockaddr_in

20
bind system call (contd)
  • struct sockaddr_un
  • struct sockaddr_un
  • sa_family_t sun_family / AF_UNIX /
  • char sun_path108 /pathname /

21
bind system call (contd)
  • struct sockaddr_in
  • struct sockaddr_in
  • sa_family_t sin_family
  • unsigned short int sin_port
  • struct in_addr sin_addr
  • unsigned char sin_zero8
  • struct in_addr
  • __u32 s_addr

22
inet_aton inet_ntoa
  • Internet address manipulation routines
  • int inet_aton(const char cp, struct in_addr
    inp)
  • char inet_ntoa (struct in_addr in)
  • inet_ntoa???32??????IP??????????IP?????
  • inet_addr An obsolete interface to inet_aton
  • inet_addr_t inet_addr (const char cp)

23
const INADDR_ANY
  • An unsigned long int value.
  • When INADDR_ANY is specified in the bind call the
    socket will be bound to all local interfaces.

24
listen system call
  • listen listen for connections on a socket
  • int listen(int s, int backlog)
  • ?????socket
  • ???????bind()??,??????????????
  • ????
  • ????Client??????????????,??Server????accept???????
    ?
  • backlog???????????,??Server????accept?????????????
    ?,???????????

25
accept system call
  • accept()?????????,????
  • int accept(int sockfd,struct sockaddr addr,int
    addrlen)
  • accept???????,?????????
  • sockfd ??(??)?socket???
  • ????,??????socket???(connected socket
    descriptor)?????????????????Client????
  • addr???????????????????,????IP?????

26
connect system call
  • connect initiate a connection on a socket
    (connect to a server).
  • int connect(int sockfd, struct sockaddr
    servaddr, int addrlen)
  • ???socket
  • servaddr?????????,Server?IP??????????????

27
send/recv
  • send/recv connection-oriented
  • int send(int s, const void msg, size_t len, int
    flag)
  • int recv(int s, void buf, size_t len, int flag)
  • ?write/read??
  • ssize_t write(int fd, const void buf, size_t
    count)
  • ssize_t read(int fd, void buf, size_t count)
  • flag
  • send MSG_OOB, MSG_DONTROUTE, MSG_DONTWAIT,
    MSG_NOSIGNAL, ...
  • recv MSG_OOB, MSG_PEEK, MSG_WAITALL,
    MSG_NOSIGNAL, ...

28
sendto/recvfrom
  • sendto/recvfrom
  • int sendto(int s, const void msg, size_t len,
    int flags, const struct sockaddr to, socketlen_t
    tolen)
  • int recvfrom(int s, void buf, size_t len, int
    flags, struct sockaddr from, socklen_t fromlen)

29
close shutdown
  • close
  • int close(int sockfd)
  • shutdown
  • int shutdown(int sockfd, int how)
  • how SHUT_RD, SHUT_WR, SHUT_RDWR
  • shutdown???TCP??????,close????????????

30
4. Examples
  • ????socket C/S??
  • ???socket C/S??

31
5. Network information
  • Host name and address conversions
  • Service name and port number conversions

32
Host name and address conversions
  • Domain Name System

33
Host name and address conversions (contd)
  • Files
  • /etc/resolv.conf, /etc/hosts
  • Functions
  • include ltnetdb.hgt
  • struct hostent gethostbyname(const char name)
  • struct hostent gethostbyaddr(const char addr,
    size_t len, int type)
  • struct hostent
  • struct hostent
  • char h_name / official name of host /
  • char h_aliases / alias list /
  • int h_addrtype / host address type /
  • int h_length / length of address /
  • char h_addrlist / list of addresses /

34
Service name and port number conversions
  • File
  • /etc/services
  • Functions
  • include ltnetdb.hgt
  • struct servent getservbyname(const char name,
    const char proto)
  • struct servent getservbyport(int port, const
    char proto)
  • struct servent
  • struct servent
  • char s_name / official service name /
  • char s_aliases / alias list /
  • int s_port / port number /
  • char s_proto / protocol to use /

35
Review
  • Berkley Socket????
  • ??????
  • ??/??socket??????
  • ????

36
??????
  • socket
  • bind
  • connect
  • listen
  • accept
  • recv, recvfrom
  • send, sendto
  • close, shutdown

37
??/??socket??????
  • gethostbyaddr, gethostbyname
  • getservbyname, getservbyport
  • getsockname, getpeername
  • getsockopt, setsockopt
  • fcntl/ioctl

38
????
  • IP????
  • inet_aton()
  • inet_ntoa()
  • ??????
  • htons()--"Host to Network Short"
  • htonl()--"Host to Network Long"
  • ntohs()--"Network to Host Short"
  • ntohl()--"Network to Host Long"

39
6. Multi-client
  • Multi-client, n servers?
  • Iterative server
  • Concurrent server
  • Multi-process/multi-thread
  • I/O Models
  • Block mode
  • Non-block mode
  • I/O multiplexing
  • Signal-driven I/O
  • Asynchronous I/O

40
Multi-process server
41
I/O Models
  • Block model
  • default
  • Non-block model
  • I/O multiplexing
  • Signal-driven I/O
  • Asynchronous I/O

42
Non-block mode
  • ???????????
  • errno - EWOULDBLOCK
  • ??????
  • int flags
  • if ( (flagsfcntl(sock_fd, F_GETFL, 0)) lt 0)
  • err_sys()
  • flags O_NONBLOCK
  • if ( fcntl(sock_fd, F_SETFL, flags) lt 0)
  • err_sys()

43
Signal-driven I/O
  • ????????
  • ????
  • SIGURG, SIGIO
  • ??
  • fcntl(conn_fd, F_SETOWN, getpid())
  • sigaction

44
I/O multiplexing
  • ????
  • ????????????,????????(?select),???????????????????
    I/O????,??????????????????I/O.

45
select
  • select synchronous I/O multiplexing.
  • include ltsys/select.hgt
  • int select(int n, fd_set readfds, fd_set
    writefds, fd_set exceptfds, struct timeval
    timeout)
  • FD_ZERO(fd_set set)
  • FD_SET(int fd, fd_set set)
  • FD_CLR(int fd, fd_set set)
  • FD_ISSET(int fd, fd_set set)

46
(No Transcript)
47
(No Transcript)
48
7. Using inetd/xinetd
  • The disadvantages of the old model
  • inetd/xinetd
  • Internet service daemon (also referred as
    super-server).
  • /etc/inetd.conf or /etc/xinetd.conf

49
(No Transcript)
50
xinetd???????
  • xinetd?????/etc/xinetd??????(?????/etc/inetd??),??
    ???????????????????????????,???????select()???????
    ??
  • ????????bind(),?????????????/etc/xinetd???????????
    ??
  • ???TCP????????listen(),??????????UDP???,?????????
  • ????????,????select()?????????????
  • ?select()??TCP???,???accept()??????????UDP,???????
    ??

51
xinetd???????(?)
  • xinetd??fork()?????,???????????
  • ????????????,????????????????????TCP?accept()?????
    ?,??UDP???????????????????dup()??,??????????0?1?2,
    ??????????????????????,??????????
  • ?????/etc/xinetd???????,????root??,??????setuid?se
    tgid???ID??ID???????????
  • ??TCP???,?????????????????????????????select()??,?
    ???????????
  • ??????????????????,????????????????????

52
Example
  • ?xinetd??????
  • hello.c

53
8. RPC
  • RPC
  • Remote Procedure Call
  • IPC vs. RPC
  • Explicit vs. implicit network programming
  • RPC-gtDistributed Object (CORBA, RMI, etc.)

54
(No Transcript)
55
(No Transcript)
56
Review
Write a Comment
User Comments (0)
About PowerShow.com