正在加载图片...
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 88 哈工大计算机学院 李全龙 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
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有