cpts 360 system programming unit 17 network ipc sockets
play

CptS 360 (System Programming) Unit 17: Network IPC (Sockets) Bob - PowerPoint PPT Presentation

Unit 17: Network IPC (Sockets) CptS 360 (System Programming) Unit 17: Network IPC (Sockets) Bob Lewis School of Engineering and Applied Sciences Washington State University Spring, 2020 Bob Lewis WSU CptS 360 (Spring, 2020) Unit 17: Network


  1. Unit 17: Network IPC (Sockets) CptS 360 (System Programming) Unit 17: Network IPC (Sockets) Bob Lewis School of Engineering and Applied Sciences Washington State University Spring, 2020 Bob Lewis WSU CptS 360 (Spring, 2020)

  2. Unit 17: Network IPC (Sockets) Motivation ◮ Processes need to talk to each other across a network. ◮ Lots of servers depend on networked IPC. Bob Lewis WSU CptS 360 (Spring, 2020)

  3. Unit 17: Network IPC (Sockets) References ◮ Stevens & Rago Ch. 16 ◮ man pages ◮ Stones & Matthew “Beginning Linux Programming” Bob Lewis WSU CptS 360 (Spring, 2020)

  4. Unit 17: Network IPC (Sockets) Introduction ◮ So far, our IPC has been within a single system. ◮ This reflects POSIX’s mainframe origins. ◮ How about between different computers? ◮ Network transfers “packets” of data between computers. ◮ Packets contain ◮ format ◮ routing information ◮ data ◮ 4.4BSD devised “sockets” ◮ “open a socket” is now part of the culture (e.g., in the show “24”) Bob Lewis WSU CptS 360 (Spring, 2020)

  5. Unit 17: Network IPC (Sockets) socket(2) ◮ creates a socket ◮ args specify ◮ domain kind of communication, esp. address format ◮ type of connection ◮ protocol when domain and type map to multiple protocols (usually the default is what you want, so set to 0) ◮ This call does not make the socket visible outside of your process. (That comes later.) Bob Lewis WSU CptS 360 (Spring, 2020)

  6. Unit 17: Network IPC (Sockets) Socket Domains ◮ AF_INET IPv4 – (allows 32-bit address) the Internet as we know it ◮ AF_INET6 IPv6 – (allows 128-bit address) the Internet as we would eventually like to know it one of these days eventually, maybe (Actually, after 17 years it’s the default on many mobile phone systems and it’s slowly becoming reality.) ◮ Are you using it? https://test-ipv6.com ◮ How many places are using it? https://www.google.com/intl/en/ipv6 ◮ AF_UNIX (aka AF_LOCAL ) intramachine ◮ AF_UNSPEC “any” domain (not sure how this works) Bob Lewis WSU CptS 360 (Spring, 2020)

  7. Unit 17: Network IPC (Sockets) Socket Types ◮ SOCK_DGRAM (“datagram”) fixed length, connectionless, unreliable ◮ SOCK_STREAM sequenced, reliable, bidirectional, “connection oriented” ◮ SOCK_SEQPACKET fixed-length, sequenced, reliable, “connection oriented” ◮ SOCK_RAW datagram interface to IP Bob Lewis WSU CptS 360 (Spring, 2020)

  8. Unit 17: Network IPC (Sockets) Byte Ordering ◮ big- vs. little-endian remember mnemonic for which is which. ◮ byteorder(3) Bob Lewis WSU CptS 360 (Spring, 2020)

  9. Unit 17: Network IPC (Sockets) Address Formats ◮ determined by domain ◮ generic sockaddr struct : struct sockaddr { sa_family_t sa_family; char sa_data[]; ... } (another approach to polymorphism) ◮ (see /usr/include/netinet/in.h ) Bob Lewis WSU CptS 360 (Spring, 2020)

  10. Unit 17: Network IPC (Sockets) Address Lookup: Host Name ◮ gethostent(2) browse the list of all known hosts, just like getpwent(2) ◮ gethostbyaddr(2) ◮ gethostbyname(2) Bob Lewis WSU CptS 360 (Spring, 2020)

  11. Unit 17: Network IPC (Sockets) Address Lookup: Network, Protocol, Service ◮ getnetent(2) , getnetbyaddr(2) , etc. ◮ getprotoent(2) , getprotobyname(2) , etc. ◮ getservent(2) , getservbyname(2) , etc. ◮ getaddrinfo(2) ◮ replaces gethostbyname*(3) and gethostbyaddr*(3) ◮ (see demos/0 addrinfo.[ch] ) Bob Lewis WSU CptS 360 (Spring, 2020)

  12. Unit 17: Network IPC (Sockets) Associating Addresses with Sockets ◮ bind(2) ◮ getsockname(2) inverse of bind(2) ◮ getpeername(2) peer is socket address at the other end of the socket Bob Lewis WSU CptS 360 (Spring, 2020)

  13. Unit 17: Network IPC (Sockets) Connection Establishment ◮ If we’re dealing with a connection-oriented network service (such as ?)... ◮ connect(2) ◮ (see demos/6 sr connect retry.[ch] ) Bob Lewis WSU CptS 360 (Spring, 2020)

  14. Unit 17: Network IPC (Sockets) Data Transfer If there’s a connection and all we want to do is send data, we can use ◮ read(2) ◮ write(2) but to handle ◮ connectionless transfer ◮ multiple clients ◮ send out-of-band data ◮ other options we need ... Bob Lewis WSU CptS 360 (Spring, 2020)

  15. Unit 17: Network IPC (Sockets) Data Transfer: Sending ◮ send(2) ◮ connected only ◮ note flags argument ◮ sendto(2) ◮ works with connectionless transfer ◮ sendmsg(2) ◮ send from multiple buffers (like writev(2) ) Bob Lewis WSU CptS 360 (Spring, 2020)

  16. Unit 17: Network IPC (Sockets) Data Transfer: Receiving ◮ recv(2) ◮ connected only ◮ recvfrom(2) ◮ works with connectionless transfer ◮ recvmsg(2) ◮ send from multiple buffers (like readv(2) ) Bob Lewis WSU CptS 360 (Spring, 2020)

  17. Unit 17: Network IPC (Sockets) Server Chronology I gethostname() hostname getaddrinfo() service sockaddr ("sa") socket(sa->fam, ...) sockfd bind() connected protocol connectionless protocol ("stream") ("datagram") Bob Lewis WSU CptS 360 (Spring, 2020)

  18. Unit 17: Network IPC (Sockets) Server Chronology II connected protocol connectionless protocol ("stream") ("datagram") listen() recvfrom() sockaddr "kernel, we're a server" sendto() accept() fd_client read() write() send*() recv*() (see demos/5 sr stream server.c ) (see demos/8 sr datagram server.c ) Bob Lewis WSU CptS 360 (Spring, 2020)

  19. Unit 17: Network IPC (Sockets) Client Chronology hostname getaddrinfo() getaddrinfo() and service sockaddr ("sa") socket(sa->fam, ...) connected connectionless sockfd connect() recvfrom() read() sendto() write() send*() recv*() (see demos/7 sr stream client.c ) (see demos/9 sr datagram client.c ) Bob Lewis WSU CptS 360 (Spring, 2020)

  20. Unit 17: Network IPC (Sockets) Out-of-Band Data ◮ sent ahead of any queued data ◮ TCP allows one byte ◮ UDP doesn’t allow ◮ recipent gets SIGURG Bob Lewis WSU CptS 360 (Spring, 2020)

  21. Unit 17: Network IPC (Sockets) Asynchronous vs. Nonblocking I/O ◮ asynchronous ◮ start now, return immediately ◮ I’ll check later. ◮ non-blocking ◮ start now ◮ return immediately if I/O can’t be completed ◮ I’m not going to bother checking. Bob Lewis WSU CptS 360 (Spring, 2020)

Recommend


More recommend