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 message7 哈工大计算机学院 李全龙 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 connectionoriented 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