Home |
Function recvfrom |
Function recvfromReceive data at a socket and return source address information. int recvfrom(SOCKET socket, char * buffer, int buflen, int flags, struct sockaddr * from, int * fromlen); ParameterssocketSocket returned by socket. bufferBuffer to receive to. buflenMaximum bytes to receive. flagsControl flags (MSG_PEEK). See recv for details. fromPort number and IP address of the node that sent the data; port number and IP address are in network byte order. This parameter has type struct sockaddr * for historical reasons. Use type struct sockaddr_in * instead. fromlenPointer to length of the structure pointed to by parameter from. return valueThe number of bytes received, 0 if end of file (TCP only), or SOCKET_ERROR on error. If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Error Codes further describes each error. End of file is detected when the other side closes (i.e. sends a FIN) and the input window is empty. Possible values for this function are:
This routine will receive up to buflen bytes from the connection and copy the data to *buffer. It will never return more than buflen bytes but will return as soon as bytes are available in the buffer. If parameter from is non-zero, the IP address and port number of the endpoint where the data came from is returned to the caller in *from. The size of the from structure is passed to recvfrom in fromlen and recvfrom returns the number of valid bytes stored in from in fromlen. For UDP, connect may be called before recvfrom but it is not necessary. For TCP, the socket must be connected before calling recvfrom. If no data is available and the socket is in blocking mode, recvfrom blocks on the read signal until any data is available. If no data is available and the socket is in non-blocking mode, recvfrom returns immediately with an error condition. select may be called prior to calling recvfrom to determine when data is available. For TCP, if the socket option SO_TCP_NO_COPY is set, the incoming packets are stored in the input window without copying the data. Therefore, copying data to the user buffer is the only copying which is done. For TCP, this function returns 0 if a FIN packet has been received (i.e. the remote host did a closesocket) and the input window is empty. Raw packets are queued on every socket whose protocol matches the protocol field in the packet. The socket's protocol is a parameter to socket. The data written to buffer includes the IP header. In blocking mode, this function blocks infinitely. Use select for a timeout capability. Warning: For UDP, each call to recv dequeues one packet from the socket's input queue. If there is more data in the packet than the caller requested, the residual data will be lost.
|