How do we Communicate? Introduction to Unix Network Send a mail from Alice to Bob Bob Programming Alice in Champaign, Bob in Hollywood Example: US Postal Service Reference: Stevens Unix Alice Network Programming Hollywood, California Champaign, Illinois 8/30/06 UIUC - CS/ECE 438, Fall 2006 2 What does Alice do? What does Bob do? Alice Alice 200 Cornfield Rd. 200 Cornfield Rd. Champaign, IL 61820 Champaign, IL 61820 Bob Bob 100 Santa Monica Blvd. 100 Santa Monica Blvd. Hollywood, CA 90028 Hollywood, CA 90028 Bob’s address (to a mailbox) Install a mailbox Bob’s name – in case people share mailbox Receive the mail Postage – have to pay! Get rid of envelope Alice’s own name and address – in case Bob wants to return a Read the message message 8/30/06 UIUC - CS/ECE 438, Fall 2006 3 8/30/06 UIUC - CS/ECE 438, Fall 2006 4 What about sending a TCP/IP Two simplest networking packet? programs Alice Bob Very similar to Alice-mailing-to-Bob the sending process the receiving process Alice’s address: Bob’s address: Different terminologies 128.174.246.177 (IP addr) 216.52.167.132 (IP addr) Alice’s name: 12345 (port #) Bob’s name: 23456 (port #) Different technologies int main () { int main () { int sockfd; int sockfd, n; Suppose to be better (faster, more struct sockaddr_in bob_addr, alice_addr; struct sockaddr_in bob_addr, alice_addr; char mesg[100]; reliable, cheaper, …) bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_family = AF_INET; bob_addr.sin_port = 23456; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456; // do the same for alice_addr … sockfd = socket(AF_INET, SOCK_DGRAM, 0); sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(sockfd, &bob_addr, sizeof(bob_addr)); sendto(sockfd, “hi”, strlen(“hi”), 0, &bob_addr, sizeof(bob_addr)); n= recvfrom(sockfd, mesg, 100, 0, } &alice_addr, sizeof(alice_addr)); } 8/30/06 UIUC - CS/ECE 438, Fall 2006 5 8/30/06 UIUC - CS/ECE 438, Fall 2006 6 1
What are the problems? Direction and Principles Message may be lost Programming Network is congested learn to use Internet for Receiver is congested communication (with focus on Transport implementation of networking Message may be duplicated, corrupted Network concepts) Multiple messages: re-ordered Data Link learn to build network from ground Physical Concurrent connections up Principles and … Concepts 8/30/06 UIUC - CS/ECE 438, Fall 2006 7 8/30/06 UIUC - CS/ECE 438, Fall 2006 8 Network Programming with Sockets Sockets process sends/receives Reading: host or host or messages to/from its Stevens 2nd ed., Ch. 1-6 or 1st ed., Ch. 1-3, 6 server server socket socket analogous to controlled by app developer Sockets API: mailbox process process sending process relies A transport layer service interface socket socket on transport Introduced in 1981 by BSD 4.1 TCP with TCP with infrastructure which Internet buffers, buffers, Implemented as library and/or system calls brings message to variables variables Similar interfaces to TCP and UDP socket at receiving Can also serve as interface to IP (for super-user); process known as “raw sockets” 8/30/06 UIUC - CS/ECE 438, Fall 2006 9 8/30/06 UIUC - CS/ECE 438, Fall 2006 10 Outline Client-Server Model Asymmetric Communication Client-Sever Model Client sends requests Client TCP/UDP Overview Server sends replies Server/Daemon Addresses and Data Well-known name (e.g., IP address + port) Client Waits for contact Sockets API Server Processes requests, sends replies Client Example Client Initiates contact Waits for response Client 8/30/06 UIUC - CS/ECE 438, Fall 2006 11 8/30/06 UIUC - CS/ECE 438, Fall 2006 12 2
Client-Server Communication Socket Communication Model Service Model Concurrent: Server processes multiple clients’ requests Client process Server process simultaneously Sequential: Server processes only one client’s requests at a time Hybrid: 3-way Listening handshaking socket Server maintains multiple connections, but processes responses sequentially Client and server categories are not disjoint Client Connection socket socket A server can be a client of another server A server can be a client of its own client 8/30/06 UIUC - CS/ECE 438, Fall 2006 13 8/30/06 UIUC - CS/ECE 438, Fall 2006 14 TCP Connections TCP Service Reliable Data Transfer Transmission Control Protocol (TCP) Guarantees delivery of all data Service Exactly once if no catastrophic failures OSI Transport Layer Sequenced Data Transfer Guarantees in-order delivery of data Service Model If A sends M1 followed by M2 to B, B never receives M2 Reliable byte stream (interpreted by application) before M1 16-bit port space allows multiple connections on a Regulated Data Flow single host Monitors network and adjusts transmission appropriately Prevents senders from wasting bandwidth Connection-oriented Reduces global congestion problems Set up connection before communicating Data Transmission Tear down connection when done Full-Duplex byte stream 8/30/06 UIUC - CS/ECE 438, Fall 2006 15 8/30/06 UIUC - CS/ECE 438, Fall 2006 16 UDP Services UDP Services Unit of Transfer User Datagram Protocol Service Datagram (variable length packet) OSI Transport Layer Unreliable Provides a thin layer over IP No guaranteed delivery 16-bit port space (distinct from TCP Drops packets silently ports) allows multiple recipients on a Unordered single host No guarantee of maintained order of delivery Unlimited Transmission No flow control 8/30/06 UIUC - CS/ECE 438, Fall 2006 17 8/30/06 UIUC - CS/ECE 438, Fall 2006 18 3
Addresses and Data Byte Ordering Internet domain names Big Endian vs. Little Endian Human readable Little Endian (Intel, DEC): Variable length Least significant byte of word is stored in the lowest memory address Ex: sal.cs.uiuc.edu Big Endian (Sun, SGI, HP): IP addresses Most significant byte of word is stored in the lowest Easily handled by routers/computers memory address Network Byte Order = Big Endian Fixed length Allows both sides to communicate Somewhat geographical Must be used for some data (i.e. IP Addresses) Ex: 128.174.252.217 Good form for all binary data 8/30/06 UIUC - CS/ECE 438, Fall 2006 19 8/30/06 UIUC - CS/ECE 438, Fall 2006 20 Byte Ordering Functions Socket Address Structure 16- and 32-bit conversion functions (for platform IP address: independence) struct in_addr { Examples: in_addr_t s_addr; /* 32-bit IP address */ }; int m, n; TCP or UDP address: short int s,t; struct sockaddr_in { m = ntohl (n) net-to-host long (32-bit) translation short sin_family; /* e.g., AF_INET */ s = ntohs (t) net-to-host short (16-bit) translation ushort sin_port; /* TCP/UDP port */ struct in_addr; /* IP address */ n = htonl (m) host-to-net long (32-bit) translation t = htons (s) host-to-net short (16-bit) translation }; all but sin_family in network byte order 8/30/06 UIUC - CS/ECE 438, Fall 2006 21 8/30/06 UIUC - CS/ECE 438, Fall 2006 22 Address Access/Conversion Functions Structure: hostent All binary values are network byte ordered The hostent data structure (from /usr/include/netdb. h ) struct hostent* gethostbyname (const char* canonical domain name and aliases hostname); list of addresses associated with machine Translate English host name to IP address (uses DNS) also address type and length information struct hostent { char* h_name; /* official name of host */ struct hostent* gethostbyaddr (const char* char** h_aliases; /* NULL-terminated alias list */ addr, size_t len, int family); int h_addrtype /* address type (AF_INET) */ Translate IP address to English host name (not secure) int h_length; /* length of addresses (4B) */ char** h_addr_list; /* NULL-terminated address list */ #define h_addr h_addr_list[0];/* backward-compatibility */ char* inet_ntoa (struct in_addr inaddr); }; Translate IP address to ASCII dotted-decimal notation (e.g., “128.32.36.37”) 8/30/06 UIUC - CS/ECE 438, Fall 2006 23 8/30/06 UIUC - CS/ECE 438, Fall 2006 24 4
Recommend
More recommend