正在加载图片...
Generic Address Structure Generic Address Structure Not all address families define endpoints that fit into the Predefined structure sockaddr in struct sockadd Confusion often arises in practice because tl structure accommodates address in the AFINETfamily u char sin len /*length of To keep programs portable and maintainable, TCP/IP cod u char sin family; /*family of should not use the sockaddr structure in declaration. u short sin port; /*protocol port struct in addr sin addr char sin zero [8]; /*not used (set to Each protocol family that uses sockets defines the exact representation of its endpoint addresses The socket sof tware provides corresponding structure It never needs to use the sockaddrstructure Functions in the socket API Generic Address structure WSAStartup Key points: Programs using windows Sockets must call When representing a TCP/IP communication Startup before using sockets endpoint, an application program uses structure sockaddr in, which contains both Two arguments n IP address and a protocol port number First to specify the version of win Sock Programmers must be careful when writing programs that use a mixture of protocols Integer in hexadecimal, e.g. Ox102 means version because some non-TCP/IP endpoint Second to return information about actual addresses require a larger structure. version of wins Points to a WSADA TA structure into which os writes version information Functions in the socket API: Functions in the socket API WSACleanup Creating a Socket Once application finishes using and closing desc=socket(protofamily,type,proto); sockets, the application calls to deallocate all data structures and socket bindings. Application calls socket function A program usually calls WSACleanup only OS returns descriptor for socket tor valid until ap Common: protofamily= PF_INET type= SOCK_STREAM, SOCK_DGRAM SOCK RAW6 哈工大计算机学院 李全龙 Network Application Development Socket Programming 31 Generic Address Structure „ Not all address families define endpoints that fit into the sockaddr structure. „ Confusion often arises in practice because the sockaddr structure accommodates address in the AF_INET family. „ To keep programs portable and maintainable, TCP/IP code should not use the sockaddr structure in declaration. „ Instead, sockaddr should be used only as an overlay, and code should reference only the sa_family field in it. „ Each protocol family that uses sockets defines the exact representation of its endpoint addresses. „ The socket software provides corresponding structure declarations. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 32 Generic Address Structure struct sockaddr_in { u_char sin_len; /*length of address*/ u_char sin_family; /*family of address*/ u_short sin_port; /*protocol port number*/ struct in_addr sin_addr; /*IP address*/ char sin_zero[8]; /*not used(set to zero)*/ } „ Predefined structure sockaddr_in: „ An application that uses TCP/IP protocols exclusively can use structure sockaddr_in exclusively, „ It never needs to use the sockaddr structure. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 33 Generic Address Structure „ Key points: „When representing a TCP/IP communication endpoint, an application program uses structure sockaddr_in, which contains both an IP address and a protocol port number. „ Programmers must be careful when writing programs that use a mixture of protocols because some non-TCP/IP endpoint addresses require a larger structure. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 34 Functions in the socket API: WSAStartup „ Programs using Windows Sockets must call WSAStartup before using sockets. „ Two arguments: „ First to specify the version of WinSock requested. „ Integer in hexadecimal, e.g. 0x102 means version 2.1 „ Second to return information about actual version of WinSock „ Points to a WSADATA structure into which OS writes version information 哈工大计算机学院 李全龙 Network Application Development Socket Programming 35 Functions in the socket API: WSACleanup „ Once application finishes using and closing sockets, the application calls to deallocate all data structures and socket bindings. „ A program usually calls WSACleanup only when it is completely finished and ready to exit. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 36 Functions in the socket API: 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);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有