Chapter 2: Socket Programming Chapter 2: Socket Programming Chapter goal Chapter goal: Program interface to Program interface to protocol tocol Socket API a Socket API Socket programming model Socket programming model Interacting with Protocol Loosely specified protocol Software software interface a Client or server uses transp TCP/IP protocol software is part of the protocols computers system sof two TCP/IP was designed to operate in a multi-vendor Protocol software inside oS environment Applications outside OS TCP/iP does not define details of API TCP/ IP andφp Mechanism needed to bridge the two that use it has been Called Application Program Interface application software interfaces wit (API) They only suggest the required functionality, and allow an aPI gners to choose the details when creating Advantages and disadvantages of Loose specification API functionality Advantages API must support Different OS select own implementation. Disadvantage Designers can make the interface details Vait for a connection(server Less portable across machine In practice, only a few APIs have developed for use with TCP/IP. Berkeley UNTX: socket interface, or bort communication AT&T, System V UNIX: TLI Release local resources when communication finishes Windows: windows sockets interface WINSOCK
1 哈工大计算机学院 李全龙 Network Application Development Socket Programming 1 Chapter 2: Socket Programming Chapter goal: Program interface to protocol Socket API Socket programming model 哈工大计算机学院 李全龙 Network Application Development Socket Programming 2 Chapter 2: Socket Programming Chapter goal: Program interface to protocol Socket API Socket programming model 哈工大计算机学院 李全龙 Network Application Development Socket Programming 3 Interacting with Protocol Software Client or server uses transport protocols Protocol software inside OS Applications outside OS Mechanism needed to bridge the two Called Application Program Interface (API) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 4 Loosely specified protocol software interface TCP/IP protocol software is part of the computer’s system software. TCP/IP was designed to operate in a multi-vendor environment. TCP/IP does not define details of API The interface between TCP/IP and applications that use it has been loosely specified. TCP/IP standards do not specify the details of how application software interfaces with TCP/IP protocol software They only suggest the required functionality, and allow system designers to choose the details when creating an API 哈工大计算机学院 李全龙 Network Application Development Socket Programming 5 Advantages and disadvantages of Loose specification Advantages: Flexibility and tolerance. Different OS select own implementation. Disadvantages: Designers can make the interface details different for each OS. Less portable across machines. In practice, only a few APIs have been developed for use with TCP/IP. Berkeley UNIX: socket interface, or sockets AT&T, System V UNIX: TLI Windows: windows sockets interface, WINSOCK 哈工大计算机学院 李全龙 Network Application Development Socket Programming 6 API functionality API must support: Allocate local resources for communication Specify local and remote communication endpoints Initiate a connection (client side) Wait for a connection (server side) Send or receive data Determine when data arrives Generate urgent data Terminate a connection gracefully Handle connection termination from the remote site Abort communication Handle error condition or a connection abort Release local resources when communication finishes
Conceptual interface specification Implementation of an API The TCP/IP standards do not leave All implementations of a particular API appear the ithout any guidance. or functions)that ar They specify a conceptual interface: a set of an call to establish communication or send procedures and functions nd receive data The conceptual interface defined by TCP/IP In practice, the implementation of the API depend standards does not specify data representations or programming details or it might reside in a region of memory devoted te It merely provides an example of one possible interface that an OS can offer to application Win95/98 uses a shared lib scheme known as programs that use TCP/IP. Implementation of an API Implementation of an API Win95/98 a windows NT/2000/XP uses a hybrid sche that includes both a dll and resident ce App programs @- LL containing socket interface procedures DLL containing TCP/IP software]-TCPnP functions ing socket interface procedures.-Socket API TCP/IP functions Operating System Function Operating System Functions yo function Two basic approaches to Implementation of an API network communication Other implementations are possible. OS with no DLL support, all the procedures in API are The designer invents entirely new procedures calls that applications use to es fhe socket API without knowing how the aPI is Windows uses this method The programmer makes procedure calls which may a The designer attempts to use convention he application I/O calls to access TCP/IP procedures in DLL, or procedures in OS. Unix uses this metho App programs Socket API, Operating System Function
2 哈工大计算机学院 李全龙 Network Application Development Socket Programming 7 Conceptual interface specification The TCP/IP standards do not leave implementors without any guidance. They specify a conceptual interface: a set of procedures and functions. The conceptual interface defined by TCP/IP standards does not specify data representations or programming details; It merely provides an example of one possible interface that an OS can offer to application programs that use TCP/IP. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 8 Implementation of an API All implementations of a particular API appear the same to programmers: the API merely consists of a set of procedures ( or functions) that an application program can call to establish communication or send and receive data. In practice, the implementation of the API depends on the underlying system. Sharing API code might reside in the computer’s OS, or it might reside in a region of memory devoted to shared libraries. Win95/98 uses a shared lib scheme known as Dynamic Linked Library (DLL). 哈工大计算机学院 李全龙 Network Application Development Socket Programming 9 Implementation of an API Win95/98 : Operating System Functions DLL containing TCP/IP software DLL containing socket interface procedures App1 App2 …… Appn I/O functions TCP/IP functions Socket API App programs 哈工大计算机学院 李全龙 Network Application Development Socket Programming 10 Implementation of an API Windows NT/2000/XP uses a hybrid scheme that includes both a DLL and resident code. Operating System Functions DLL containing socket interface procedures App1 App2 …… Appn TCP/IP functions and I/O functions Socket API App programs 哈工大计算机学院 李全龙 Network Application Development Socket Programming 11 Implementation of an API Other implementations are possible. OS with no DLL support, all the procedures in API are part of OS. A programmer can create an application program that uses the socket API without knowing how the API is implemented. The programmer makes procedure calls which may invoke procedures that are linked into the application, procedures in DLL, or procedures in OS. Operating System Functions App1 App2 …… Appn Socket API, TCP/IP functions and I/O functions App programs 哈工大计算机学院 李全龙 Network Application Development Socket Programming 12 Two basic approaches to network communication The designer invents entirely new procedures calls that applications use to access TCP/IP. Windows uses this method. The designer attempts to use conventional I/O calls to access TCP/IP. Unix uses this method
The basic I/o functions in ANSI C Chapter 2: Socket Programming open: prepare a device or file for input or output Chapter goal: erations close: terminate use of a previously opened device Program interface to read: obtain data from an input device or file, and a Socket API write: transmit data from lication program's nory to an output device Socket programming model Iseek: move to a specific position in a file or device octl: control a device or software used to access it Socket API What is" socke↑"? Originally designed A socket is a virtual connection For BSD UNIX-Berkley Socket Interface between two applications To use with TCP/IP protocols Now Using a socket, two processes can communicate with each other The socket is the major communication Microsoft chose the socket interface as the primary API tool for Internet applications In Ms windows, WINSOCK A socket is bi-directional(full-duplex transmission A socket can be created dynamically Socket as a virtual connection between two processes Specifying a protocol interface Host A o support TCP/IP communicatio (nirmal cowe crion) Information Hiding ge neral ndsuese parameters to make TcP/IP communication The socket interface provides generalized functions that support network communication using many Socket functions refer to all TCP /IP protocols as a (plysical connection)
3 哈工大计算机学院 李全龙 Network Application Development Socket Programming 13 The basic I/O functions in ANSI C open: prepare a device or file for input or output operations. close: terminate use of a previously opened device or file. read: obtain data from an input device or file, and place it in the application program’s memory. write: transmit data from the application program’s memory to an output device or file. lseek: move to a specific position in a file or device. ioctl: control a device or software used to access it. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 14 Chapter 2: Socket Programming Chapter goal: Program interface to protocol Socket API Socket programming model 哈工大计算机学院 李全龙 Network Application Development Socket Programming 15 Socket API Originally designed For BSD UNIX-Berkley Socket Interface To use with TCP/IP protocols Now Industry standard Available on many operating systems Microsoft chose the socket interface as the primary API. In MS Windows, WINSOCK 哈工大计算机学院 李全龙 Network Application Development Socket Programming 16 What is “socket”? • A socket is a virtual connection between two applications • Using a socket, two processes can communicate with each other • The socket is the major communication tool for Internet applications • A socket is bi-directional (full-duplex) transmission • A socket can be created dynamically 哈工大计算机学院 李全龙 Network Application Development Socket Programming 17 Network Socket as a virtual connection between two processes (physical connection) Host B Process 2 Host A Process 1 Network Adapter Card Information Hiding Socket connection (virtual connection) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 18 Specifying a protocol interface The designers must choose one of two broad approaches: Define functions specifically to support TCP/IP communication. Define functions that support network communication in general, and use parameters to make TCP/IP communication in special case. The socket interface provides generalized functions that support network communication using many possible protocols. Socket functions refer to all TCP/IP protocols as a single protocol family. The functions allow the programmer to specify the type of service required rather than the name of a specific protocol
Socket abstraction Socket abstraction In most OS, an application that needs to perform I/0 asks the Os to open a file. a The socket interface adds a new OS responds by creating a file descriptor. abstraction for network communication -socket nternal data structure for file 0 Os abstraction(not hardware rnal data structure for Internal data structure for file 2 Internal data structure for file 3 Persists only while application runs a Referenced by a socket descriptor Socket Descriptor Creating a Socket a Small integer desc= socket(protofamily, type, proto) m One per active socket a Used in all operations on socket Application calls socket function a Generated by OS when socket created OS returns descriptor for socket Only meaningful to application/process Descriptor valid until application closes hat owns socket socket or exits In UNIX, integrated with file descriptors Common: protofamily PF_INET. The Windows OS keeps a separate table of type =SOCK_STREAM, SOCK_DGRAM socket descriptors for each process or s Socket Functionality Using sockets Socket completely general aA socket used by a server: passive socket Can be used A socket used by a client: active socket By client The difference lies in how applications use server them With a co transport protocol With a CL transport protocol a The sockets are created the same way initially To send data receive data or both Large set of operations
4 哈工大计算机学院 李全龙 Network Application Development Socket Programming 19 Socket abstraction In most OS, an application that needs to perform I/O asks the OS to open a file. OS responds by creating a file descriptor. . 0: Internal data structure for file 0 . 1: Internal data structure for file 1 . 2: Internal data structure for file 2 . 3: Internal data structure for file 3 . . . Descriptor table (one per process) Operating System 哈工大计算机学院 李全龙 Network Application Development Socket Programming 20 Socket abstraction The socket interface adds a new abstraction for network communication-socket. OS abstraction (not hardware) Created dynamically Persists only while application runs Referenced by a socket descriptor 哈工大计算机学院 李全龙 Network Application Development Socket Programming 21 Socket Descriptor Small integer One per active socket Used in all operations on socket Generated by OS when socket created Only meaningful to application/process that owns socket In UNIX, integrated with file descriptors The Windows OS keeps a separate table of socket descriptors for each process 哈工大计算机学院 李全龙 Network Application Development Socket Programming 22 Creating a Socket Application calls socket function OS returns descriptor for socket Descriptor valid until application closes socket or exits Common: protofamily = PF_INET, type = SOCK_STREAM,SOCK_DGRAM or SOCK_RAW desc = socket(protofamily,type,proto); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 23 Socket Functionality Socket completely general Can be used By client By server With a CO transport protocol With a CL transport protocol To send data, receive data, or both Large set of operations 哈工大计算机学院 李全龙 Network Application Development Socket Programming 24 Using sockets A socket used by a server: passive socket A socket used by a client: active socket The difference lies in how applications use them The sockets are created the same way initially
Data structures for socket Data structures for socket The easiest way to understand the abstraction ocket descriptor table (one per process Data structure for a socket When an application calls socket, the Os allocate a new data structure to hold the information needed for communicatio Fill in a new entry in the process' socket 0123 Pointers to other socket tructures descriptor table with a pointer to the data The system keeps a separate socket Operating System descriptor table for each process The application that creates the socket must make Threads in the process share the table. I procedures calls to fill in information in the socket data st Specifying an endpoint address Specifying an endpoint address ost A ocket connection (viral connection) ss and protocol por Other protocol families define their endpoint addresses in other accommodate multiple families of protocols, socket abstraction ock Adapter Card It allows each protocol family to specify endpoints however it likes. The socket abstraction defines an address family for each type of rotocol family can use one or more address families to define When a socket is created it does not have addre Bedore using a socket, must specify one or both end onstant AF INET. Both PF_INET and aF_ INET have the same value(2) Generic Address structure Generic Address structure Application may need to manipulate protocol In practice, the socket API provides declarations of addresses without knowing the details of how every predefined data types for address endpoints. protocol family defines its address representation. To accommodate such programs, the socket system structure es a generalized format that all endpoint struct sockaddr (address family, endp addr n that family) u char sa⊥en /*length of address*/ u char sa family; /*family of address*/ char sa data[141 preassigned address type
5 哈工大计算机学院 李全龙 Network Application Development Socket Programming 25 Data structures for socket The easiest way to understand the abstraction When an application calls socket, the OS allocate a new data structure to hold the information needed for communication Fill in a new entry in the process’ socket descriptor table with a pointer to the data structure. The system keeps a separate socket descriptor table for each process Threads in the process share the table. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 26 Data structures for socket The application that creates the socket must make additional procedures calls to fill in information in the socket data structure before the socket can be used. . 0: . 1: . Pointers to other socket structures 2: . 3: . . . Socket descriptor table (one per process) Operating System family:PF_INET service:sock_stream local IP: remote IP: . . . local port: remote port: Data structure for a socket 哈工大计算机学院 李全龙 Network Application Development Socket Programming 27 Specifying an endpoint address When a socket is created it does not have addresses Before using a socket, must specify one or both end addresses. Network (physical connection) Host B Process 2 Host A Process 1 Network Adapter Card Information Hiding Socket connection (virtual connection) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 28 Specifying an endpoint address TCP/IP defines communication endpoint to consists of an IP address and protocol port number. Other protocol families define their endpoint addresses in other ways. To accommodate multiple families of protocols, socket abstraction does not specify how to define endpoint addresses nor does it define a particular protocol address format. It allows each protocol family to specify endpoints however it likes. The socket abstraction defines an address family for each type of address. A protocol family can use one or more address families to define address representation. TCP/IP protocols all use a single address rep, with the address family denoted by the symbolic constant AF_INET. Both PF_INET and AF_INET have the same value (2). 哈工大计算机学院 李全龙 Network Application Development Socket Programming 29 Generic Address Structure Application may need to manipulate protocol addresses without knowing the details of how every protocol family defines its address representation. To accommodate such programs, the socket system defines a generalized format that all endpoint addressed use. (address family, endpoint address in that family) Contains a constant that denotes one of the preassigned address type Contains an endpoint address using the standard representation for the specific address type 哈工大计算机学院 李全龙 Network Application Development Socket Programming 30 Generic Address Structure In practice, the socket API provides declarations of predefined data types for address endpoints. The most general structure is known as a sockaddr structure. struct sockaddr { u_char sa_len; /*length of address*/ u_char sa_family; /*family of address*/ char sa_data[14]; /*address itself*/ }