当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

哈尔滨工业大学:《网络应用开发》英文版 NAD-chapter2-6

资源类别:文库,文档格式:PDF,文档页数:11,文件大小:189.85KB,团购合买
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
点击下载完整版文档(PDF)

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*/ }

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 RAW

6 哈工大计算机学院 李全龙 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);

Functions in the socket API osesocke Functions in the socket API: bind closesocket Specify local endpoint address for a ket. closesocket Specify protocol port for a socket ents a reference count and deallocates the socket when the reference count reaches Specify local IP address for a socket There is no socket count for the threads within a Arguments Socket descriptor close 心 scriptor is deallocated for al‖ I threads in the Can use INADDR_ANy for any IP address bind(socket,localaddr,addrlen); Functions in the socket API: listen Functions in the socket API: accept When a socket is created it is neither .For tCp sockets active nor passive. a Used by connection-oriented servers .Used by server Prepares socket to accept incor Waits for next connection and returns new socket connections(in passive mode Original socket can accept additional connection To ensure that no request is lost, a server must pass listen two arguments New socket transfers data for the specific client Socket accept( listen(socket, queuesize)i Functions in the socket API: connect Two Purposes of the Connect Function function, which is called by Used by client lients. has two uses With connection. oriented transport, establishes a Performs a TCP connection transport connection to a specified server. With connectionless transport Fully specifies addresses for UDP records the server s address in the socket allowing the client to send many messages to the same server without specifying the destination address with each message

7 哈工大计算机学院 李全龙 Network Application Development Socket Programming 37 Functions in the socket API: closesocket „ closesocket „ Terminate use of socket „ Permanent „ If several processes share a socket, closesocket decrements a reference count and deallocates the socket when the reference count reaches zero. „ There is no socket count for the threads within a process „ If one thread in a process closes a socket, the descriptor is deallocated for all threads in the process. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 38 Functions in the socket API:bind „ bind „ Specify local endpoint address for a socket „ Specify protocol port for a socket „ Specify local IP address for a socket „ Arguments: „ Socket descriptor „ Endpoint address: sockaddr_in „ Can use INADDR_ANY for any IP address bind(socket,localaddr,addrlen); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 39 Functions in the socket API: listen „ listen „ When a socket is created, it is neither active nor passive. „ Used by connection-oriented servers „ Prepares socket to accept incoming connections (in passive mode) „ To ensure that no request is lost, a server must pass listen two arguments: „ Socket „ Size of queue listen(socket,queuesize); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 40 Functions in the socket API: accept newsock = accept(socket,caddr,caddrlen); „accept „For TCP sockets „Used by server „Waits for next connection and returns new socket „Original socket can accept additional connection request „New socket transfers data for the specific client 哈工大计算机学院 李全龙 Network Application Development Socket Programming 41 Functions in the socket API: connect „ connect „ Used by client „ Either „ Performs a TCP connection „ Fully specifies addresses for UDP connect(socket,saddr,saddrlen); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 42 Two Purposes of the Connect Function The connect function, which is called by clients, has two uses. With connection￾oriented transport, connect establishes a transport connection to a specified server. With connectionless transport, connect records the server’s address in the socket, allowing the client to send many messages to the same server without specifying the destination address with each message

Functions in the socket API send, sendto, sendmsg Format of msgstruct send,sendto,and sendmsg struct msgstruct Transfer outgoing data from application struct sockaddr *m saddr /*dest address* send(socket, data, length, flags) sendto(socket, data, length, flags struct a rights /*access rights* destaddr, addrlen)i int m length; /*size of access rights* Functions in the socket API recv, recvfrom, and recvmsg Summary of socket calls used with TCP a recv, recvfrom, and recvmsg WSAStartup initialize the socket lib(windows only) Transfer incoming data to application socket: create a descriptor for use in network connect: connect to a remote peer(client) recvfrom(socket, buffer, length, flags bind: bind a local IP address and protocol port number senderaddr, saddrlen) hister place the socket in passive mode and set the recvmsg(socket, msgstruct, flags) recv incoming data from a stream connection or the next incoming msg Summary of socket calls used with TCP Utility routines for integer conversion i recvfrom: TCP/IP specifies a standard representation for select sockers becomes read foe i od set send send outgoing data or a ms Some socket routines require arguments to be stored convert between network byt on in one or both directions getpeername: after a connection arrives, obtain the htonf host to network long(32bits) getsockopt: obtain the current options for a socket Doing so makes the source code le to ary machines, regardless of its native byte or 8

8 哈工大计算机学院 李全龙 Network Application Development Socket Programming 43 Functions in the socket API: send, sendto, sendmsg „ send, sendto, and sendmsg „ Transfer outgoing data from application send(socket,data,length,flags); sendmsg(socket,msgstruct,flags); sendto(socket,data,length,flags, destaddr,addrlen); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 44 Format of msgstruct struct msgstruct { struct sockaddr *m_saddr; /*dest address*/ struct datavec *m_dvec; /*message (vector)*/ int mdvlength; /*size of vector*/ struct access *m_rights; /*access rights*/ int m_alength; /*size of access rights*/ } 哈工大计算机学院 李全龙 Network Application Development Socket Programming 45 Functions in the socket API: recv, recvfrom, and recvmsg „ recv, recvfrom, and recvmsg „ Transfer incoming data to application recv(socket,buffer,length,flags); recvmsg(socket,msgstruct,flags); recvfrom(socket,buffer,length,flags, senderaddr,saddrlen); 哈工大计算机学院 李全龙 Network Application Development Socket Programming 46 Summary of socket calls used with TCP „ WSAStartup: initialize the socket lib (Windows only) „ WSACleanup: terminate use of socket lib (Windows only) „ socket: create a descriptor for use in network communication „ connect: connect to a remote peer (client) „ closesocket: terminate communication and deallocate a descriptor „ bind: bind a local IP address and protocol port number to a socket „ listen: place the socket in passive mode and set the number of incoming TCP connections the system will enqueue (server) „ accept: accept the next incoming connection (server) „ recv: acquire incoming data from a stream connection or the next incoming msg. 哈工大计算机学院 李全龙 Network Application Development Socket Programming 47 Summary of socket calls used with TCP „ recvfrom: receive the next incoming datagram and record its source endpoint address „ select: wait until the first of a specified set of sockets becomes ready for I/O „ send: send outgoing data or a msg. „ sendto: send an outgoing datagram to a specified endpoint „ shutdown: terminate a TCP connection in one or both directions „ getpeername: after a connection arrives, obtain the remote machine’s endpoint address from socket „ getsockopt: obtain the current options for a socket „ setsockopt: change the options for a socket 哈工大计算机学院 李全龙 Network Application Development Socket Programming 48 Utility routines for integer conversion „ TCP/IP specifies a standard representation for binary integers used in protocol headers: network byte order „ Some socket routines require arguments to be stored in network byte order. „ Several functions can convert between network byte order and local host’s byte order. „ htons: host to network short (16bits) „ ntohs: network to host short „ htonl: host to network long (32bits) „ ntohl: network to host long „ Doing so makes the source code portable to any machines, regardless of its native byte order

Symbolic constants for socket call P arameters Chapter 2: Socket Programming A set of predefined symbolic constants and Chapter goal: data structure declarations that applications Program interface to se to declare data and to specify arguments tocol Such as: SocK sTREAM. SOCK DGRAM a Socket API a In windows should include statement: #include Socket programming model Socket as a client/server model Socket Programming Tutorial(1) indicates a blocking function call. makes a request, then th server responds Host A 8 Socket p rogramming Tutorial(2 Socket Programming Tutorial(3) SERVER indicates a blocking function calL. Step 1: socket() call Prepare data structure to After socker( call manage socket OS is responsible for this □ a command 「[[ It declares a socket to be used

9 哈工大计算机学院 李全龙 Network Application Development Socket Programming 49 Symbolic constants for socket call parameters „ A set of predefined symbolic constants and data structure declarations that applications use to declare data and to specify arguments. „ Such as: SOCK_STREAM, SOCK_DGRAM „ In windows, should include statement: #include 哈工大计算机学院 李全龙 Network Application Development Socket Programming 50 Chapter 2: Socket Programming Chapter goal: „ Program interface to protocol „ Socket API „ Socket programming model 哈工大计算机学院 李全龙 Network Application Development Socket Programming 51 Server Host A The server should always be waiting for requests from the clients. A client makes a request, then the server responds. Client 2 Client 3 Host C socket socket Client 1 Host B Socket as a client/server model Reply Request Socket 哈工大计算机学院 李全龙 Network Application Development Socket Programming 52 SERVER bind() listen() accept() recv() send() closesocket() CLIENT socket() connect() send() closesocket() socket() 1: Connection Request 2. Send a command 3. Receive the result recv() “*” indicates a blocking function call. * * * * Socket Programming Tutorial(1) Request Acknowledge WSACleanup WSACleanup WSAStartup WSAStartup 哈工大计算机学院 李全龙 Network Application Development Socket Programming 53 “*” indicates a blocking function call. SERVER bind() listen() accept() recv() CLIENT socket() connect() send() closesocket() socket() * * * 1: Connection Request 2. Send a command 3. Receive the result recv() send() We are not doing this... * 4. END Socket Programming Tutorial(2) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 54 Server Step 1: socket(_) call It declares a socket to be used. After socket(_) call: • Prepare data structure to manage socket • OS is responsible for this Socket Programming Tutorial(3)

Socket Programming Tutorial(4) Socket Programming Tutorial(5) tep 2: bind() call Step 3: listen( call After bvmd call: After listen( call: buffer for incoming connections A logical connecting point at a host using socket listen(socket_mumber_of_conned Port 2I. FIP We need to specify how held n the buller when SERVER ts a process to a specific port 024 and above aya lable tou s busy (cant accep a reque Socket Programming Tutorial(6) Socket Programming Tutorial(n) Step 4-Part 1: accept( call Step 4-Part 2: accept() call After accept( call: The accept( call returns another port number and establish another connection o OS duplicates the socket connection new port is chosen by Socket Programming Tutorial(8) Socket Programming Tutorial(9) Step 5: recv( and sendo call Step 6: closesocket ( call The server and clent communicate using the second socket Close the second socket and leave the first socket for next client 10

10 哈工大计算机学院 李全龙 Network Application Development Socket Programming 55 Step 2: bind(_) call It connects a process to a specific port After bind(_) call: 6500 Server Port Numbers: 0~1023: System Reserved Port 21: FTP Port 23: telnet Port 80: HTTP 1024 and above: available to users Port Port = A logical connecting point at a host for two communicating processes using socket Socket Programming Tutorial(4) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 56 Step 3: listen(_) call After listen(_) call: 6500 Buffer We need to specify how many connection requests should be held in the buffer when SERVER is busy (can’t accept a request). listen (socket_id, number_of_connection); listen( ) system call: prepare memory buffer for incoming connections Server Socket Programming Tutorial(5) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 57 Step 4 - Part 1: accept(_) call The server process accepts a request from a client After accept(_) call: Server Client 6500 accept ( ) function is a blocking function Socket Programming Tutorial(6) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 58 Step 4 - Part 2: accept(_) call The accept(_) call returns another port number and establish another connection Client Server 7100 6500 d A new port is chosen by OS c OS duplicates the socket connection Socket Programming Tutorial(7) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 59 Step 5: recv(_) and send() call Client Server 7100 The server and client communicate using the second socket 6500 Data transmission Socket Programming Tutorial(8) 哈工大计算机学院 李全龙 Network Application Development Socket Programming 60 Step 6: closesocket (_) call Client Server 7100 Close the second socket and leave the first socket for next client 6500 Socket Programming Tutorial(9)

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共11页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有